Self Balancing Trees
Data Structures and Algorithms
CSE 373 SP 18 - KASEY CHAMPION 1
Self Balancing Trees Data Structures and Algorithms CSE 373 SP 18 - - PowerPoint PPT Presentation
Self Balancing Trees Data Structures and Algorithms CSE 373 SP 18 - KASEY CHAMPION 1 Warm Up What will the binary search tree look like if you insert nodes in the following order: 5, 8, 7, 10, 9, 4, 2, 3, 1 What is the pre-order traversal
Data Structures and Algorithms
CSE 373 SP 18 - KASEY CHAMPION 1
What will the binary search tree look like if you insert nodes in the following order: 5, 8, 7, 10, 9, 4, 2, 3, 1 What is the pre-order traversal order for the resulting tree?
Socrative:
www.socrative.com Room Name: CSE373 Please enter your name as: Last, First
CSE 373 SP 18 - KASEY CHAMPION 2
Binary Search Trees allow us to:
Dictionary Operations: Runtime in terms of height, “h” get() – O(h) put() – O(h) remove() – O(h)
What do you replace the node with? Largest in left sub tree or smallest in right sub tree
CSE 373 SP 18 - KASEY CHAMPION 3
10 “foo” 7 “bar” 12 “baz” 9 “sho” 5 “fo” 15 “sup” 13 “boo” 8 “poo” 1 “burp”
CSE 373 SP 18 - KASEY CHAMPION 4
For “balanced” trees h ≈ logc(n) where c is the maximum number of children Balanced binary trees h ≈ log2(n) Balanced trinary tree h ≈ log3(n) Thus for balanced trees operations take Θ(logc(n))
CSE 373 SP 18 - KASEY CHAMPION 5
Is this a valid Binary Search Tree? Yes, but… We call this a degenerat enerate e tree ee For trees, depending on how balanced they are, Operations at worst can be O(n) and at best can be O(logn) How are degenerate trees formed?
CSE 373 SP 18 - KASEY CHAMPION 6
10 9 7 5
Measuring balance: For each node, compare the heights of its two sub trees Balanced when the difference in height between sub trees is no greater than 1
CSE 373 SP 18 - KASEY CHAMPION 7
10 8 15 7 12 18 8 7 7 8 7 9
AVL Trees es must satisfy the following properties:
must be larger than the root node
AVL stands for Adelson-Velsky and Landis (the inventors of the data structure)
CSE 373 SP 18 - KASEY CHAMPION 8
CSE 373 SP 18 - KASEY CHAMPION 9
7 4 10 3 9 12 5 8 11 13 14 2 6 Is it…
yes yes yes
CSE 373 SP 18 - KASEY CHAMPION 10
6 2 8 1 7 12 4 9 10 13 11 3 5 Is it…
yes yes no Height = 2 Height = 0
CSE 373 SP 18 - KASEY CHAMPION 11
8 6 11 2 15 7
9 Is it…
yes no yes 9 > 8 5
Dictionary Operations: get() – same as BST containsKey() – same as BST put() - ??? remove() - ???
CSE 373 SP 18 - KASEY CHAMPION 12
Add the node to keep BST, fix AVL property if necessary Replace the node to keep BST, fix AVL property if necessary
1 2 3 Unbalanced! 2 1 3
CSE 373 SP 18 - KASEY CHAMPION 13
a b X c Y Z a b X Y Z c a b X Y Z Insert ‘c’ Unbalanced! Balanced!
CSE 373 SP 18 - KASEY CHAMPION 14
a b X c Y Z a b X Y Z c a b X Y Z Insert ‘c’ Unbalanced! Balanced!
CSE 373 SP 18 - KASEY CHAMPION 15
15 8 22 4 24 10 3 19 6 20 17 put(16); 16
CSE 373 SP 18 - KASEY CHAMPION 16
15 8 22 4 24 10 3 19 6 20 17 put(16); 16
CSE 373 SP 18 - KASEY CHAMPION 17
1 3 2 Unbalanced! 3 1 2 Rotate Left Unbalanced! Rotate Right 1 3 2 Unbalanced!
CSE 373 SP 18 - KASEY CHAMPION 18
1 3 2 1 2 3 Line Case Solve with 1 rotation Kink Case Solve with 2 rotations 3 2 1
Rotate Right Parent’s left becomes child’s right Child’s right becomes its parent
Rotate Left Parent’s right becomes child’s left Child’s left becomes its parent 3 1 2 Rotate subtree left Rotate root tree right Rotate subtree right Rotate root tree left
CSE 373 SP 18 - KASEY CHAMPION 19
a e W d Y Z a e X X Z Insert ‘c’ Unbalanced! d X Y c a d W Y Z X e c
CSE 373 SP 18 - KASEY CHAMPION 20
a d W Y Z X e c Unbalanced! a d W Y Z X e c