




Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Community
Ask the community for help and clear up your study doubts
Discover the best universities in your country according to Docsity users
Free resources
Download our free guides on studying techniques, anxiety management strategies, and thesis advice from Docsity tutors
Main points of this exam paper are: Consider a Tree, Remarkable Property, Initially Empty, Average Depth, Root Respectively, Asymptotic Behavior, Maximum Depth
Typology: Exams
1 / 8
This page cannot be seen from the preview
Don't miss anything!
CSE3358 Problem Set 7 Solution
Problem 1: A remarkable property of BST (10 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:
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 > 1.
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
∑n i=1 depth(xi) n , because that’s easier to prove.
This problem is designed to expose this common mistake.
Let P (n) =
∑n i=1 depth(xi) for a binary search tree on^ n^ nodes. Therefore, the average depth in the binary search tree is P^ ( nn ).
(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 − 1 − k nodes in R.
P (n) =
∑^ n
i=
depth(xi) =
∑
xi∈L
depth(xi) +
∑
xi∈R
depth(xi)
The root contributes 1 to the depth of every node in L and R. Therefore,
P (n) = P (k) + P (n − 1 − k) + n − 1
If the tree is complete then k = bn/ 2 c and hence n− 1 −k = bn/ 2 c. Therefore, P (n) = 2P (bn/ 2 c)+n−1. If we are interested in the asymptotic behavior of P (n) we can express P (n) as the recurrence P (n) = 2 P (n/2) + Θ(n). By the Master theorem, P (n) = Θ(n log n). Therefore, the average depth in the binary search tree will be P^ ( nn )= Θ(log n).
Consider the following binary search tree consisting of a complete binary tree with a long tail.
tail of n 2 nodes
complete tree on n 1 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 1 nodes, the height of that three is Θ(log n 1 ) (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 1 + n 2 ).
(c) (5 points) Show that P (n = n 1 + n 2 ) = Θ(n 1 log n 1 + n 2 log n 1 + n^22 ) for the above binary search
tree and conclude that the average depth in the above binary search tree is Θ(log n 1 + n^22 n 1 +n 2 ).
Using part (a), P (n) = Θ(n 1 log n 1 )+
∑ x∈ tail depth(x). The^ i th (^) node on the tail has depth Θ(log n 1 )+i.
Therefore, P (n) = Θ(n 1 log n 1 ) + Θ(n 2 log n 1 ) +
∑n 2 i=1 i^ = Θ(n^1 log^ n^1 +^ n^2 log^ n^1 +^ n
2 2 ). Therefore, the average depth in the binary search tree is P^ ( nn )= Θ(log n 1 + n^22 n 1 +n 2 ).
(d) (10 points) Find a relation between n 1 and n 2 to make the average depth in the above binary search tree Θ(log n) but its height 6 = Θ(log n). What is that height in Θ notation?
ANSWER: Note that if n 2 > n 1 , then asymptotically, n = Θ(n 2 ) and n
(^22) n 1 +n 2 = Θ(n^2 ) = Θ(n). Therefore, the average depth will be Θ(n) 6 = Θ(log n). Hence, we conclude that we must have n 2 < n 1 and as a result n = Θ(n 1 ) and we can work with n 1 instead of n asymptotically.
POSTORDER-TREE-WALK(x) if x 6 = N IL then POSTORDER-TREE-WALK(lef t[x]) POSTORDER-TREE-WALK(right[x]) print key[x]
(b) (10 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) (10 points) Consider a set S of distinct bit strings, i.e. string over the alphabet { 0 , 1 }. A special tree, called radix tree, can represent such a set S. Here’s the radix tree for S = { 1011 , 10 , 011 , 100 , 0 }.
0
011
10
100
1011
0 1
1
1
0
0 1
1
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, 011, 10, 100, 1011. 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:
Problem 4: Binary search tree and Min-Heap (20 points)
(a) (10 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.
(b) (10 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 lef t, 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 6 = N IL 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 6 = N IL 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 6 = N IL then parent[y]←x . find next node without a parent y←lef t[y] right[x]←y if y 6 = N IL then parent[y]←x . find next node without a parent y←lef t[y] . x has both left and right defined x←z
(c) (10 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.