hullem A Inight balanced binary tree is a binary tree whose subtrees dite in hei
ID: 3605431 • Letter: H
Question
hullem A Inight balanced binary tree is a binary tree whose subtrees dite in height by at most e. This means that height balaneend tes are as elose to full binary trosa psible even when the mber of veetie prceluds it. In order to being the height of a tree into halance we mst coaside two paatieular operations, left rotations, and riglit rotations. These are described pictotially below: Rotate Left tyL. tyR tyL Here, the wetex y beeomes the parent of wetex , the left child o y becomes the right ehild of z, and the parent of z beeomes the parent o y. Similarly eonsider the right rotation shown below Rotate Riglt ty txR txR ty The reetangles tr, ty,yL,tR,trL, trR are subtzees from the parent waties. These subtaes may be epty, the otatioas still work the same way. (a) Suppose a binary tree lis 100 wtiees, wlat is the maximum height of such a troe if it is to be eght balanced? (b) Now eoaside the mast unbalanced binay tree possible which hic 100 e ties. That is a single line starting with vertex ul where the paint of vm. How many rotations are eossary to height balance this tee?Explanation / Answer
(a) Given that the balanced binary tree has 100 nodes.
Suppose the function h(i) denotes the minimum number of nodes required to build a height balanced binary tree of height i.
h(0) = 1, h(1) = 2
h(k) = 1 + h(k-1) + h(k-2) for k>1
Thus we have
h(2) = 4
h(3) = 7
h(4) = 12
h(5) = 20
h(6) = 33
h(7) = 54
h(8) = 88
h(9) = 143
Since 88<100<143, maximum height achievable for a balanced binary tree with 100 nodes is 8.
(b) We are considering the line graph with 100 nodes. Its height is 99. We want to make it a height balanced binary tree.
From our previous question we know that such a tree can have left height 7 and right height 6 since h(7)=54 and h(6)=33.
To minimize the number of notations we keep 33 nodes on the right subtree and 66 nodes of the left subtree of the 100 node tree.
Let R(i) denote the number of rotations required for a line graph with i nodes. Then we have R(2)=0, R(3)=1, R(4)=1. We have
R(100) = 33 + R(33) + R(66)
= 33 + (12 + R(20) + R(12)) + (32+ R(33) + R(32))
= 77 + (4 + R(7) + R(4)) + (7 + R(12) + R(7)) + (12 + R(19) + R(12)) + (12 + R(20) + R(12))
= 112 + R(4) + 2R(7) + 3R(12) + R(19) + R(20)
= 112 + 1 + 2(2+R(2)+R(4)) + 3(4+R(7)+R(4)) + (7+R(11)+R(7)) + (7+R(12)+R(7))
= 133 + 2R(2) + 5R(4) + 5R(7) + R(11) + R(12)
= 138 + 5R(7) + (4+R(6)+R(4)) + (4+R(7)+R(4))
= 138 + 2R(4) + R(6) + 6R(7)
= 140 + (2+R(2)+R(3)) + 6(2+R(2)+R(4))
= 143 + 6(2+1)
= 143 + 18
= 161
Hence 161 rotations are required.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.