INF 2220: algorithms and data structures Series 1

Similar documents
Binary Search Trees. Data in each node. Larger than the data in its left child Smaller than the data in its right child

Binary Search Trees CMPSC 122

Binary Search Trees (BST)

DATA STRUCTURES USING C

Ordered Lists and Binary Trees

1) The postfix expression for the infix expression A+B*(C+D)/F+D*E is ABCD+*F/DE*++

Data Structure [Question Bank]

Data Structures and Algorithms

root node level: internal node edge leaf node Data Structures & Algorithms McQuain

Converting a Number from Decimal to Binary

A binary search tree is a binary tree with a special property called the BST-property, which is given as follows:

Symbol Tables. Introduction

Data Structures, Practice Homework 3, with Solutions (not to be handed in)

Full and Complete Binary Trees

TREE BASIC TERMINOLOGIES

Binary Heaps * * * * * * * / / \ / \ / \ / \ / \ * * * * * * * * * * * / / \ / \ / / \ / \ * * * * * * * * * *

Binary Search Trees. A Generic Tree. Binary Trees. Nodes in a binary search tree ( B-S-T) are of the form. P parent. Key. Satellite data L R

A binary search tree or BST is a binary tree that is either empty or in which the data element of each node has a key, and:

Questions 1 through 25 are worth 2 points each. Choose one best answer for each.

Binary Trees and Huffman Encoding Binary Search Trees

Data Structures Fibonacci Heaps, Amortized Analysis

From Last Time: Remove (Delete) Operation

CSE 326: Data Structures B-Trees and B+ Trees

Previous Lectures. B-Trees. External storage. Two types of memory. B-trees. Main principles

Binary Heap Algorithms

Data Structures. Level 6 C Module Descriptor

Algorithms and Data Structures

MAX = 5 Current = 0 'This will declare an array with 5 elements. Inserting a Value onto the Stack (Push)

B-Trees. Algorithms and data structures for external memory as opposed to the main memory B-Trees. B -trees

The following themes form the major topics of this chapter: The terms and concepts related to trees (Section 5.2).

ECE 250 Data Structures and Algorithms MIDTERM EXAMINATION /5:15-6:45 REC-200, EVI-350, RCH-106, HH-139

1. The memory address of the first element of an array is called A. floor address B. foundation addressc. first address D.

Binary Heaps. CSE 373 Data Structures

Learning Outcomes. COMP202 Complexity of Algorithms. Binary Search Trees and Other Search Trees

Data Structure with C

PES Institute of Technology-BSC QUESTION BANK

Lecture 6: Binary Search Trees CSCI Algorithms I. Andrew Rosenberg

- Easy to insert & delete in O(1) time - Don t need to estimate total memory needed. - Hard to search in less than O(n) time

Lecture Notes on Binary Search Trees

How To Create A Tree From A Tree In Runtime (For A Tree)

Data Structure and Algorithm I Midterm Examination 120 points Time: 9:10am-12:10pm (180 minutes), Friday, November 12, 2010

Sorting revisited. Build the binary search tree: O(n^2) Traverse the binary tree: O(n) Total: O(n^2) + O(n) = O(n^2)

5. A full binary tree with n leaves contains [A] n nodes. [B] log n 2 nodes. [C] 2n 1 nodes. [D] n 2 nodes.

Lecture Notes on Binary Search Trees

GUJARAT TECHNOLOGICAL UNIVERSITY, AHMEDABAD, GUJARAT. Course Curriculum. DATA STRUCTURES (Code: )

Operations: search;; min;; max;; predecessor;; successor. Time O(h) with h height of the tree (more on later).

Krishna Institute of Engineering & Technology, Ghaziabad Department of Computer Application MCA-213 : DATA STRUCTURES USING C

2. (a) Explain the strassen s matrix multiplication. (b) Write deletion algorithm, of Binary search tree. [8+8]

ER E P M A S S I CONSTRUCTING A BINARY TREE EFFICIENTLYFROM ITS TRAVERSALS DEPARTMENT OF COMPUTER SCIENCE UNIVERSITY OF TAMPERE REPORT A

Exercises Software Development I. 11 Recursion, Binary (Search) Trees. Towers of Hanoi // Tree Traversal. January 16, 2013

Chapter 14 The Binary Search Tree

10CS35: Data Structures Using C

Binary Search Trees 3/20/14

Analysis of Algorithms I: Binary Search Trees

CS104: Data Structures and Object-Oriented Design (Fall 2013) October 24, 2013: Priority Queues Scribes: CS 104 Teaching Team

CSC148 Lecture 8. Algorithm Analysis Binary Search Sorting

Sample Questions Csci 1112 A. Bellaachia

GRAPH THEORY LECTURE 4: TREES

6 March Array Implementation of Binary Trees

Heaps & Priority Queues in the C++ STL 2-3 Trees

Analysis of Algorithms I: Optimal Binary Search Trees

Binary Search Tree Intro to Algorithms Recitation 03 February 9, 2011

INTERNATIONAL JOURNAL OF PURE AND APPLIED RESEARCH IN ENGINEERING AND TECHNOLOGY

Why? A central concept in Computer Science. Algorithms are ubiquitous.

Efficiency of algorithms. Algorithms. Efficiency of algorithms. Binary search and linear search. Best, worst and average case.

Data Structures and Algorithms Written Examination

Outline BST Operations Worst case Average case Balancing AVL Red-black B-trees. Binary Search Trees. Lecturer: Georgy Gimel farb

Analysis of Binary Search algorithm and Selection Sort algorithm

In mathematics, it is often important to get a handle on the error term of an approximation. For instance, people will write

Algorithms Chapter 12 Binary Search Trees

S. Muthusundari. Research Scholar, Dept of CSE, Sathyabama University Chennai, India Dr. R. M.

Parallelization: Binary Tree Traversal

Data Structures. Algorithm Performance and Big O Analysis

Data storage Tree indexes

B+ Tree Properties B+ Tree Searching B+ Tree Insertion B+ Tree Deletion Static Hashing Extendable Hashing Questions in pass papers

Class Notes CS Creating and Using a Huffman Code. Ref: Weiss, page 433

Big Data and Scripting. Part 4: Memory Hierarchies

The ADT Binary Search Tree

The Union-Find Problem Kruskal s algorithm for finding an MST presented us with a problem in data-structure design. As we looked at each edge,

Any two nodes which are connected by an edge in a graph are called adjacent node.

Home Page. Data Structures. Title Page. Page 1 of 24. Go Back. Full Screen. Close. Quit

Rotation Operation for Binary Search Trees Idea:

2. FINDING A SOLUTION

The Tower of Hanoi. Recursion Solution. Recursive Function. Time Complexity. Recursive Thinking. Why Recursion? n! = n* (n-1)!

CSC 180 H1F Algorithm Runtime Analysis Lecture Notes Fall 2015

Class Overview. CSE 326: Data Structures. Goals. Goals. Data Structures. Goals. Introduction

Exam study sheet for CS2711. List of topics

Data Structures UNIT III. Model Question Answer

Motivation Suppose we have a database of people We want to gure out who is related to whom Initially, we only have a list of people, and information a

Cpt S 223. School of EECS, WSU

Algorithms and Data Structures Written Exam Proposed SOLUTION

M-way Trees and B-Trees

Analysis of a Search Algorithm

CS211 Spring 2005 Final Exam May 17, Solutions. Instructions

A Comparison of Dictionary Implementations

Algorithms and Data Structures

Lecture Notes on Linear Search

Fundamental Algorithms

Why Use Binary Trees?

CSE 326, Data Structures. Sample Final Exam. Problem Max Points Score 1 14 (2x7) 2 18 (3x6) Total 92.

Transcription:

Universitetet i Oslo Institutt for Informatikk D. Karabeg, I. Yu INF 2220: algorithms and data structures Series 1 Høst 2014 19. 08. 2014 Topic: Function growth & estimation of running time, trees (Exercises with hints for solution) Issued: 19. 08. 2014 Exercise 1 (Growth of functions) Order the following functions by growth rate: N, N, N 1.5, N 2, N log N, N log log N, N log 2 N, N log(n 2 ), 2/N, 2 N, 2 N/2, 37, N 2 log N, N 3. Indicate which functions grow at the same rate. Solution: [of 1]The first one is of course a anomaly, since it grows negatively. Hardly any algorithm will ever get faster on larger input. 2/N, 37, N, N, N log log(n), N log N, N log N 2, N log 2 N, N 1.5, N 2, N 2 log N, N 3, 2 N/2, 2 n Exercise 2 (O-notation) The statements below show some features of big-o notation for the functions f f(n) and g g(n). Determine whether each of the following statements is true or false and correct the formula in the latter case. 1. O(f g) = O(f) O(g) 2. O(f) < O(g) for f(n) log n C 1, g(n) log n C 2 some constants C 1, C 2 where C 1 < C 2 Solution: 1. TRUE 2. FALSE, O(f) = O(g) = O(log N) Exercise 3 (Analysis of running time) Estimate the running time of the following program fragments (a big-o analysis). In the fragments, the variable n, an integer, is the input. f o r ( i = 0 ; i < n ; i += 2) f o r ( j = 0 ; j < n ; j++)

f o r ( i n t i = 0 i < n ; i ++) f o r ( i n t j = 0 j < n ; j++) f o r ( j = 0 ; j < n n ; j++) f o r ( j = 0 ; j < i ; j++) f o r ( j = 0 ; j < n n ; j++) f o r ( k = 0 ; k < j ; k++) f o r ( i = 1 ; i < n ; i = i 2) Solution: [Analysis of running time] O(N) O(N) O(N 2 ) O(N) O(N 3 ) O(N 2 ) O(N 5 ) O(log N) Note that of course the analysis of given programs (in this exercise more of simple program fragments/loops) is a (much) simpler problem than runtime complexity analysis of a problem. When analysing a problem, one is typically interested in finding an estimation (for instance a worst-case estimation) of the best of all possible algorithms! The same remark of course applies to the next exercise. Exercise 4 (Analysis of running time) 1. The following program snippet sorts an integer array int[] A = new int[n]. What s the order of running time for that in big-o notation. f o r ( i n t i = 0 ; i < n ; i ++) { minj = i ; f o r ( j = i + 1 ; j < n ; j++) { i f (A[ j ] < A[ minj ] ) { minj = j ; bytt ( i, minj ) ; 2. What s the order of running time for that in big-o notation for the following fragment. Pay attention especially for the conditional inside the second nested loop. f o r ( i n t i = 1 ; i <= n ; i ++) { f o r ( i n t j =1; j <= i i ; j++) { i f ( j % i == 0) { f o r ( i n t k = 0 ; k < j ; k++ ) 2

3. For a given n > 0, which value will be found in the variable L2 after executing the following program snippet. The answer should be given as a function of n. Of which order is the running time of this program in big-o notation? i = 1 ; L2 = 1; w h i l e ( i <= n ) { i = i 2 ; L2++; Solution: 1. O(N 2 ) 2. O(N 4 ) 3. O(log 2 N) Exercise 5 (Terminology of trees and tree traversal) For the given tree, determine what is the root? which are the leaves what s the tree s height Give the result of preorder, postorder, inorder, and level-order traversal. For all the nodes of the tree name the parent list the children list the siblings compute the height, depth, and size. 3

Nodes Parent Children Siblings Height Depth Size a - b, c - 4 0 13 b a d c 3 1 5 c a e, f b 3 1 7 d b g, h e, f 2 2 4 e c i d, f 1 2 2 f c j d, e 2 2 4 g d k h, i, j 1 3 2 h d - g, i, j 0 3 1 i e - g, h, j 0 3 1 j f l, m g, h, i 1 3 3 k g - l, m 0 4 1 l j - k, m 0 4 1 m j - k, l 0 4 1 a / \ / \ / \ b c \ / \ d e f / \ / / g h i j \ / \ k l m Solution: [Terminology of trees and tree traversal] root: a leaves: k, h, i, l, m tree s depth: 4 preorder: a, b, d, g, k, h, c, e, i, f, j, l, m postorder: k, g, h, d, b, i, e, l, m, j, f, c, a inorder: b, g, k, d, h, a, i, e, c, l, j, m, f Exercise 6 (Binary search tree - insertion and deletion) Show the result of inserting 6, 4, 8, 5, 1, 9, 7, 11, 2 into an initially empty binary search tree. Show the result of first deleting 1 (from the previously constructed tree), and then 6. Solution: [Binary search tree - insertion and deletion] 4

6 7 / \ / \ / \ / \ 4 8 4 8 / \ / \ / \ \ 1 5 7 9 2 5 9 \ \ \ 2 11 11 Exercise 7 (Non-unique search keys) We use a binary search tree to store a number of elements containing an integer value (of type int) together with a number of other data. We assume that each node has a pointer to its left, resp. right child, as usual. Different from most examples in the lecture, we allow here that different elements can have the same value the other data can be different and they are supposed to be stored in different nodes. 1. A possible solution does the following when inserting a value: goes further down the tree, if hitting an object carrying the same value. We can always chooses to go down further in the right subtree if we encounter an object with the same value. Write a insert-method that implements this idea and sketch some typical trees that result in that implementation. 2. In which order will we get out object sharing the same value if we print trees that result from above under 1, when we follow an infix traversal? What happens if we follow down the left children instead the right ones, as in 1? 3. In this part of the exercise, the nodes with the same value should be put into a list starting at the first node with that value. This list, however, requires additional pointers, but we can do it as follows: If we insert an object and there is alread exactly one with this value in the tree, so link we that element into the between the old node and its right-hand subtree. If later on more object with the same value should be inserted, they well be linked into a list from that second object where the left-child-pointer is used as list-pointer. Sketch some examples, and write an insert method based in that idea. 4. when printing out trees constructed as described under 3 in infix order, nodes with the same values still end up in one batch. But in which order are they actually processed? Write a modified print-method which prints nodes with the same values in the order they had been filled in originally. Exercise 8 (Frequency Tree) We want to use a binary search tree to analyze the play Vildanden from Henrik Ibsen. First read all the words which are separated by, e.g. ;,?, etc, from the file and insert them into an initially binary search tree. All the words which are different from upper case and lower case are considered to be the same. Each node in the tree is corresponding to a unique word in the file. Each node should remember the frequency of the corresponding word appear in the play. Create a separate frequency tree which is sorted on the frequency of each word. 5

Write a sorted list of the N most frequently used words (e.g. N = 20). Write a list of all words having frequency between X and Y (which may be equal). Calculate the depth of the left and right subtrees for both the binary search tree and the frequency tree. Are the left and right subtrees balanced? If not, propose a way to make the tree more balanced, and implement and test the proposal. 6