avl trees
play

AVL Trees All keys in left subtree smaller than nodes key 2 6 - PDF document

1/21/2016 Review: Binary Search Tree (BST) Structure property (binary tree) Each node has 2 children 8 Result: keeps operations simple CSE373: Data Structures and Algorithms Order property 5 11 AVL Trees All keys in


  1. 1/21/2016 Review: Binary Search Tree (BST) • Structure property (binary tree) – Each node has  2 children 8 – Result: keeps operations simple CSE373: Data Structures and Algorithms • Order property 5 11 AVL Trees – All keys in left subtree smaller than node’s key 2 6 10 12 – All keys in right subtree larger than node’s key Steve Tanimoto – Result: easy to find any given key 4 7 9 14 Winter 2016 13 This lecture material represents the work of multiple instructors at the University of Washington. Thank you to all who have contributed! Winter 2016 CSE373: Data Structures & Algorithms 2 BST: Efficiency of Operations? How can we make a BST efficient? Observation • Problem: operations may be inefficient if BST is • BST: the shallower the better! unbalanced. Solution : Require and maintain a Balance Condition that 1. Ensures depth is always O ( log n ) – strong enough! • Find, insert, delete 2. Is efficient to maintain – not too strong! – O(n) in the worst case • When we build the tree, make sure it’s balanced. • BuildTree • BUT…Balancing a tree only at build time is insufficient because – O(n 2 ) in the worst case sequences of operations can eventually transform our carefully balanced tree into the dreaded list  • So, we also need to also keep the tree balanced as we perform operations. Winter 2016 CSE373: Data Structures & Algorithms 3 Winter 2016 CSE373: Data Structures & Algorithms 4 Potential Balance Conditions Potential Balance Conditions 1. Left and right subtrees of the root 3. Left and right subtrees of every node have equal number of nodes have equal number of nodes Too weak! Too strong! Only perfect trees (2 n – 1 nodes) Height mismatch example: 2. Left and right subtrees of the root 4. Left and right subtrees of every node have equal height have equal height Too weak! Too strong! Only perfect trees (2 n – 1 nodes) Double chain example: Winter 2016 CSE373: Data Structures & Algorithms 5 Winter 2016 CSE373: Data Structures & Algorithms 6 1

  2. 1/21/2016 The AVL Tree Data Structure The AVL Balance Condition Left and right subtrees of every node have heights differing by at most 1 An AVL tree is a self-balancing binary search tree. Definition : balance ( node ) = height( node .left) – height( node .right) Structural properties 1. Binary tree property (same as BST) AVL property : for every node x, –1  balance( x )  1 2. Order property (same as for BST) • Ensures small depth 1. Balance property: balance of every node is between -1 and 1 – Will prove this by showing that an AVL tree of height h must have a number of nodes exponential in h (i.e. height must be logarithmic in number of nodes) Result: Worst-case depth is O(log n ) • Efficient to maintain • Named after inventors Adelson-Velskii and Landis (AVL) – Using single and double rotations – First invented in 1962 7 Winter 2016 CSE373: Data Structures & Algorithms Winter 2016 CSE373: Data Structures & Algorithms 8 Is this an AVL tree? Is this an AVL tree? 4 6 3 6 3 4 8 1 4 1 8 2 2 1 5 0 0 7 11 0 0 1 0 11 1 7 3 1 0 0 10 12 0 2 Yes! Because the left and right subtrees of every Nope! The left and right subtrees of some nodes (e.g. node have heights differing by at most 1 1, 4, 6) have heights that differ by more than 1 Winter 2016 CSE373: Data Structures & Algorithms 9 Winter 2016 CSE373: Data Structures & Algorithms 10 The shallowness bound Implementing AVL Trees Let S ( h ) = the minimum number of nodes in an AVL tree of height h Node structure – S(h) grows exponentially in h . • (Can be proved, but we will not do it in class.) Tree operations – Therefore, a tree with n nodes has a logarithmic height (We'll want to be sure these operate in O(log n) worst case time.) – Thus FIND can be done in O(log n) time. – We will also see that INSERT and DELETE can be done in O(log n) time, while maintaining the AVL property. h h -2 h -1 Winter 2016 CSE373: Data Structures & Algorithms 11 Winter 2016 CSE373: Data Structures & Algorithms 12 2

  3. 1/21/2016 An AVL Tree AVL tree operations Node object 10 key • AVL find : 3 … value – Same as BST find 10 3 height 2 2 • AVL insert : children 5 20 – First BST insert , then check balance and potentially “fix” the AVL tree 1 1 0 0 – Four different imbalance cases 15 30 2 9 0 0 • AVL delete : 7 17 – The “easy way” is lazy deletion – Otherwise, do the deletion and then check for several imbalance Track height at all times! cases (we will skip this) Winter 2016 CSE373: Data Structures & Algorithms 13 Winter 2016 CSE373: Data Structures & Algorithms 14 Insert: detect potential imbalance Case #1: Example Insert(6) 0 1 2 6 6 6 1. Insert the new node as in a BST (a new leaf) Insert(3) 2. For each node on the path from the root to the new leaf, the Insert(1) 0 1 insertion may (or may not) have changed the node’s height 3 3 3. So after insertion in a subtree, detect height imbalance and Third insertion violates perform a rotation to restore balance at that node 0 balance property 1 • happens to be at All the action is in defining the correct rotations to restore balance the root Fact that an implementation can ignore: What is the only way to 1 – There must be a deepest element that is imbalanced after the 3 fix this? insert (all descendants still balanced) 0 0 – After rebalancing this deepest node, every node is balanced 1 6 – So at most one node needs to be rebalanced Winter 2016 CSE373: Data Structures & Algorithms 15 Winter 2016 CSE373: Data Structures & Algorithms 16 Fix: Apply “Single Rotation” The example generalized • Single rotation: The basic operation we’ll use to rebalance • Insertion into left-left grandchild causes an imbalance – Move child of unbalanced node into parent position – 1 of 4 possible imbalance causes (other 3 coming up!) – Parent becomes the “other” child (always okay in a BST!) • Creates an imbalance in the AVL tree (specifically a is imbalanced) – Other subtrees move in only way BST allows (next slide) AVL Property violated at node 6 a h+2 a h+3 h+1 h+2 1 2 h b h b 3 6 h h h+1 h Z 1 Z 0 0 3 X Y 1 6 X Y 0 1 Child’s new-height = old-height-before-insert Winter 2016 CSE373: Data Structures & Algorithms 17 Winter 2016 CSE373: Data Structures & Algorithms 18 3

  4. 1/21/2016 The general left-left case Another example: insert(16) • So we rotate at a – Move child of unbalanced node into parent position 15 – Parent becomes the “other” child 8 22 – Other sub-trees move in the only way BST allows: • using BST facts: X < b < Y < a < Z 24 4 10 19 a h+3 b h+2 6 h+2 3 17 20 h+1 a b h 16 h h+1 h+1 h h 15 Z X 8 19 X Y Y Z 22 4 10 17 • A single rotation restores balance at the node 16 20 24 3 6 – To same height as before insertion, so ancestors now balanced Winter 2016 CSE373: Data Structures & Algorithms 19 Winter 2016 CSE373: Data Structures & Algorithms 20 The general right-right case Two cases to go • Mirror image to left-left case, so you rotate the other way Unfortunately, single rotations are not enough for insertions in the left-right subtree or the right-left subtree – Exact same concept, but need different code Simple example: insert (1), insert (6), insert (3) – First wrong idea: single rotation like we did for left-left h+3 a b h+2 h+2 h 2 h+1 1 b 1 Violates order a h+1 6 property! h X h h 1 h+1 6 Z 0 0 Y Z X Y 3 1 0 3 Winter 2016 CSE373: Data Structures & Algorithms 21 Winter 2016 CSE373: Data Structures & Algorithms 22 Two cases to go Sometimes two wrongs make a right  • First idea violated the order property Unfortunately, single rotations are not enough for insertions in the • Second idea didn’t fix balance left-right subtree or the right-left subtree • But if we do both single rotations, starting with the second, it works! (And not just for this example.) Simple example: insert (1), insert (6), insert (3) • Double rotation: – Second wrong idea: single rotation on the child of the 1. Rotate problematic child and grandchild unbalanced node 2 2. Then rotate between self and new child 2 1 1 Still unbalanced! 2 2 1 1 1 1 1 6 3 3 1 6 1 3 0 0 3 6 0 1 0 0 0 3 6 6 Winter 2016 CSE373: Data Structures & Algorithms 23 Winter 2016 CSE373: Data Structures & Algorithms 24 4

Recommend


More recommend