2012 07 01
play

2012-07-01 From last time Binary search trees can give us great - PDF document

2012-07-01 From last time Binary search trees can give us great performance due to providing a structured binary search. CSE 332 Data Abstractions: This only occurs if the tree is balanced. A Heterozygous Forest of AVL, Splay, and B Trees


  1. 2012-07-01 From last time… Binary search trees can give us great performance due to providing a structured binary search. CSE 332 Data Abstractions: This only occurs if the tree is balanced. A Heterozygous Forest of AVL, Splay, and B Trees Kate Deibel Summer 2012 July 2, 2012 CSE 332 Data Abstractions, Summer 2012 1 July 2, 2012 CSE 332 Data Abstractions, Summer 2012 2 Three Flavors of Balance How to guarantee efficient search trees has been an active area of data structure research We will explore three variations of "balancing":  AVL Trees: Guaranteed balanced BST with only constant time additional overhead  Splay Trees: Ignore balance, focus on recency Arboreal masters of balance AVL TREES  B Trees: n-ary balanced search trees that work well with real world memory/disks July 2, 2012 CSE 332 Data Abstractions, Summer 2012 3 July 2, 2012 CSE 332 Data Abstractions, Summer 2012 4 Achieving a Balanced BST (part 1) Achieving a Balanced BST (part 2) For a BST with n nodes inserted in Shallower trees give better performance arbitrary order  This happens when the tree's height is O(log n)  like a perfect or complete tree  Average height is O(log n) – see text  Worst case height is O(n) Solution: Require a Balance Condition that  Simple cases, such as pre-sorted, lead to 1. ensures depth is always O (log n ) worst-case scenario 2. is easy to maintain  Inserts and removes can and will destroy any current balance July 2, 2012 CSE 332 Data Abstractions, Summer 2012 5 July 2, 2012 CSE 332 Data Abstractions, Summer 2012 6 1

  2. 2012-07-01 Potential Balance Conditions Potential Balance Conditions 1. Left and right subtrees 3. Left and right subtrees of the root have equal of every node have number of nodes equal number of nodes Too weak! Too strong! Height mismatch example: Only perfect trees (2 n – 1 nodes) 2. Left and right subtrees 4. Left and right subtrees of the root have equal of every node have height equal height Too weak! Too strong! Double chain example: Only perfect trees (2 n – 1 nodes) July 2, 2012 CSE 332 Data Abstractions, Summer 2012 7 July 2, 2012 CSE 332 Data Abstractions, Summer 2012 8 The AVL Balance Condition An AVL Tree? Left and right subtrees of every node have To check if this tree is an AVL, we calculate heights differing by at most 1 the heights and balances for each node h:4, b:2 6 Mathematical Definition: For every node x, – 1  balance (x)  1 where h:1, b:0 h:3, b:2 4 8 balance (node) h:2, b:-2 1 5 7 11 0 0 0 = height(node.left) – height(node.right) h:-1 3 h:1, b:1 0 2 July 2, 2012 CSE 332 Data Abstractions, Summer 2012 9 July 2, 2012 CSE 332 Data Abstractions, Summer 2012 10 AVL Balance Condition Calculating Height What is the height of a tree with root r? Ensures small depth  Can prove by showing an AVL tree of int treeHeight(Node root) { if(root == null ) height h must have nodes exponential in h return -1; return 1 + max( treeHeight (root.left), treeHeight (root.right)); Efficient to maintain }  Requires adding a height parameter to the node class (Why?) Running time for tree with n nodes: 10 key  Balance is maintained through O ( n ) – single pass over tree value … constant time manipulations of Very important detail of definition: 3 height the tree structure: single and height of a null tree is -1, height of tree children double rotations with a single node is 0 July 2, 2012 CSE 332 Data Abstractions, Summer 2012 11 July 2, 2012 CSE 332 Data Abstractions, Summer 2012 12 2

  3. 2012-07-01 Height of an AVL Tree? Minimal AVL Tree (height = 0) Using the AVL balance property, we can determine the minimum number of nodes in an AVL tree of height h Recurrence relation: Let S ( h ) be the minimum nodes in height h , then S ( h ) = S ( h -1) + S ( h -2) + 1 where S (-1) = 0 and S (0) = 1 Solution of Recurrence: S ( h )  1.62 h July 2, 2012 CSE 332 Data Abstractions, Summer 2012 13 July 2, 2012 CSE 332 Data Abstractions, Summer 2012 14 Minimal AVL Tree (height = 1) Minimal AVL Tree (height = 2) July 2, 2012 CSE 332 Data Abstractions, Summer 2012 15 July 2, 2012 CSE 332 Data Abstractions, Summer 2012 16 Minimal AVL Tree (height = 3) Minimal AVL Tree (height = 4) July 2, 2012 CSE 332 Data Abstractions, Summer 2012 17 July 2, 2012 CSE 332 Data Abstractions, Summer 2012 18 3

  4. 2012-07-01 AVL Tree Operations Insert / Detect Potential Imbalance Insert the new node (at a leaf, as in a BST) AVL find:  For each node on the path from the new  Same as BST find leaf to the root  The insertion may, or may not, have AVL insert: changed the node’s height  Starts off the same as BST insert After recursive insertion in a subtree  Then check balance of tree  detect height imbalance  Potentially fix the AVL tree (4 imbalance cases)  perform a rotation to restore balance at that node AVL delete:  Do the deletion All the action is in defining the correct rotations to restore balance  Then handle imbalance (same as insert) July 2, 2012 CSE 332 Data Abstractions, Summer 2012 19 July 2, 2012 CSE 332 Data Abstractions, Summer 2012 20 The Secret Example If there is an imbalance, then there must Insert(6) 0 1 2 6 6 6 be a deepest element that is imbalanced Insert(3) 0 1  After rebalancing this deepest node, every 3 3 Insert(1) node is then balanced 0 1  Ergo, at most one node needs rebalancing Third insertion violates balance What is a way to fix this? July 2, 2012 CSE 332 Data Abstractions, Summer 2012 21 July 2, 2012 CSE 332 Data Abstractions, Summer 2012 22 Single Rotation Single Rotation Example: Insert(16) The basic operation we use to rebalance  Move child of unbalanced node into parent position 15  Parent becomes a “other” child 8 22  Other subtrees move as allowed by the BST 24 4 10 19 6 17 20 3 1 2 16 Balance 3 6 violated here 1 0 0 3 1 6 0 1 July 2, 2012 CSE 332 Data Abstractions, Summer 2012 23 July 2, 2012 CSE 332 Data Abstractions, Summer 2012 24 4

  5. 2012-07-01 Single Rotation Example: Insert(16) Single Rotation Example: Insert(16) 15 15 8 22 8 22 24 24 4 10 19 4 10 19 17 20 17 20 3 6 3 6 16 16 15 8 19 4 10 17 22 16 3 6 20 24 July 2, 2012 CSE 332 Data Abstractions, Summer 2012 25 July 2, 2012 CSE 332 Data Abstractions, Summer 2012 26 Left-Left Case Left-Left Case Node imbalanced due to insertion in left- So we rotate at a, using BST facts: X < b < Y < a < Z left grandchild (1 of 4 imbalance cases) A single rotation restores balance at the node First we did the insertion, which made a  Node is same height as before insertion, so imbalanced ancestors now balanced h+2 a h+3 a h+3 h+2 a b h+1 h+2 h+2 h h+1 b h b h a b h h+1 h h h h+1 h+1 h h Z Z Z X X Y X Y X Y Y Z July 2, 2012 CSE 332 Data Abstractions, Summer 2012 27 July 2, 2012 CSE 332 Data Abstractions, Summer 2012 28 Right-Right Case The Other Two Cases Mirror image to left-left case, so you rotate Single rotations not enough for insertions the other way left-right or right-left subtree  Exact same concept, but different code  Simple example: insert(1), insert(6), insert(3) First wrong idea: single rotation as before h+3 2 a 1 1 h+2 b h+2 h 6 h+1 1 b h+1 6 a h X h 0 0 h h+1 1 3 0 Z 3 Y Z X Y July 2, 2012 CSE 332 Data Abstractions, Summer 2012 29 July 2, 2012 CSE 332 Data Abstractions, Summer 2012 30 5

  6. 2012-07-01 The Other Two Cases Double Rotation Single rotations not enough for insertions First attempt at violated the BST property left-right or right-left subtree Second attempt did not fix balance  Simple example: insert(1), insert(6), insert(3) Double rotation: If we do both, it works! Second wrong idea: single rotation on child  Rotate problematic child and grandchild  Then rotate between self and new child 2 2 1 1 Intuition: 3 must become root 2 2 1 1 1 1 1 6 3 3 1 6 1 3 0 0 3 6 0 0 0 0 3 1 6 6 July 2, 2012 CSE 332 Data Abstractions, Summer 2012 31 July 2, 2012 CSE 332 Data Abstractions, Summer 2012 32 Right-Left Case Right-Left Case Height of the subtree after rebalancing is the h+3 a h+2 same as before insert h b h+1  No ancestor in the tree will need rebalancing h c X h h-1 Does not have to be implemented as two Z V rotations; can just do: U h+2 c h+3 h+3 h+1 h+2 a h+1 a c h+2 b h+2 a h c h h b h-1 h+1 h h+1 h+1 h h+1 h h b a b c h X h-1 U V X h h X h-1 U h Z h-1 h V Z U V V X U Z Z July 2, 2012 CSE 332 Data Abstractions, Summer 2012 33 July 2, 2012 CSE 332 Data Abstractions, Summer 2012 34 Left-Right Case Memorizing Double Rotations Mirror image of right-left Easier to remember than you may think:  No new concepts, just additional code to write  Move grandchild c to grandparent’s position  Put grandparent a, parent b, and subtrees X, U, V , and Z in the only legal position h+3 a h+2 c h+2 h b h+1 h+1 h+1 a b c h Z h h h h-1 h h-1 U V X X U Z V July 2, 2012 CSE 332 Data Abstractions, Summer 2012 35 July 2, 2012 CSE 332 Data Abstractions, Summer 2012 36 6

Recommend


More recommend