Вы находитесь на странице: 1из 10

Solutions for Homework 5

CSG113 Design and Analysis of Algorithms


Problem 1 (based on solution by Animesh Kumar
!hat is the differen"e between the binary sear"h tree #ro#erty and min$hea#
#ro#erty% Can the min$hea# #ro#erty be used to #rint out the keys of a n$node tree
in sorted order in &(n time% '(#lain 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 dont 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". $e prove this by noticing
that for the first half of the array, when we e%tract ma%imum 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. 'ince
i ( n)*, each time the amount of work to restore a heap is & !log n". 'ince this amount of
work is done n)* times, the total work for e%tracting 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
+!n" time with ,eapsort.
Problem * (solution by Prabhu Anand Nakkeeran
!rite the +,''$P,'D'SS&, #ro"edure)
T-..-/-.0.1.''+-!%"
2f left3%4 56 728
Then return T-..-M9:2M;M!left3%4"
< 6 /3%4
$hile y56 728 == % 6 left3y4
0o % 6 y
y 6 /3y4
return y
here T-..-M9:2M;M is from the book
T-..-M9:2M;M!%"
while right3%4 56 728
do % 6 right3%4
return %
Problem - (solution by Tracy Ann Rodrigues
Professor .unyan thinks he has dis"o/ered a remarkable #ro#erty of .S+) Su##ose
that the sear"h for key k in a .S+ ends u# in a leaf) Consider three sets0 A1 the keys
to the left of the sear"h #ath2 .1 the keys on the sear #ath2 and C1 the keys to the
right of the sear"h #ath) Professor .unyan "laims that any three keys
A a
1
B b

and
C c
must satisfy
c b a
) Gi/e a smallest #ossible "ountere(am#le to the
#rofessor3s "laim)
>ollowing is a small counter e%ample to the claim:
1onsider that we are searching for the key ? in the following binary search tree.
The search path has been shaded in gray. ,ence node with values @, A, ? belong to the set
B and node with value * belongs to set 9.
But * C @, that a C b, where a

9 and b

B.
,ence the claim is false.
Problem 5 (solution by Saurabh Lodha
4s the o#eration of deletion 5"ommutati/e6 in the sense that deleting ( and then y
from a .S+ lea/es the same tree as deleting y and then (%
0elete operation on binary search tree is not commutative. This can be proved by
e%ample given below.


@?
D
@E
F @@
@D *F
*A
(a .47A,8 S'A,CH +,''

!b" .47A,8 S'A,CH +,'' A9+', D':'+4&7 &9 1; and then 1<


(".47A,8 S'A,CH +,'' A9+', D':'+4&7 &9 1< and then 1;
0iagram b depicts binary search tree when @D is deleted first and then @E.
0iagram c depicts binary search tree when @E is deleted first and then @D.
'ince b and c are not similar so we have that deletion is not commutative.
Problem = (solution by Vicky Chan
'(#lain how to find the minimum key stored in a .$tree and how to find the
#rede"essor of a gi/en key stored in a .$tree)
To find the minimum key:
B-T-..-M272M;M!%"
while not leaf3%4
02'G--.90!c
@
3%4"
% 6 c
@
3%4
@?
D
*F
F @@ *A
@?
D
*A
F @@ *F
return key
@
3%4
To find the predecessor of k:
B-T-..-M9:2M;M!%"
i 6 n3%4
while not leaf3%4
02'G--.90!c
i
3%4"
% 6 c
i
3%4
i 6 n3%4
return key
i
3%4
B-T-..-/-.0.1.''+-!%,i"
)) key
i
3%4 6 k
if c
i
3%4 56 728
then return B-T-..-M9:2M;M!c
i
3%4"
else if i C @
then return key
i-@
3%4
else )) when k is a leaf and the leftmost key
y 6 p3%4
while y 56 728 and c
@
3p3y44 6 y
y 6 p3y4
H 6 @
while c
H
3y4 5 6 y
H 6 H I @
if H 6 @ then return 728 )) no predecessor
else return key
H-@
3y4
Problem > (solution by Justin Sheehy
Show the results of deleting C1 P and ?1 in order1 from the tree of 9igure 1;);(f
Problem ; (solution by Justin Sheehy
Draw the "om#lete .S+ of height 3 on the keys @11*1))115A) Add the 74: lea/e and
"olor the nodes in three different ways su"h that the bla"k heights of the resulting
red$bla"k trees are *1 31 and -)
Problem < (solution by Jiten S !ala
Draw the red$bla"k tree that results after tree$insert is "alled on the tree in 9igure
13)1 with key 3=) 4f the inserted node is "olored red1 is the resulting tree a red$bla"k
tree% !hat if it is "olored bla"k%
'olution:
1onsider the following -.0- B891G T-..:


2f we add the node with key AJ, using T-..-27'.-T in the Tree it will be placed at
following location.
*
@
@
K
?
@
@
?
?
K
A
L
*
J
K
@
L
@
J
@
E
*
D
*
L
A
@
*
@
F
*
A
A
D
A
F
A
E
The -.0-B891G Tree becomes:


7ow if the node with key AJ is red coloured then it violates following -.0-B891G Tree
/roperty.
9 red node in -.0-B891G Tree cannot have a -ed node as its child.
'o the resulting tree is not a -.0- B891G Tree.
2f the node with key AJ is colored black then: 2t violates the -.0- B891G following
Tree /roperty
>or each node, all paths from the node to descendent leaves contain the same number of
black nodes.
,ence the resulting tree will not be a -ed M Black tree.
*
@
@
K
?
@
@
?
?
K
A
L
*
J
K
@
L
@
J
@
E
*
D
*
L
A
@
*
@
F
*
A
A
D
A
F
A
E
A
J
Problem 1B (solution by "arana #esai
Show that any arbitrary n$node .S+ "an be transformed into any other arbitrary n$
node .S+ using &(n rotations)
$ith at most !n N @" right rotations, we can convert any binary search tree into one that is
Hust a right-going chain.
,ere in this case assume that leaves are full fledged nodes and that the sentinels can be
ignored.
-ight 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.
9 binary search tree that is Hust a right-going chain has all n nodes in the right sub tree.
9s long as the tree is not Hust a right spine, we can repeatedly find some node y on the
right spine that has a non-leaf left child % and then perform a right rotation on y.
>ig: from e-book of the te%tbook.
2n this figure any of O, P, and Q can be empty subtrees.
$hen we rotate this, the rotation adds $ 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 @. 9ny binary search tree starts out with at least one root node which
can be considered in the right spine. 9lso 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 N @ right
rotations are needed to put all nodes in the right spine, so that the tree consists of a single
right-going chain.
2f 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 M to transform T back into T.
Therefore, here is how we can transform any binary search tree T@ into any other binary
search tree T*. 8et T R be the unique right-going chain consisting of the nodes of T@
!which is the same as the nodes of T*". 8et r 6 r@, r*, S, rk be a sequence of right
rotations that transforms T@ to T R, and let rR 6 rR@, rR*, S, rRk be a sequence of right
rotation that transforms T* to T R. $e know that there e%ist sequence r and rR with k, kR T
n N @. >or each right rotation rRi let lRi be the corresponding inverse left rotation. Then the
sequence r@, r*, S, rk, lRk, lRk-@, S, lR*, l@ transforms T@ to T* in at most *n N *
rotations.

Вам также может понравиться