Download AVL Trees - Algorithms and Abstract Data Structures - Lecture Slides and more Slides Data Structures and Algorithms in PDF only on Docsity!
CPSC 223
Algorithms & Data Abstract Structures
Lecture 17:!
AVL Trees!
Today …
• Quiz 8!
• Assignments!
– Assignment 7 due!
– Assignment 8 assigned!
• Heapsort exercise!
• AVL Trees (intro) [Ch 12: pp. 681-686]!
'()'&$$*&++&,-..&$"!"&
'()'&$$*&++&,-..&$"!"& AVL Trees “Self-Balancing” Binary Search Trees Recall that in a BST!
- Depending on the order of insertion!
- … we may end up with a structure that is not “tree-like”! In these (worst) cases!
- O ( n ) to find ( lookup / retrieve ) a node w/ matching search key!
- And thus O ( n ) to insert and delete!
- We can do better by keeping the tree balanced …! '()'&$$*&++&,-..&$"!"&
G&
Minimum Height Trees Full and complete binary trees are of “ minimum height ”! 2 h^ – 1 = nodes in a full binary tree !log 2 ( n +1)" = minimum height h of a binary tree! '()'&$$*&++&,-..&$"!"&
6<=3&=3&-&@A..&B=2-5C&6544&
6<=3&=3&-&D?EF.464&B=2-5C&6544&
!log 2 (7+1)" = 3& !log 2 (6+1)" = 3&
Binary Trees A “ balanced ” binary tree has for every node!
- left and right subtrees that differ in height by at most 1 '()'&$$*&++&,-..&$"!"&
,& 6<=3&=3& !"# &-&B-.-2D4H&B=2-5C&6544&
h = 3 h = 1 0&
6<=3& $% &-&B-.-2D4H&B=2-5C&6544& ,&
h = 3 h = 2
0& &'('!)+,')#"- &?@&&I.4J&<4=><6K&L&!&I5=><6&<4=><6K&M&$&
0& &'('!)+,')#"- &?@&&L&$&M&!&
/-.-2D4H&M&B-.-2D4&@-D6?5&?@&!N&"N&?5&+!&
O&
Binary Trees Are all balanced binary trees of minimum height?!
- NO! … Being balanced does not imply minimum height!
- But, balanced trees still have a height that is O (log n )
This is a subtle point …!
- It means we don’t need to maintain strictly complete trees! '()'&$$*&++&,-..&$"!"&
6<=3& $% &-&B-.-2D4H&B=2-5C&6544& ,&
!log 2 (7+1)" = 3!&
Heaps Revisited
- “Coolness” of heaps:!
- They remain complete (i.e., of minimum height)!
- E.g., this lets us store them efficiently in an array!
- How does this work?!
- By using “ trickle down ” (delete) and “ trickle up ” (insert)!
- These use only a small number of steps (log n ) for “ balancing ”!
- So, we need something similar for BSTs!! '()'&$$*&++&,-..&$"!"&
Z&
A “Brute Force” Approach
T?=H& !"#$%&'()"" I/=2)4-5D<S544[&6544N&:265C&-55-CWXN&=26&\536N&=26&.-36K&
]&
&&&&=@I\536&^M&.-36K&]&
&&&&&&&&=26&E=HH.4&M&I\536&Y&.-36K&#&$_&
&&&&&&&& 65448 %*+"), I-55-CWE=HH.4XK_&
&&&&&&&& !"#$%&'()"" I6544N&-55-CN&\536N&E=HH.4+!K_&
&&&&&&&& !"#$%&'()"" I6544N&-55-CN&E=HH.4Y!N&.-36K_&
&&&&`&&
`&
'()'&$$*&++&,-..&$"!"&
123456&Q0R& '&
12?5H45&S5-T453-.&
UA6FA6&-55-C&
V4BA=.H&S544&
A “Brute Force” Approach
- What is the cost?!
- Finding/retrieving an item becomes O (log n )!
- Inorder traversal is O ( n )
- Reinserting is O ( n log n ) … O ( n ) inserts each O (log n )
- So the total cost is O (log n + n + n log n ) = O( n log n )
- Disadvantages!
- This is expensive (e.g., compared to a Heap)!
- Uses O ( n ) extra space for temporary array!
- We can do better than this!!
- AVL and Red Black (binary trees)!
- 2-3, 2-3-4, B Trees (for n -ary trees)! '()'&$$*&++&,-..&$"!"&
a&
AVL Trees [Adelson-Velskii & Landis, 1962]
- Use “ tree rotations ” to rebalance the tree!
- Do tree rotations (if needed) after insert or delete!
- Four cases:!
- Single rotation (“ left-left ”)!
- Single rotation (“ right-right ”)!
- Double rotation (“ left-right ”)!
- Double rotation (“ right-left ”)!
- Traverse up the tree from inserted/deleted node!
- Only necessary if an insertion/deletion changes the balance!
- Compute a “ balance factor ” at each node! '()'&$$*&++&,-..&$"!"& AVL Trees
- Single rotation example! '()'&$$*&++&,-..&$"!"&
a& !P& G& !"& !G& $& P& b = 1 b = 0 b = 1 b = 1
a& !P& G& !"& !G& $& P& !& b = 1 b = 2 b = 2 123456&Q!R& !$& G& !P& $& a& !G& !& P& !"& b = 1 b = 0 b = 1
V?6-b?2& b = 1
AVL Trees
- General case for single rotation (“ right-right ”)!
- Insertion in the right subtree of the right child of k 2 (subtree A)
- This is just the “ mirror image ” of the left-left case! '()'&$$*&++&,-..&$"!"& k 2 k 1 b = - b = - A B
S<4&5?6-b?2&3463&
6<4&3AB6544&6?&=63&
"-$.$!'(+/*$./# c&&
C
k 2 b = 0 k 1 b = 0 B
A C
AVL Trees
// return new root after left-left rotation
Node* rotateWithLeftChild(Node* k2)
// return new root after right-right subtree
Node* rotateWithRightChild(Node* k1)
'()'&$$*&++&,-..&$"!"&
AVL Trees A single rotation might not rebalance the tree …! '()'&$$*&++&,-..&$"!"& k 2 b = -1 k 1 b = 2 C
U?F3&d&6<=3&I.4J+.4JK&5?6-b?2&H=H2e6&<4.Fccc&&
A
B
k 2 k 1 b = 1 b = - C
B
A
AVL Trees
- We sometimes need 2 rotations …!
- General case for double rotation ( left - right )! '()'&$$&++&,-..&$"!"& k 3 b = -1 k 2 b = 2 D A B k 1 _C :!+",+#/%+/'%+#/+$!%-#9+!"9+_
k 3 b = 0 or 1^ k 2 b = 2 D A B k 1 C b = 1 or 2