10 17 2016
play

10/17/2016 Review: Binary Search Trees (BST) Structure property - PDF document

10/17/2016 Review: Binary Search Trees (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


  1. 10/17/2016 Review: Binary Search Trees (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 Autumn 2016 13 This lecture material represents the work of multiple instructors at the University of Washington. Thank you to all who have contributed! Autumn 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! –  (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 –  (n 2 ) in the worst case sequences of operations can eventually transform our carefully balanced tree into the dreaded list  How? • So, we also need to also keep the tree balanced as we perform operations. 1 1 2 1 2 3 Autumn 2016 CSE373: Data Structures & Algorithms 3 Autumn 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: Autumn 2016 CSE373: Data Structures & Algorithms 5 Autumn 2016 CSE373: Data Structures & Algorithms 6 1

  2. 10/17/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 – This is because an AVL tree of height h must have a number of nodes exponential in h Result: Worst-case depth is in  (log n ) Thus height must be log(number of nodes). • Efficient to maintain • Named after inventors Adelson-Velskii and Landis (AVL) – Using single and double rotations – First invented in 1962 7 Autumn 2016 CSE373: Data Structures & Algorithms Autumn 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 11 0 7 1 3 1 0 0 10 12 2 0 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 Autumn 2016 CSE373: Data Structures & Algorithms 9 Autumn 2016 CSE373: Data Structures & Algorithms 10 What do AVL trees give us? An AVL Tree Node object 10 key • If we have an AVL tree, then the number of nodes is an 3 … value exponential function of the height. 10 3 height 2 2 children • Thus the height is a log function of the number 5 20 of nodes! 1 1 0 0 • And thus find is O ( log n ) 2 9 15 30 But as we insert and delete elements, we need to: 0 0 1. Track balance 7 17 2. Detect imbalance Track height at all times! 3. Restore balance Autumn 2016 CSE373: Data Structures & Algorithms 11 Autumn 2016 CSE373: Data Structures & Algorithms 12 2

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

  4. 10/17/2016 Another example: insert(16) The general right-right case 15 • Mirror image to left-left case, so you rotate the other way 8 22 – Exact same concept, but needs different code 24 4 10 19 h+3 3 6 17 20 a b h+2 h+2 h 16 h+1 15 b h+1 a h X h 8 19 h h+1 Z 22 4 10 17 Y Z X Y 16 20 24 3 6 Autumn 2016 CSE373: Data Structures & Algorithms 19 Autumn 2016 CSE373: Data Structures & Algorithms 20 Two cases to go Two cases to go Unfortunately, single rotations are not enough for insertions in the Unfortunately, single rotations are not enough for insertions in the left-right subtree or the right-left subtree left-right subtree or the right-left subtree Simple example: insert (1), insert (6), insert (3) Simple example: insert (1), insert (6), insert (3) – First wrong idea: single rotation like we did for left-left – Second wrong idea: single rotation on the child of the unbalanced node 2 2 2 1 1 1 1 Violates order Still unbalanced! 6 property! 1 1 1 6 6 3 0 0 3 1 0 0 0 3 3 6 Autumn 2016 CSE373: Data Structures & Algorithms 21 Autumn 2016 CSE373: Data Structures & Algorithms 22 Sometimes two wrongs make a right  The general right-left case h+3 • First idea violated the order property a h+2 • Second idea didn’t fix balance h b h+1 • But if we do both single rotations, starting with the second, it h c works! (And not just for this example.) X h h-1 • Double rotation: Z V 1. Rotate problematic child and grandchild U 2. Then rotate between self and new child h+2 c h+3 2 2 a h+1 1 h+1 1 b h+2 a 1 h c h h 1 3 h+1 h h h-1 6 1 b 3 X U V h-1 X U Z 0 1 0 h 0 0 V 3 6 6 Z Autumn 2016 CSE373: Data Structures & Algorithms 23 Autumn 2016 CSE373: Data Structures & Algorithms 24 4

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend