200 likes | 221 Views
6. v. 8. 3. z. 4. AVL Trees ( 高度平衡樹 ). AVL Tree Definition. An AVL Tree is a binary search tree such that for every internal node v of T, the heights of the children of v can differ by at most 1 Proposed by G. M. Adelson-Velsky & E. M. Landisin 1962. Height. Quiz!.
E N D
6 v 8 3 z 4 AVL Trees(高度平衡樹) AVL Trees
AVL Tree Definition • An AVL Tree is a binary search tree such that for every internal node v of T, the heights of the children of v can differ by at most 1 • Proposed by G. M. Adelson-Velsky& E. M. Landisin 1962. Height Quiz! Example of AVL tree AVL Trees
n(2) 3 n(1) 4 Height of an AVL Tree • Fact: The height of an AVL tree storing n keys is O(log n). • Proof: • Let us bound n(h), the minimum number of internal nodes of an AVL tree of height h. • Base cases: n(1) = 1 and n(2) = 2 • Recurrent formula: For n > 2, an AVL tree of height h contains the root node, one AVL subtree of height n-1 and another of height n-2 n(h) = 1 + n(h-1) + n(h-2) • Knowing n(h-1) > n(h-2), we get n(h) > 2n(h-2). So n(h) > 2 n(h-2) > 22 n(h-4) > 23 n(h-6) > … > 2i n(h-2i) n(h) > 2 h/2-1 h < 2log n(h) +2 • Thus the height of an AVL tree is O(log n) More details in textbook Quiz: What is the analytic formula for n(h)? AVL Trees
44 17 78 44 32 50 88 17 78 48 62 32 50 88 54 48 62 Insertion • Insertion is as in a binary search tree • Example: c=z a=y Insert 54 b=x w Newly added before insertion after insertion AVL Trees
T T 1 1 Insertion Example, continued unbalanced... 4 44 Newly added x 3 2 17 62 z y 2 1 2 78 32 50 1 1 1 ...balanced 54 88 48 Via double rotation T 2 T T AVL Trees 0 3
Single Rotations • Single Rotations: RR Imbalance a b single rotation b a c c T T 0 3 T T T T T 1 3 0 1 2 T 2 LL Imbalance c b single rotation b a c a T T 3 0 T T T T T 0 2 1 2 3 AVL Trees T 1
Double Rotations • double rotations: RL Imbalance double rotation a b c a c b T T 0 2 T T T T T 3 0 1 3 2 T LR Imbalance 1 double rotation c b a a c b T T 0 2 T T T T T 3 3 1 0 2 T 1 AVL Trees
Recap on Insert • From the inserted node, you need to find the first node x leading to the root that has AVL violation. • Perform adjustment only once to restore all AVL order leading to the root • Two types of adjustments • RR or LL imbalance Single rotation • RL or LR imbalance Double rotations AVL Trees
Example of Single Rotations Quiz! • Insert 1, 2, 3, 4, 5, 6, 7 into an AVL tree. AVL Trees
Example of Single and Double Rotations Quiz! • Insert 1, 3, 4, 15, 14, 12, 2 into an empty AVL tree. AVL Trees
Exercise Quiz! • Insert 12, 8, 7, 14, 18, 10, 20, 16, 15 into an empty AVL tree. AVL Trees
44 17 62 32 50 78 88 48 54 Delete • Delete begins as in a binary search tree, which means the node removed will become an empty external node. Its parent, w, may cause an imbalance. • Example: 44 17 62 Delete 32 50 78 88 48 54 before deletion of 32 after deletion AVL Trees
Rebalancing after a Delete (1/2) • Let z be the first unbalanced node encountered while travelling up the tree from w. Also, let y be the child of z with the larger height, and let x be the child of y with the larger height • If x has RR imbalance, we perform adjustment of single rotation to restore balance at z • As this restructuring may upset the balance of another node higher in the tree, we must continue checking for balance until the root of T is reached 62 z 44 Single Rotation! 44 78 w y 17 62 17 50 88 x 50 78 48 54 88 48 54 AVL Trees
Rebalancing after a Delete (2/2) • If we have a tie and x is chosen to have RL imbalance • We perform adjustment of double rotations (which is unnecessary) to restore balance at z 50 (Unnecessary) double Rotation! z 44 62 44 w y 17 62 48 78 54 17 50 78 x 88 88 48 54 AVL Trees
Example of Complex Deletes (1/2) • Delete that involves two adjustments Quiz! 9 5 20 15 4 7 25 12 6 18 30 Delete! 10 AVL Trees
Example of Complex Deletes (2/2) • Delete that involves two adjustments Quiz! Delete! 9 5 20 15 4 7 25 12 6 30 22 29 AVL Trees
Example of More Deletes Quiz! • Create a minimum-node AVL tree that, when a node is deleted, it involves three adjustments to balance the tree. Deleting node 4 involves 2 restructures! 9 5 20 15 4 7 25 12 6 18 30 AVL Trees 10
Recap on Deletes Quiz! • Comparison • Delete: Check imbalance all the way to the root(and perform adjustments accordingly) • Insert: Perform adjustment only once. • To make the delete sequence generate the same tree each time: • Use the in-order successor (if the node has both subtrees) to replace the deleted node. • Use single rotation whenever possible. Please follow this rule in you quiz to make the answer unique! AVL Trees
Youtube Links for AVL Trees • Intro to AVL tree • MIT open course ware (For inserts only. The lecturer mistakenly said that you need to check all the way to the root…) • Tree growing • A detailed example from SDSU: 43, 18, 22, 9, 21, 6, 8, 20, 63, 50, 62, 51. • Tree shrinking • A simple example • Another example • Yet another example AVL Trees
AVL Tree Performance • A single adjustment takes O(1) time • Using a linked-structure binary tree • find takes O(log n) time • Height of tree is O(log n), no restructures needed • put takes O(log n) time • Initial find is O(log n) • Adjusting up the tree, maintaining heights is O(log n) • erase takes O(log n) time • Initial find is O(log n) • Adjusting up the tree, maintaining heights is O(log n) AVL Trees