Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
Insertion Order matters • Difference between these binary search trees? Insert: 4, 2, 1, 3, 6, 5, 7 Insert: 1, 2, 3, 4, 5, 6, 7 CSED233: Data Structures (2015F) Lecture13: Tree III 1 2 4 2 Bohyung Han CSE, POSTECH [email protected] 1 3 6 3 5 4 7 5 6 7 2 Complexity of Operations CSED233: Data Structures by Prof. Bohyung Han, Fall 2015 Self‐balancing Trees • Lookup Time • Variants of binary search tree • Self‐balancing Balanced tree: log Unbalanced tree: Additional rules for how to structure tree Needs operations to move nodes around • Insertion time Balanced tree: log Unbalanced tree: • Constraints on height differences of leaves • Ensures that tree does not degenerate to list. • Balance does matter for efficient trees. 3 CSED233: Data Structures by Prof. Bohyung Han, Fall 2015 4 CSED233: Data Structures by Prof. Bohyung Han, Fall 2015 AVL Tree Insertion • What is AVL tree? • Insertion is as in a binary search tree. A self‐balancing binary search tree Adelson‐Velskii and Landis' tree, named after the inventors For every node of tree , the heights of the children of differ by at most 1. Height: log 4 44 2 78 17 1 44 3 2 2 32 1 50 78 17 1 88 1 48 62 2 3 2 32 78 17 1 1 50 88 48 62 1 50 1 1 4 3 32 1 1 5 44 4 88 48 AVL tree 62 2 1 54 Before insertion After insertion 5 6 CSED233: Data Structures by Prof. Bohyung Han, Fall 2015 Insertion CSED233: Data Structures by Prof. Bohyung Han, Fall 2015 Restructuring • Tree height after insertion • Single Rotation: All nodes along the path increase their height by 1. It may violate the AVL tree property. • We rebalance the subtree of using a restructuring method. : the first violation node from the bottom along the path : ’s child with the higher height : ’s child with the higher height , , : inorder traversal of node , , and 44 2 5 h‐3 78 17 1 4 1 50 88 a=z 48 62 h‐2 b=y h‐2 b=y single rotation a=z h‐3 c=x c=x h‐3 T0 T3 T1 a=y 1 h‐2 h‐1 h‐3 c=z 3 32 h‐1 h T3 T0 T1 T2 T2 2 b=x c=z 1 54 b=y single rotation b=y a=x c=z a=x T3 T0 T3 T2 T2 T1 T0 T1 7 CSED233: Data Structures by Prof. Bohyung Han, Fall 2015 8 CSED233: Data Structures by Prof. Bohyung Han, Fall 2015 Restructuring Insertion Example • Double rotations: h h‐1 h‐1 h‐3 double rotation a=z h‐2 c = y h‐3 ≤h‐3 b = x ≤h‐3 T0 T0 78 T3 48 b=x a=y a=y 1 T0 T2 T3 T1 1 62 2 2 b=x 88 a=y 54 48 1 After the first rotation After insertion unbalanced T0 50 1 54 T2 c=z b=x 1 c=z 4 3 32 88 62 b=x T3 78 17 1 50 1 c=z 2 c=z a=y T1 double rotation 4 3 32 5 44 17 1 T2 T1 2 c = y h‐3 ≤h‐3 5 44 h‐2 b=x a=z h‐3 T3 T2 h‐2 T1 9 10 CSED233: Data Structures by Prof. Bohyung Han, Fall 2015 Insertion Example 44 2 78 4 44 c=z 3 32 2 1 62 88 1 48 54 3 2 78 1 48 54 1 1 88 After the second rotation After the first rotation 11 CSED233: Data Structures by Prof. Bohyung Han, Fall 2015 44 62 1 c=z 4 17 2 50 1 2 b=x a=y a=y 1 44 62 32 50 4 17 b=x 2 Removal 5 17 1 CSED233: Data Structures by Prof. Bohyung Han, Fall 2015 3 2 32 48 62 17 2 50 78 54 3 2 2 50 1 1 4 1 1 1 88 Before deletion of 32 48 78 54 1 1 88 After deletion of 32 12 CSED233: Data Structures by Prof. Bohyung Han, Fall 2015 Reconstructing after a Removal Reconstructing after a Removal • Node definition • Recursive reconstructing Restructuring may upset the balance of another node higher in the tree. We must continue checking for balance until the root is reached. : the first unbalanced node encountered while travelling up the tree from the violating node : the child of with the larger height : the child of with the larger height 4 44 1 17 z 1 62 w 3 17 48 62 3 2 50 x 1 1 1 48 3 y 2 78 54 z 62 2 50 4 w y 2 1 44 78 54 1 x 1 1 17 44 z w 50 1 88 48 4 y 78 2 x 1 2 88 54 1 88 13 CSED233: Data Structures by Prof. Bohyung Han, Fall 2015 14 CSED233: Data Structures by Prof. Bohyung Han, Fall 2015 AVL Tree Performance • Restructuring A single restructure takes 1 time. Using a linked‐structure binary tree • Search A search takes log The height of tree is time. log , no restructuring is needed. • Insertion An insertion takes log time. Restructuring up the tree, maintaining heights is log . log . • Deletion A deletion takes log time. Restructuring up the tree, maintaining heights is 15 CSED233: Data Structures by Prof. Bohyung Han, Fall 2015 16