alanced inar Search Trees Lecture Overview The importance of being balanced VL trees Definition alance Insert Other balanced trees Data structures in general Readings LRS hapter. and. (but different approach: red-blac trees) Recall: inar Search Trees (STs) rooted binar tree each node has e left pointer right pointer parent pointer See Fig. 4 0 9 50 6 Figure : Heights of nodes in a ST
Figure : ST propert ST propert (see Fig. ). height of node = length ( edges) of longest downward path to a leaf (see LRS.5 for details). The Importance of eing alanced: STs support insert, min, delete, ran, etc. in O(h) time, where h = height of tree (= height of root). h is between lg(n) and n: Fig. ). vs. Perfectl alanced Path Figure : alancing STs balanced ST maintains h = O(lg n) all operations run in O(lg n) time.
Lecture 4 alanced inar Search Trees 6.006 Spring 008 VL Trees: Definition VL trees are self-balancing binar search trees. These trees are named after their two inventors G.M. del son-vel sii and E.M. Landis n VL tree is one that requires heights of left and right children of ever node to differ b at most ±. This is illustrated in Fig. 4) - Figure 4: VL Tree oncept In order to implement an VL tree, follow two critical steps: Treat nil tree as height. Each node stores its height. This is inherentl a DT STRUTURE UGMENTTION procedure, similar to augmenting subtree size. lternativel, one can just store difference in heights. good animation applet for VL trees is available at this lin. To compare inar Search Trees and VL balancing of trees use code provided here. Original Russian article: delson-velsii, G.; E. M. Landis (96). n algorithm for the organization of information. Proceedings of the USSR cadem of Sciences 46: 666. (English translation b Mron J. Ricci in Soviet Math. Dolad, :596, 96.)
Lecture 4 alanced inar Search Trees 6.006 Spring 008 alance: The balance is the worst when ever node differs b. Let N h = min ( nodes). N h = N h + N h + lternativel: > N h N h > h/ = h < lg h N h > F n (n th Fibonacci number) In fact,n h = F n+ (simple induction) h F h = 5 (rounded to nearest integer) + 5 where =.68 = mah log (n).440 lg(n) (golden ratio) VL Insert:. insert as in simple ST. wor our wa up tree, restoring VL propert (and updating heights as ou go). Each Step: suppose is lowest node violating VL assume is right-heav (left case smmetric) if s right child is right-heav or balanced: follow steps in Fig. 5 else follow steps in Fig. 6 then continue up to s grandparent, greatgrandparent... 4
Lecture 4 alanced inar Search Trees 6.006 Spring 008 - + Left-Rotate() - - - - z + Left-Rotate() + - Figure 5: VL Insert alancing + Right-Rotate(z) - z + Left-Rotate() z D - - - or - D - - or - Figure 6: VL Insert alancing 5
Lecture 4 alanced inar Search Trees 6.006 Spring 008 Eample: n eample implementation of the VL Insert process is illustrated in Fig. 7 Insert() 4 = 9: left-left case 4 0 0 9 50 50 9 6 Done 4 6 Insert(55) 4 0 0 50 6 50 6 9 9 =: left-right case 4 Done 4 0 0 55 6 9 50 55 50 6 9 Figure 7: Illustration of VL Tree Insert Process omment. In general, process ma need several rotations before an Insert is completed. omment. Delete(-min) harder but possible. 6
Lecture 4 alanced inar Search Trees 6.006 Spring 008 alanced Search Trees: There are man balanced search trees. VL Trees del son-velsii and Landis 96 -Trees/--4 Trees aer and Mcreight 97 (see LRS 8) [α] Trees Nievergelt and Reingold 97 Red-blac Trees LRS hapter Spla-Trees Sleator and Tarjan 985 Sip Lists Pugh 989 Scapegoat Trees Galperin and Rivest 99 Treaps Seidel and ragon 996 Note. Sip Lists and Treaps use random numbers to mae decisions fast with high probabilit. Note. Spla Trees and Scapegoat Trees are amortized : adding up costs for several operations = fast on average. 7
Lecture 4 alanced inar Search Trees 6.006 Spring 008 Spla Trees Upon access (search or insert), move node to root b sequence of rotations and/or doublerotations (just lie VL trees). Height can be linear but still O(lg n) per operation on average (amortized) Note: We will see more on amortization in a couple of lectures. Optimalit For STs, cannot do better than O(lg n) per search in worst case. In some cases, can do better e.g. in-order traversal taes Θ(n) time for n elements. put more frequent items near root onjecture: Spla trees are O(best ST) for ever access pattern. With fancier trics, can achieve O(lg lg u) performance for integers u [Van Ernde oas; see 6.854 or 6.85 (dvanced Data Structures)] ig Picture: bstract Data Tpe(DT): interface spec. e.g. Priorit Queue: Q = new-empt-queue() Q.insert() = Q.deletemin() vs. Data Structure (DS): algorithm for each op. There are man possible DSs for one DT. One eample that we will discuss much later in the course is the heap priorit queue. 8