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
CSED233: Data Structures (2014F) Lecture13: Tree III Bohyung Han CSE, POSTECH [email protected] 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 1 2 4 2 1 3 6 3 5 4 7 5 6 7 2 CSED233: Data Structures by Prof. Bohyung Han, Fall 2014 Complexity of Operations • Lookup Time Balanced tree: 𝑂(log 𝑛) Unbalanced tree: 𝑂(𝑛) • Insertion time Balanced tree: 𝑂(log 𝑛) Unbalanced tree: 𝑂(𝑛) • Balance does matter for efficient trees. 3 CSED233: Data Structures by Prof. Bohyung Han, Fall 2014 Self‐balancing Trees • Variants of binary search tree • Self‐balancing Additional rules for how to structure tree Needs operations to move nodes around • Constraints on height differences of leaves • Ensures that tree does not degenerate to list. 4 CSED233: Data Structures by Prof. Bohyung Han, Fall 2014 AVL Tree • What is AVL 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 𝑛) 44 2 4 78 17 1 3 2 32 1 50 1 48 88 62 1 AVL tree 5 CSED233: Data Structures by Prof. Bohyung Han, Fall 2014 Insertion • Insertion is as in a binary search tree. 44 2 44 4 78 17 1 3 2 32 48 1 50 1 1 4 3 32 88 62 78 17 1 1 50 1 2 5 88 48 62 2 1 54 Before insertion After insertion 6 CSED233: Data Structures by Prof. Bohyung Han, Fall 2014 Insertion • Tree height after insertion 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 𝑧 7 44 2 5 78 17 1 4 c=z 3 32 1 50 88 a=y 1 48 62 2 b=x 1 54 CSED233: Data Structures by Prof. Bohyung Han, Fall 2014 Restructuring • Single Rotation: h-1 h h-3 a=z h-2 b=y h-3 T0 h-2 h-1 single rotation h-3 c=x T1 T3 T2 c=z b=y a=z c=x h-3 T0 single rotation b=y h-2 T1 T3 T2 b=y a=x c=z a=x T0 T1 T2 T3 T3 8 T2 T1 T0 CSED233: Data Structures by Prof. Bohyung Han, Fall 2014 Restructuring • Double rotations: h h-3 h-1 h-1 a=z double rotation h-2 c = y h-3 ≤h-3 b = x ≤h-3 T0 T1 h-2 b=x a=z h-3 T3 T2 h-2 T0 a=y T2 T1 double rotation c=z c = y h-3 ≤h-3 T3 b=x a=y c=z b=x T3 T2 T0 T3 T2 T1 T0 T1 9 CSED233: Data Structures by Prof. Bohyung Han, Fall 2014 Insertion Example 44 2 5 44 78 17 1 4 c=z 3 32 2 78 17 1 1 50 48 62 c=z 1 62 88 b=x a=y 1 4 3 32 88 5 2 2 b=x 50 a=y 1 1 48 54 54 1 After the first rotation After insertion unbalanced 10 CSED233: Data Structures by Prof. Bohyung Han, Fall 2014 Insertion Example 44 2 5 78 17 1 4 c=z 3 32 44 2 1 62 88 62 17 1 b=x 2 48 54 b=x 2 50 78 a=y a=y 1 3 2 32 50 4 1 1 48 54 c=z 1 1 88 After the second rotation After the first rotation 11 CSED233: Data Structures by Prof. Bohyung Han, Fall 2014 Removal 44 2 4 44 62 17 1 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 2014 Reconstructing after a Removal • Node definition 𝑧: 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 62 w 3 y 2 2 50 1 48 13 78 54 x 1 1 88 CSED233: Data Structures by Prof. Bohyung Han, Fall 2014 Reconstructing after a Removal • 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. 44 1 17 4 z 62 62 w 3 2 2 50 1 48 3 y 78 54 1 x 1 1 17 44 88 14 y 78 z w 1 4 50 48 2 x 1 2 88 54 1 CSED233: Data Structures by Prof. Bohyung Han, Fall 2014 AVL Tree Performance • Restructuring A single restructure takes 𝑂(1) time. Using a linked‐structure binary tree • Search A search takes 𝑂(log 𝑛) time. The height of tree is 𝑂(log 𝑛), no restructuring is needed. • Insertion An insertion takes 𝑂(log 𝑛) time. Restructuring up the tree, maintaining heights is 𝑂(log 𝑛). • Deletion A deletion takes 𝑂(log 𝑛) time. Restructuring up the tree, maintaining heights is 𝑂(log 𝑛). 15 CSED233: Data Structures by Prof. Bohyung Han, Fall 2014 16