sol5

12
Solutions for Homework 5 CSG113 Design and Analysis of Algorithms Problem 1 (based on solution by Animesh Kumar) What is the difference between the binary search tree property and min-heap property? Can the min-heap property be used to print out the keys of a n-node tree in sorted order in O(n) time? Explain how or why not. Min-heap property is different from the binary-search-tree property in following manner: The min heap by its definition requires the smallest element in a sub-tree to be the root and the larger elements to be the left and right children. The relative magnitudes of the children don’t matter whereas in a binary-search tree the root of a sub-tree is greater than its left child and smaller than its right child, which means the smallest element in a binary-search-tree would be the left most child of the left sub-tree. The min-heap property cannot be used to print the keys of an n-node tree in sorted order in О(n) time because the lower bound for heapsort is Ω(nlogn). We prove this by noticing that for the first half of the array, when we extract maximum element for the i-th time, we replace it with a leaf and then have to propagate that leaf down for θ(log(n-i)) steps. Since i < n/2, each time the amount of work to restore a heap is θ (log n). Since this amount of work is done n/2 times, the total work for extracting the first half of the array is θ (n log n) and hence the total sorting time is also θ (n log n). This shows that we cannot sort in O(n) time with Heapsort. Problem 2 (solution by Prabhu Anand Nakkeeran) Write the TREE-PREDESSOR procedure. TREE-PREDECESSOR(x)

Upload: gobara-dhan

Post on 28-Dec-2015

2 views

Category:

Documents


0 download

DESCRIPTION

Tree

TRANSCRIPT

Page 1: sol5

Solutions for Homework 5

CSG113 Design and Analysis of Algorithms

Problem 1 (based on solution by Animesh Kumar)What is the difference between the binary search tree property and min-heap property? Can the min-heap property be used to print out the keys of a n-node tree in sorted order in O(n) time? Explain how or why not.

Min-heap property is different from the binary-search-tree property in following manner: The min heap by its definition requires the smallest element in a sub-tree to be the root and the larger elements to be the left and right children. The relative magnitudes of the children don’t matter whereas in a binary-search tree the root of a sub-tree is greater than its left child and smaller than its right child, which means the smallest element in a binary-search-tree would be the left most child of the left sub-tree.

The min-heap property cannot be used to print the keys of an n-node tree in sorted order in О(n) time because the lower bound for heapsort is Ω(nlogn). We prove this by noticing that for the first half of the array, when we extract maximum element for the i-th time, we replace it with a leaf and then have to propagate that leaf down for θ(log(n-i)) steps. Since i < n/2, each time the amount of work to restore a heap is θ (log n). Since this amount of work is done n/2 times, the total work for extracting the first half of the array is θ (n log n) and hence the total sorting time is also θ (n log n). This shows that we cannot sort in O(n) time with Heapsort.

Problem 2 (solution by Prabhu Anand Nakkeeran)Write the TREE-PREDESSOR procedure.

TREE-PREDECESSOR(x)If left[x] != NIL

Then return TREE-MAXIMUM(left[x])Y = P[x]While y!= NIL && x = left[y]

Do x = y y = P[y]

return y

here TREE-MAXIMUM is from the book

TREE-MAXIMUM(x)while right[x] != NIL

do x = right[x]return x

Page 2: sol5

Problem 4 (solution by Tracy Ann Rodrigues)Professor Bunyan thinks he has discovered a remarkable property of BST. Suppose that the search for key k in a BST ends up in a leaf. Consider three sets: A, the keys to the left of the search path; B, the keys on the sear path; and C, the keys to the right of the search path. Professor Bunyan claims that any three keys , and must satisfy . Give a smallest possible counterexample to the professor’s claim.

Following is a small counter example to the claim:Consider that we are searching for the key 4 in the following binary search tree.

The search path has been shaded in gray. Hence node with values 1, 3, 4 belong to the set B and node with value 2 belongs to set A.

But 2 > 1, that a > b, where a A and b B.Hence the claim is false.

Problem 5 (solution by Saurabh Lodha)Is the operation of deletion “commutative” in the sense that deleting x and then y from a BST leaves the same tree as deleting y and then x?

Delete operation on binary search tree is not commutative. This can be proved by example given below.

14

8 19

5 1118 25

23

Page 3: sol5

(a) BINARY SEARCH TREE

(b) BINARY SEARCH TREE AFTER DELETION OF 18 and then 19

(c)BINARY SEARCH TREE AFTER DELETION OF 19 and then 18

Diagram b depicts binary search tree when 18 is deleted first and then 19.Diagram c depicts binary search tree when 19 is deleted first and then 18.

Since b and c are not similar so we have that deletion is not commutative.

Problem 6 (solution by Vicky Chan)Explain how to find the minimum key stored in a B-tree and how to find the predecessor of a given key stored in a B-tree.

To find the minimum key:

B-TREE-MINIMUM(x)while not leaf[x]

DISK-READ(c1[x])x = c1[x]

14

8 25

5 11 23

14

8 23

5 11 25

Page 4: sol5

return key1[x]

To find the predecessor of k:

B-TREE-MAXIMUM(x)i = n[x]while not leaf[x]

DISK-READ(ci[x])x = ci[x]

i = n[x]return keyi[x]

B-TREE-PREDECESSOR(x,i)// keyi[x] = k

if ci[x] != NILthen return B-TREE-MAXIMUM(ci[x])else if i > 1

then return keyi-1[x]else // when k is a leaf and the leftmost key

y = p[x]while y != NIL and c1[p[y]] = y

y = p[y]j = 1while cj[y] ! = y

j = j + 1if j = 1 then return NIL // no predecessor

else return keyj-1[y]

Problem 7 (solution by Justin Sheehy)Show the results of deleting C, P and V, in order, from the tree of Figure 18.8(f)

Page 5: sol5
Page 6: sol5

Problem 8 (solution by Justin Sheehy)Draw the complete BST of height 3 on the keys <1,2,..,15>. Add the NIL leave and color the nodes in three different ways such that the black heights of the resulting red-black trees are 2, 3, and 4.

Page 7: sol5

Problem 9 (solution by Jiten S. Gala) Draw the red-black tree that results after tree-insert is called on the tree in Figure 13.1 with key 36. If the inserted node is colored red, is the resulting tree a red-black tree? What if it is colored black?

Solution:

Consider the following RED- BLACK TREE:

If we add the node with key 36, using TREE-INSERT in the Tree it will be placed at following location.

21

17

41

14

47

30

26

7

10

16

19

28

20

3

12

15

23

38

35

39

Page 8: sol5

The RED-BLACK Tree becomes:

Now if the node with key 36 is red coloured then it violates following RED-BLACK Tree Property.

A red node in RED-BLACK Tree cannot have a Red node as its child.So the resulting tree is not a RED- BLACK Tree.

If the node with key 36 is colored black then: It violates the RED- BLACK following Tree Property

For each node, all paths from the node to descendent leaves contain the same number of black nodes.

Hence the resulting tree will not be a Red – Black tree.

21

17

41

14

47

30

26

7

10

16

19

28

20

3

12

15

23

38

35

39

36

Page 9: sol5

Problem 10 (solution by Zarana Desai)Show that any arbitrary n-node BST can be transformed into any other arbitrary n-node BST using O(n) rotations.

With at most (n − 1) right rotations, we can convert any binary search tree into one that is just a right-going chain.Here in this case assume that leaves are full fledged nodes and that the sentinels can be ignored.Right spine is the root and all descendants of the root that are reachable by following only right pointers from the root. i.e the right subtree.

A binary search tree that is just a right-going chain has all n nodes in the right sub tree. As long as the tree is not just a right spine, we can repeatedly find some node y on the right spine that has a non-leaf left child x and then perform a right rotation on y.

Fig: from e-book of the textbook.

In this figure any of α, β, and γ can be empty subtrees.

When we rotate this, the rotation adds x to the right spine, and no other nodes are removed from the right spine. Thus, this right rotation increases the number of nodes in the right spine by 1. Any binary search tree starts out with at least one root node which can be considered in the right spine. Also if there are any nodes not on the right spine, then at least one of the nodes has a parent on the right spine. Thus, at most n − 1 right rotations are needed to put all nodes in the right spine, so that the tree consists of a single right-going chain.

If we knew the sequence of right rotations that transforms an arbitrary binary search tree T to a single right-going chain T’, then we could perform this sequence in reverse-turning each right rotation into its inverse left rotation – to transform T’ back into T.

Therefore, here is how we can transform any binary search tree T1 into any other binary search tree T2. Let T ' be the unique right-going chain consisting of the nodes of T1

Page 10: sol5

(which is the same as the nodes of T2). Let r = ⟨r1, r2, …, rk⟩ be a sequence of right rotations that transforms T1 to T ', and let r' = ⟨r'1, r'2, …, r'k’⟩ be a sequence of right rotation that transforms T2 to T '. We know that there exist sequence r and r' with k, k' ≤ n − 1. For each right rotation r'i let l'i be the corresponding inverse left rotation. Then the sequence ⟨r1, r2, …, rk, l'k’, l'k’-1, …, l'2, l’1⟩ transforms T1 to T2 in at most 2n − 2 rotations.