scema binary searc trees data structures and algoritms 05 0 lecture 7 VL-trees material scema binary searc tree binary tree: linked data structure wit nodes containing binary searc trees VL-trees material x.key from a totally ordered set x.left points to left cild of node x x.rigt points to rigt cild of nodex x.p points to parent of node x binary searc tree property: for every node x wit key k we ave: its left sub-tree contains only keys less tan (equal to) k its rigt sub-tree contains only keys greater tan (equal to) k
binary searc tree: example inorder traversal flattening te binary searc tree yields an ordered sequence 4 lgoritm inorder(v): if not v = nil ten inorder(v.left) print v.key inorder(v.rigt) inorder: time complexity questions we visit all nodes, so in Ω(n) recurrence equation wit k nodes in te left sub-tree we sow: T (n) (c + d) n + c T (0) = c T (n) = T (k) + T (n k ) + d wat are te binary searc trees wit keys,, give a min-eap tat is not a binary searc tree give a binary searc tree tat is not a min-eap tis yields: T (n) in O(n) ence T (n) in Θ(n)
searcing in a binary searc tree: example searcing in a binary searc tree input: node v and key k look for key 4 lgoritm treesearc(v, k): < > if v = null or k = v.key ten return v if k < v.key ten 4 return treesearc(v.left, k) else treesearc(v.rigt, k) alternative: use a wile-loop wat is te worst-case running time of treesearc? in O() binary searc tree: searc smallest key binary searc tree: searc largest key as far as possible to te left as far as possible to te rigt 4 4
minimum and maximum: pseudo-code question lgoritm treemimimum(x): wile x.left nil do x := x.left return x given a node ow to compute te node tat is visited next in inorder traversal? lgoritm treemaximum(x): wile x.rigt nil do x := x.rigt return x successor: pseudo-code adding: example input: node x lgoritm treesuccessor(x): if x.rigt nil ten add node wit key 5 < return treemimimum(x.rigt) y := x.p > wile y nil and x = y.rigt do x := y y := y.p 4 > 4 5 return y wat appens if x contains te largest key? wat is te worst-case running time of treesuccessor?
binary searc tree: adding example removal easy case lgoritm insert(t, z): y := nil x := T.root wile not x = null do y = x if z.key < x.key ten remove x := x.left else x := x.rigt 4 7 4 7 z.p := y if y = null ten T.root := z else if z.key < y.key ten y.left := z else y.rigt := z example removal difficult case removal remove 7 remove node z from binary searc tree T if z as at most cild ten transplant 4 7 4 if z as cildren ten take treeminimum of rigt subtree transplant tat one on te place of z
removal: pseudo-code and pseudo-code for transplant lgoritm treedelete(t, z): if z.left = nil ten transplant(t, z, z.rigt) else if z.rigt = nil ten transplant(t, z, z.left) else y := treeminimum(z.rigt) if y.p z ten transplant(t, y, y.rigt) y.rigt := z.rigt y.rigt.p := y transplant(t, z, y) y.left := z.left y.left.p := y lgoritm transplant(t, u, v): if u.p = nil ten T.root := v else if u = u.p.left ten u.p.left := v else u.p.rigt := v if v nil ten v.p := u.p binary searc tree binary searc trees: furter improvements operations for searcing, adding, deleting all in O(eigt) best case: eigt is in O(log n) worst case: eigt is in O(n) because te eigt is crucial for te time complexity of te operations tere are many subclasses of balanced binary searc tree compromise between te optimal and arbitrary binary searc tree expected case: eigt is in O(log n) (no proof)
scema VL-tree: definition binary searc trees VL-trees material binary searc tree wit in addition balance-property: for every node te absolute value of te difference between te eigt of te left sub-tree and te eigt of te rigt sub-tree is at most (take eigt for te empty tree) VL-tree: example LV-tree: eigt te key is in te node, te eigt of te sub-tree above te node 0 0 0 0 4 7 0 eigt of an VL-tree wit n nodes is in O(log n) (no proof)
operations on VL-trees te four cases of unbalanced nodes after insertion Left Left - Left Rigt - searc: exactly as for binary searc trees - worst-case time complexity is in O() so in O(log n) + + add: as for binary searc trees, and ten restore balance inserted Rigt Rigt inserted Rigt Left remove: as for binary searc trees, and ten restore balance - + + inserted inserted example: left-left adding yields left-left unbalanced node: left-left general left-left unbalanced node rebalance using single rotation: rebalance using single rotation: (compare (ab)c = a(bc))
symmetry: rigt-rigt example: left-rigt adding yields left-rigt unbalanced node rigt-rigt unbalanced node rebalance using single rotation rebalance using double rotation: left-rigt general left-rigt unbalanced node symmetry: rigt-left D rigt-left unbalanced node rebalance using double rotation rebalance using double rotation D
VL-trees: insertion question: add node wit key 0 step : insert new node as for binary searc tree step : identify te first unbalanced node on te pat from te new node to te root 5 0 step : rebalance tat node (tere are 4 possible cases) 4 7 we need at most rebalance (single or double rotation) step (wy?) insertion operation is in O(log n) te six cases of unbalanced nodes after removal example: left removal yields left unbalance Left Left - - + deleted Left Rigt - + deleted Left - 0 + + deleted 4 5 Rigt Rigt deleted + Rigt Left deleted + - Rigt deleted + 0 + rebalance using single rotation 4
symmetry: rigt VL-trees: removal symmetry gives unbalance rigt rebalance using single rotation step : remove node as for binary searc trees step: walk from te removal point upwards and rebalance te first unbalanced node ow to rotate? take te eaviest cild step : go back to step if necessary removal: remarks example: remove node wit key possible cases of unbalanced nodes after update we do not need more rules for rebalancing after removal we possibly need more tan one rebalance step removal is in O(log n) 4 5 7 0
overview for ordered dictionaries VL-trees: inventors searc insert remove log file O(n) O() O(n) lookup table O(log n) O(n) O(n) delson-velskii (-04) and Landis (-7) in binary searc tree O(n) O(n) O(n) VL-tree O(log n) O(log n) O(log n) scema material binary searc trees wiki binary searc tree VL-trees wiki VL-tree material
ow many binary trees wit n nodes exist? first atalan numbers: n = ( ) n n + n,,, 5, 4, 4,, 4, 40, 4, 7, 57, 00, 7400, 74440, 445, 55770, 4470, 477700, 770, 54040, 44700, 4540, 40550, 04474, 444045,...