CSE3358 Problem Set 7 Solution

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

Algorithms Chapter 12 Binary Search Trees

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

Binary Search Trees CMPSC 122

Analysis of Algorithms I: Binary Search Trees

Ordered Lists and Binary Trees

Converting a Number from Decimal to Binary

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

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

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

Data Structures and Algorithms

Data Structure [Question Bank]

DATA STRUCTURES USING C

Binary Search Trees. Each child can be identied as either a left or right. parent. right. A binary tree can be implemented where each node

PES Institute of Technology-BSC QUESTION BANK

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

Data Structures. Level 6 C Module Descriptor

Data Structures Fibonacci Heaps, Amortized Analysis

Introduction to Algorithms March 10, 2004 Massachusetts Institute of Technology Professors Erik Demaine and Shafi Goldwasser Quiz 1.

Rotation Operation for Binary Search Trees Idea:

Binary Search Trees (BST)

Binary Trees and Huffman Encoding Binary Search Trees

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)

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

TREE BASIC TERMINOLOGIES

Exam study sheet for CS2711. List of topics

10CS35: Data Structures Using C

GRAPH THEORY LECTURE 4: TREES

Cpt S 223. School of EECS, WSU

Output: struct treenode{ int data; struct treenode *left, *right; } struct treenode *tree_ptr;

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

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:

Full and Complete Binary Trees

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

From Last Time: Remove (Delete) Operation

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

CS473 - Algorithms I

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

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

A binary heap is a complete binary tree, where each node has a higher priority than its children. This is called heap-order property

Binary Heap Algorithms

Algorithms. Margaret M. Fleck. 18 October 2010

Analysis of Algorithms, I

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

Introduction to Data Structures and Algorithms

Persistent Binary Search Trees

Chapter 14 The Binary Search Tree

Lecture 2 February 12, 2003

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

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

Data Structure with C

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

Introduction to Data Structures and Algorithms

Sequential Data Structures

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

Lecture Notes on Binary Search Trees

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

Binary Heaps. CSE 373 Data Structures

Analysis of Algorithms I: Optimal Binary Search Trees

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

Data Structures and Algorithms Written Examination

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

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

Algorithms and Data Structures

Lecture Notes on Binary Search Trees

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

The ADT Binary Search Tree

Information Theory and Coding Prof. S. N. Merchant Department of Electrical Engineering Indian Institute of Technology, Bombay

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.

Sample Questions Csci 1112 A. Bellaachia

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

Symbol Tables. Introduction

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,

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

Algorithms and Data Structures

6 March Array Implementation of Binary Trees

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

OPTIMAL BINARY SEARCH TREES

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


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

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

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

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

Lecture 1: Course overview, circuits, and formulas

Cost Model: Work, Span and Parallelism. 1 The RAM model for sequential computation:

To My Parents -Laxmi and Modaiah. To My Family Members. To My Friends. To IIT Bombay. To All Hard Workers

COMP 250 Fall 2012 lecture 2 binary representations Sept. 11, 2012

Algorithms and Data Structures Written Exam Proposed SOLUTION

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

Data Structures. Jaehyun Park. CS 97SI Stanford University. June 29, 2015

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

Section IV.1: Recursive Algorithms and Recursion Trees

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

COMPSCI 105 S2 C - Assignment 2 Due date: Friday, 23 rd October 7pm

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

M-way Trees and B-Trees

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

CPSC 211 Data Structures & Implementations (c) Texas A&M University [ 221] edge. parent

Sorting Algorithms. Nelson Padua-Perez Bill Pugh. Department of Computer Science University of Maryland, College Park

Transcription:

CSE3358 Problem Set 7 Solution Problem : A remarkable property of BST (0 points) Professor R. E. Mark Cable thinks he has discovered a remarkable property of binary search trees. Suppose that the search for key k in a binary search tree ends up in a leaf. Consider three sets: A, the keys to the left of the search path; B, the keys on the search path; and C, the keys to the right of the search path. Professor R. E. Mark Cable claims that any three keys a A, b B, and c C must satisfy a b c. Give a smallest possible counter example to the professor s claim. ANSWER: Here s a smallest counter example: 3 2 4 If we perform SEARCH(root[T ], 4), the search path will end up in a leaf and will consist of the gray nodes above. Obviously, 2 is to the left of the search path, but it is not to all keys on the search path, namely 2 >. Problem 2: Height and average depth (30 points) Consider a randomly built binary search tree, i.e. distinct keys are inserted into an initially empty binary search tree in random order. It is a fact that a randomly built binary search tree on n nodes has an expected height Θ(log n). In class we argued this fact by drawing a similarity to the recursive tree of Randomized Quicksort (see your notes). A common mistake is to claim the above by showing that the expected average depth in the binary search tree is Θ(log n), where the average depth is given by This problem is designed to expose this common mistake. n i= depth(x i) n, because that s easier to prove. Let P (n) = n i= depth(x i ) for a binary search tree on n nodes. Therefore, the average depth in the binary search tree is P (n) n. (a) (5 points) Show that P (n) obeys the following recurrence for a complete binary search tree: P (n) = 2P (n/2) + Θ(n) and conclude that the average depth in a complete binary search tree is Θ(log n) by solving for P (n).

ANSWER: Let L and R be the left and right subtrees of the root respectively. Consider a tree having k nodes in L and, therefore, n k nodes in R. n P (n) = depth(x i ) = depth(x i ) + depth(x i ) i= x i L x i R The root contributes to the depth of every node in L and R. Therefore, P (n) = P (k) + P (n k) + n If the tree is complete then k = n/2 and hence n k = n/2. Therefore, P (n) = 2P ( n/2 )+n. If we are interested in the asymptotic behavior of P (n) we can express P (n) as the recurrence P (n) = 2P (n/2) + Θ(n). By the Master theorem, P (n) = Θ(n log n). Therefore, the average depth in the binary search tree will be P (n) = Θ(log n). n Consider the following binary search tree consisting of a complete binary tree with a long tail. complete tree on n nodes tail of n 2 nodes (b) (5 points) What is the height of the above binary search tree in Θ notation? ANSWER: Since the first part is a complete binary tree on n nodes, the height of that three is Θ(log n ) (we can use the same argument as the one for a binary heap). The tail of the tree adds a Θ(n 2 ) to the height of the complete tree. Therefore, the height of this binary search tree is Θ(log n + n 2 ). (c) (5 points) Show that P (n = n + n 2 ) = Θ(n log n + n 2 log n + n 2 2) for the above binary search tree and conclude that the average depth in the above binary search tree is Θ(log n + n2 2 n +n 2 ). Using part (a), P (n) = Θ(n log n )+ x tail depth(x). The i th node on the tail has depth Θ(log n )+i. Therefore, P (n) = Θ(n log n ) + Θ(n 2 log n ) + n 2 i= i = Θ(n log n + n 2 log n + n 2 2). Therefore, the average depth in the binary search tree is P (n) = Θ(log n n + n2 2 n +n 2 ). (d) (0 points) Find a relation between n and n 2 to make the average depth in the above binary search tree Θ(log n) but its height Θ(log n). What is that height in Θ notation? ANSWER: Note that if n 2 > n, then asymptotically, n = Θ(n 2 ) and n +n 2 = Θ(n 2 ) = Θ(n). Therefore, the average depth will be Θ(n) Θ(log n). Hence, we conclude that we must have n 2 < n and as a result n = Θ(n ) and we can work with n instead of n asymptotically. 2 n 2 2

The question is now how big can n 2 be without violating that the average depth is Θ(log n )? Well, n let s set Θ( 2 2 n n +n 2 ) to be Θ(log n ). Since n > n 2, Θ( 2 2 n +n 2 ) = Θ( n2 2 n ). Therefore, we can safely set n 2 = Θ( n log n ) and we get that the average depth is Θ(log n ) = Θ(log n). Let s see what this will do to the height. h = Θ(log n + n 2 ) = Θ(log n + n log n ) = Θ( n log n ) = Θ( n log n) > Θ(log n) (with abuse of notation for >). As a side note, this is the best we can achieve. In other words, given that the average depth is Θ(log n), the height of the tree must be O( n log n). To show this fact note that the sum of depths of nodes on a path of length l is Θ(l 2 ). Therefore, if the height of the tree is more than Θ( n log n), the nodes on the longest path alone contribute more than Θ(n log n) to P (n). Hence, the average depth will be more than Θ(log n). (e) (5 points) How can you intuitively explain the fact that a bound on the average depth in a tree does not imply the same bound on the height of the tree? ANSWER: Simply because the height of a tree is equal to the maximum depth, which can be more than the average. Of course, as mentioned above in the side note, the average depth does impose an upper bound on the height, but does not exlude the fact that the height can be much more than the average depth. Again, the reason is because the height is nothing but the maximum depth. Here s an example from every day life not related to trees. We have 0 students in a class and the average grade is 7/00. Then this imposes an upper bound on any grade. In fact you can claim that no one got 7/00 as a grade; otherwise, the average will be...+7+... > 7 (well... assuming no negative 0 grades). But it is possible to find a student with a grade 70/00. As a trivial example, one student got 70/00 and the remaining students all got 0/00. The average is 7. The maximum grade is 70. Poblem 3: Tree walks (30 points) (a) (0 points) Write recursive pseudocodes similar to the INORDER-TREE-WALK we discussed in class for PREORDER-TREE-WALK and POSTORDER-TREE-WALK (see book page 254 for definitions). ANSWER: PREORDER-TREE-WALK(x) if x NIL then print key[x] PREORDER-TREE-WALK(lef t[x]) PREORDER-TREE-WALK(right[x]) 3

POSTORDER-TREE-WALK(x) if x NIL then POSTORDER-TREE-WALK(lef t[x]) POSTORDER-TREE-WALK(right[x]) print key[x] (b) (0 points) Assume the INORDER-TREE-WALK outputs g,b,e,d,f,a,c,j,k,h,i and the POSTORDER- TREE-WALK outputs g,e,b,f,a,j,k,i,h,c,d. Construct the tree and determine the output of the PREORDER- TREE-WALK. ANSWER: The post order tree walk on a tree rooted at node x outputs x last. Therefore, d is the root of the tree. Looking at the inorder tree walk output, we see that g, b, e must be in the left subtree of d and f, a, c, j, k, h, i must be in the right subtree of d. Now we apply the same reasoning recursively on the left and right subtrees. Among g, b, e, the post order tree walk outputs b last; therefore, b is the root. From the inorder tree walk, we see that g must be on its left and e must be on its right. We continue in this way... d b c g e a h f k i j The PREORDER-TREE-WALK will output: d, b, g, e, c, a, f, h, k, j, i. (c) (0 points) Consider a set S of distinct bit strings, i.e. string over the alphabet {0, }. A special tree, called radix tree, can represent such a set S. Here s the radix tree for S = {0, 0, 0, 00, 0}. 4

0 0 0 0 0 0 00 0 Given a set of distinct binary strings whose length sum is n, show how you can build its radix tree in Θ(n) time and then use it to sort S lexicographically (dictionary order) in Θ(n) time. For example, the output of the sort for the radix tree above should be: 0, 0, 0, 00, 0. Hint: use one of the tree walks. ANSWER: First, we build a radix tree for the set of strings S by inserting the strings into an originally empty radix tree. Insertion takes Θ(n) time since the insertion of each string takes a time proportional to its length (traversing a path through the tree whose length is the length of the string), and the sum of all the string lengths is n. At the end of each insertion, we color the node white and store the corresponding string as its key. Second, we perform a modified preorder walk of the tree that only prints the keys for white nodes. This will print the strings in lexicographic order because: Any node s string is a prefix of all its descendants strings, hence belongs before them in the sorted order A node s left descendants belong before its right descendants because the corresponding strings are identical up to that parent node, and in the next position the left tree s strings have 0 while the right tree s strings have. Problem 4: Binary search tree and Min-Heap (20 points) (a) (0 points) Design an algorithm that converts a binary search tree on n elements into a Min-Heap containing the same elements in Θ(n) time. ANSWER: One possible way of performing this task is to traverse the tree using a tree walk algorithm (inorder, preorder, or postorder) and output the keys of the nodes to an array. Then Perform BUILD- HEAP on the array. Both the tree walk and BUILD-HEAP run in Θ(n) time. In particular if the INORDER-TREE-WALK is used, the array will be sorted and there is no need to run BUILD-HEAP (min-heap). Here s a modified INORDER-TREE-WALKS that outputs the keys to an array. 5

count 0 INORDER-TREE-WALK(root[T ]) INORDER-TREE-WALK(x) if x NIL then INORDER-TREE-WALK(lef t[x]) count count + A[count] key[x] INORDER-TREE-WALK(right[x]) PS: this assumes that a key is the only information stored at a node. If this is not true, we need to copy the whole object into the array, not just the key. 6

(b) (0 points) Very Challenging (EXTRA CREDIT): Do the same thing inplace, i.e. convert the tree into a Min-Heap in Θ(n) time without using any extra storage We adopt the standard representation of a tree with the three left, right, and parent pointers. ANSWER: The idea is the following. First, convert the binary search tree into a sorted linked list. One way of doing this is by starting at the minimum element, and repeatedly call SUCCESSOR, and link the element to its successor. Since the repeated SUCCESSOR calls will walk the tree in the same way the INORDER-TREE-WALK does, the total time for these calls will be Θ(n) (later we will prove this more formally maybe in another exercice). The question is which pointer should we use to link an element to its successor. Messing up with the pointers might cause future successor calls to fail doing the correct thing. As an observation, the left or right pointers of a node x are never used after SUCCESSOR(x) is called. Therefore, either the left or the right pointer can serve to link x to its successor. Here s the code for converting the binary search tree into a sorted linked list using the left pointer as a next pointer. x MINIMUM(root[T ]) root[t ] x while x NIL do y SUCCESSOR(x) lef t[x] y x y Now we can transform the sorted linked list into a min-heap by setting the left, right, and parent pointers appropriately. The idea is that each node will have the earliest available two nodes as its children because we can fill the heap level by level from left to right without having to worry about the order of elements (it is a sorted list now). Here s the code which takes linear time because it just traverses the list once: x root[t ] parent[x] N IL y lef t[x] while x NIL do z is the node to consider as x in the next iteration z lef t[x] y is first node without a parent lef t[x] y if y NIL then parent[y] x find next node without a parent y lef t[y] right[x] y if y NIL then parent[y] x find next node without a parent y lef t[y] x has both left and right defined x z 7

(c) (0 points) Can you do the reverse process, i.e. convert a Min-Heap containing n elements into a binary search tree on the same elements in Θ(n) time? How? or Why not? ANSWER: It is generally not possible to transform a min-heap into a binary search tree on the same elements in linear time. Had this been possible, we would be able to sort in linear time, which is not possible due to the Ω(n log n) lower bound on sorting. Here s how we would sort an array A in linear time: H BUILD-HEAP(A) T convert(h) INORDER-TREE-WALK(root[T ]) Since all these operations take Θ(n) time, we have a Θ(n) time general sorting algorithm. Knowing that this is impossible, the assumption that conversion takes linear time must be wrong. 8