CSE : Data Structures AL Trees Neva Cernavsy Summer Te AL Balance Condton AL balance property: Left and rgt subtrees of every node ave egts dfferng by at most Ensures small dept ll prove ts by sowng tat an AL tree of egt must ave a lot of (.e. O( )) nodes Easy to mantan Usng sngle and double rotatons Te AL Tree Data Structure Structural propertes. Bnary tree property (,, or cldren). Hegts of left and rgt subtrees of every node dffer by at most Result: orst case dept of any node s: O(log n) Is ts an AL Tree? How do we trac te balance? How do we detect mbalance? How do we restore balance? Orderng property Same as for BST Crcle One: AL Hegt of an AL Tree Not AL AL Not AL M() = mnmum number of nodes n an AL tree of egt. Bass M() =, M() = Inducton M() = M(-) + M(-) + Soluton - - M() > φ - (φ = (+ )/.) Student Actvty If not AL, put a box around nodes were AL property s volated.
Proof tat M() > φ Bass: M() = > φ -, M() = > φ - Inducton step. M() = M(-) + M(-) + > (φ - - ) + (φ - - ) + = φ - (φ +) - = φ - (φ = φ +) Hegt of an AL Tree M() > φ (φ.) Suppose we ave N nodes n an AL tree of egt. N > M() N > φ - log φ (N+) > (relatvely well balanced tree!!) Node Hegts Node Hegts after Insert egt of node = balance factor = left - rgt empty egt = - balance factor -(-) = - egt of node = balance factor = left - rgt empty egt = - Insert and Rotaton n AL Trees Insert operaton may cause balance factor to become or for some node only nodes on te pat from nserton pont to root node ave possbly canged n egt So after te Insert, go bac up to te root node by node, updatng egts If a new balance factor (te dfference left - rgt ) s or, adust tree by rotaton around te node Sngle Rotaton n an AL Tree
Insertons n AL Trees Bad Case # Let te node tat needs rebalancng be α. Tere are cases: Outsde Cases (requre sngle rotaton) :. Inserton nto left subtree of left cld of α.. Inserton nto rgt subtree of rgt cld of α. Insde Cases (requre double rotaton) :. Inserton nto rgt subtree of left cld of α.. Inserton nto left subtree of rgt cld of α. Te rebalancng s performed troug four separate rotaton algortms. Insert() Insert() Insert() Fx: Apply Sngle Rotaton AL Property volated at ts node (x) Sngle Rotaton:. Rotate between x and cld AL Inserton: Outsde Case Consder a vald AL subtree AL Inserton: Outsde Case + Insertng nto destroys te AL property at node AL Inserton: Outsde Case + Do a rotaton from left
Sngle rotaton from left + Outsde Case Completed + rotaton from left done! ( rotaton from rgt s mrror symmetrc) AL property as been restored! Sngle rotaton example Insert() Insert() Insert() Bad Case # Fx: Apply Double Rotaton AL Property volated at ts node (x) Double Rotaton. Rotate between x s cld and grandcld. Rotate between x and x s new cld AL Inserton: Insde Case Consder a vald AL subtree
AL Inserton: Insde Case Insertng nto destroys te AL property at node + Does rotaton from left restore balance? AL Inserton: Insde Case + Rotaton from left does not restore balance now s out of balance AL Inserton: Insde Case Consder te structure of subtree + AL Inserton: Insde Case = node and subtrees and or - + AL Inserton: Insde Case e wll do a double rotaton... Double rotaton : frst rotaton
Double rotaton : second rotaton Double rotaton : second rotaton rgt rotaton complete or - Balance as been restored to te unverse Double rotaton, step Double rotaton, step Imbalance at node Insert nto an AL tree: a b e c d Sngle Rotaton. Rotate between x and cld Double Rotaton. Rotate between x s cld and grandcld. Rotate between x and x s new cld Student Actvty
Sngle and Double Rotatons: Insertng wat nteger values would cause te tree to need a:. sngle rotaton?. double rotaton? Inserton nto AL tree. Fnd spot for new ey. Hang new node tere wt ts ey. Searc bac up te pat for mbalance. If tere s an mbalance: case #: Perform sngle rotaton and ext. no rotaton? Student Actvty case #: Perform double rotaton and ext Bot rotatons eep te subtree egt uncanged. Hence only one rotaton s suffcent! Insert() Unbalanced? Easy Insert Hard Insert (Bad Case #) Insert() Unbalanced? How to fx? Sngle Rotaton Hard Insert (Bad Case #) Insert() Unbalanced? How to fx?
Sngle Rotaton (oops!) Double Rotaton (Step #) Double Rotaton (Step #) AL Trees Revsted Balance condton: For every node x, - balance(x) Strong enoug : orst case dept s O(log n) Easy to mantan : one sngle or double rotaton Guaranteed O(log n) runnng tme for Fnd? Insert? Delete? buldtree? AL Trees Revsted at extra nfo dd we mantan n eac node? ere were rotatons performed? How dd we locate ts node? Oter Possbltes? Could use dfferent balance condtons, dfferent ways to mantan balance, dfferent guarantees on runnng tme, y aren t AL trees perfect? Many oter balanced BST data structures Red-Blac trees AA trees Splay Trees - Trees B-Trees
Implementaton Sngle Rotaton left balance (,,-) ey rgt RotateFromRgt(n : reference node ponter) { p : node ponter; p := n.rgt; n n.rgt := p.left; p.left := n; n := p } Double Rotaton Class partcpaton Implement Double Rotaton n two lnes. DoubleRotateFromRgt(n : reference node ponter) {???? n } AL Tree Deleton Smlar to nserton Rotatons and double rotatons needed to rebalance Imbalance may propagate upward so tat many rotatons may be needed. Pros and Cons of AL Trees Arguments for AL trees:. Searc s O(log N) snce AL trees are always well balanced.. Te egt balancng adds no more tan a constant factor to te speed of nserton, deleton, and fnd. Arguments aganst usng AL trees:. Dffcult to program & debug; more space for egt nfo.. Asymptotcally faster but rebalancng costs tme.. Most large searces are done n database systems on ds and use oter structures (e.g. B-trees).. May be OK to ave O(N) for a sngle operaton f total run tme for many consecutve operatons s fast (e.g. Splay trees).