overview binary search tree data structures and algorithms AVL-trees 2020 10 05 lecture 11 overview how many binary trees with n nodes exist? 1 � 2 n � C n = n + 1 n binary search tree first Catalan numbers: AVL-trees 1 , 1 , 2 , 5 , 14 , 42 , 132 , 429 , 1430 , 4862 , 16796 , 58786 , 208012 , 742900 , 2674440 , 9694845 , 35357670 , 129644790 , 477638700 , 1767263190 , 6564120420 , 24466267020 , 91482563640 , 343059613650 , 1289904147324 , 4861946401452 , . . .
binary search trees: recall search binary search tree: worst-case time complexity input: node v in binary search tree, and key k operations for searching, adding, deleting all in O ( height ) Algorithm treeSearch( v , k ): best case: height is in O (log n ) if v = nil or k = v . key then return v worst case: height is in O ( n ) if k < v . key then expected case: height is in O (log n ) (no proof) return treeSearch( v . left , k ) else return treeSearch( v . right , k ) see also the iterative version; know pseudo-codes binary search trees: further improvements overview because the height is crucial for the time complexity of the operations binary search tree there are many subclasses of balanced binary search tree AVL-trees AVL trees, red-black trees, splay trees compromise between the optimal and arbitrary binary search tree
AVL-tree: definition AVL-tree: example height of a node: maximal length of a path to a leaf the key is in the node, the height of the sub-tree above the node height of a tree is height of the root is maximal length of a path to a leaf 3 binary search tree with in addition balance-property: 3 1 2 for every node 0 8 0 1 0 the absolute value of the difference between 2 6 9 the height of the left sub-tree 0 0 and the height of the right sub-tree 4 7 is at most 1 (take height − 1 for the empty tree) ALV-tree: height operations on AVL-trees min, max: algorithm as for BSTs; worst-case time complexity in O ( h ) so in O (log n ) successor, predecessor: algorithm as for BSTs; height of an AVL-tree with n nodes is in O (log n ) worst-case time complexity in O ( h ) so in O (log n ) (no proof) search: algorithm as for BSTs; worst-case time complexity in O ( h ) so in O (log n ) insert, delete: first step as algorithms for BSTs; balance may be destroyed so we need to do more !
binary search trees: recall insert example: insertion with left-left imbalance adding yields left-left unbalanced node: Algorithm insert( T , z ): y := nil 3 x := T . root while not x = nil do 2 y = x if z . key < x . key then 1 x := x . left else rebalance using single rotation of nodes with labels 3 and 2: x := x . right z . p := y 2 if y = nil then T . root := z 3 1 else if z . key < y . key then y . left := z here h = − 1, and x is node with label 2, and y is node with label 3 else y . right := z insertion with left-left unbalance insertion with left-left unbalance so suppose insertion takes place in subtree A suppose node y is the lowest node that becomes unbalanced height of x increases from h + 1 to h + 2 before insertion, so height of A increases from h to h + 1 its left subtree has height h + 1 and its right subtree ( C ) height h x balanced after insertion so height of B is not h − 1 after insertion, its left subtree has height h + 2 and its right subtree has height h height x increases by insertion so height of B is not h + 1 so left subtree is not empty; so height of B is h say it is node x with left subtree A and right subtree B recall: the height of C is h suppose insertion takes place in subtree A : case left-left rebalance by single rotation
left-left rebalancing: correctness left-left rebalancing general left-left unbalanced node: before insertion, the top node y in our analysis has height h + 2 C after rotation, the top node x in our analysis has height h + 2 B so no further actions are needed for rebalancing A insertion is correct for BST trees (from BST theory) rebalance using single rotation (compare ( ab ) c = a ( bc )): rotation preserves inorder; informally, A ≤ x ≤ B ≤ y ≤ C A C B example insertion with left-right inbalance insertion with left-right unbalance adding yields left-right unbalanced node suppose node y is the lowest node that becomes unbalanced 3 before insertion, its left subtree has height h + 1 and its right subtree ( C ) height h 1 after insertion, 2 its left subtree has height h + 2 and its right subtree has height h rebalance using double rotation so left subtree is not empty; say it is node x with left subtree A and right subtree B 2 now suppose the insertion was in subtree B : case left-right 1 3 here h = − 1, x is node with key 1 and y is node with key 3
insertion with left-right unbalance left-right rebalancing: correctness height of x increases from h + 1 to h + 2 before insertion, the top node y in our analysis has height h + 2 so height of B increases from h to h + 1 after rotation, the top node z in our analysis has height h + 2 x balanced after insertion so height of A is not h − 1 so no further actions are needed for rebalancing height x increases by insertion so height of A is not h + 1 insertion is correct for BST trees (from BST theory) so height of A is h rotation preserves inorder; recall: the height of C is h informally, A ≤ x ≤ B 1 ≤ z ≤ B 2 ≤ y ≤ C rebalance by double rotation left-right general symmetric cases left-right unbalanced node we have in total four cases for unbalance caused by insertion D left-left and right-right A C left-right and right-left B rebalance using double rotation (cf.( a ( bc ) d ) = ( ab )( cd )) and that is all: in total four cases of imbalance due to insertion A B C D
the four cases of unbalanced nodes after insertion AVL-trees: insertion summary Left Right Left Left -2 -2 first, insert new node using algorithm for BST -1 1 C C second, identify the lowest unbalanced node A B h A B h on the path from the new node to the root h + 1 h + 1 h h third, rebalance that node using a single or double rotation inserted inserted Right Right Right Left 2 2 then balance is restored 1 -1 A A we need at most 1 rebalance step h B C h B C h + 1 h + 1 h h insertion operation is in O (log n ) inserted inserted question: add node with key 0 binary search tree: recall deletion Algorithm treeDelete( T , z ): if z . left = nil then transplant( T , z , z . right ) 8 else if z . right = nil then 5 10 transplant( T , z , z . left ) 3 6 9 12 else y := treeMinimum( z . right ) 2 4 7 11 if y . p � = z then 1 transplant( T , y , y . right ) y . right := z . right y . right . p := y transplant( T , z , y ) y . left := z . left y . left . p := y
and pseudo-code for transplant removal of node with key k for BST remove as for binary search tree Algorithm transplant( T , u , v ): if u . p = nil then restore balance T . root := v one rebalancing step is not necessarily sufficient else if u = u . p . left then u . p . left := v we have 2 (symmetric) additional cases else u . p . right := v we do not need additional rules if v � = nil then v . p := u . p revisit case left-left for removal example new case: imbalance left 4 2 5 removal in subtree C can cause imbalance 1 because height of C decreases from h + 1 to h 3 single rotation for case left-left does not necessarily restore balance rebalance using single rotation similarly: double rotation for case left-right does not necessarily restore 2 balance 1 4 3
the six cases of unbalanced nodes after removal AVL-trees: removal first, remove node using the algorithm for BST Left Right Left Left -2 -2 Left -2 second, consider the first unbalanced node -1 1 C C 0 C on the path from the removed node upwards A B h A B h A B h deleted deleted h + 1 h h h + 1 deleted h + 1 h + 1 third, rebalance by considering the ‘heaviest child’ of the unbalanced node Right Right Right Left 2 2 if necessary, consider the next unbalanced node and iterate rebalancing Right 2 1 -1 0 A A 6 possible cases of unbalanced nodes after update A h B C h B C h B C deleted deleted we do not need more rules for rebalancing h + 1 h + 1 h h deleted h + 1 h + 1 removal is in O (log n ) example: remove node with key 9 time complexity of updating operations for AVL-trees 8 worst case time complexity of searching, adding, deleting is in O (log n ) with n the number of nodes 5 10 3 6 9 12 single and double rotations are in O (1) 2 4 7 11 1
overview worst-case time complexities for ordered AVL-trees: inventors dictionaries search insert remove log file O ( n ) O (1) O ( n ) Adelson-Velskii (1922-2014) and Landis (1921-1997) in 1962 lookup table O (log n ) O ( n ) O ( n ) binary search tree O ( n ) O ( n ) O ( n ) AVL-tree O (log n ) O (log n ) O (log n ) material background material on AVL trees: paper by Frank Pfenning additional material: wiki tree rotations
Recommend
More recommend