Download presentation
Presentation is loading. Please wait.
1
Cinda Heeren / Geoffrey Tien
AVL Trees Properties Insertion October 17, 2017 Cinda Heeren / Geoffrey Tien
2
Cinda Heeren / Geoffrey Tien
BST rotation g g e h b h b f i a e i a c c f d d Rotations preserve the in-order BST property, while altering the tree structure we can use rotations to bring imbalanced trees back into balance October 12, 2017 Cinda Heeren / Geoffrey Tien
3
Cinda Heeren / Geoffrey Tien
AVL trees An AVL tree is a balanced BST Each node's left and right subtrees differ in height by at most 1 Rebalancing via rotations occurs when an insertion or removal causes excessive height difference AVL tree nodes contain extra information to support this height information 43 19 63 57 60 50 78 38 4 21 October 17, 2017 Cinda Heeren / Geoffrey Tien
4
Cinda Heeren / Geoffrey Tien
AVL nodes enum balance_type {LEFT_HEAVY = -1, BALANCED = 0, RIGHT_HEAVY = +1}; class AVLNode { public: int data; // or template type AVLNode left; AVLNode right; balance_type balance; AVLNode(int value) { ... } AVLNode(int val, AVLNode left1, AVLNode right1) { ... } } AVLNode is almost the same as a binary tree node additional balance field indicates that state of subtree balance at that node October 17, 2017 Cinda Heeren / Geoffrey Tien
5
Cinda Heeren / Geoffrey Tien
AVL imbalance Balanced trees: 3 3 7 12 16 3 7 12 19 3 16 27 31 44 Imbalanced trees: 12 19 3 16 27 31 44 21 24 12 16 3 7 9 3 7 5 October 17, 2017 Cinda Heeren / Geoffrey Tien
6
Cinda Heeren / Geoffrey Tien
AVL imbalance 4 cases of imbalance C B A C A B A B C A C B LL imbalance LR imbalance RR imbalance RL imbalance Solve with a right rotation around C Left rotation around A, becomes LL case Symmetric to left imbalance cases October 17, 2017 Cinda Heeren / Geoffrey Tien
7
Cinda Heeren / Geoffrey Tien
AVL insertion Maintaining balance The best way to keep a tree balanced, is to never let it become imbalanced! AVL insertion begins with ordinary BST insertion (i.e. a leaf node) followed by rotations to maintain balance i.e. AVL properties are satisfied before and after insertion if the balance attribute of a subtree's root node becomes critical (-2 or +2) as a result of inserting the new leaf, rebalance it! October 17, 2017 Cinda Heeren / Geoffrey Tien
8
Cinda Heeren / Geoffrey Tien
AVL insertion Pseudocode if root is NULL Create new node containing item, assign root to it, and return true else if item is equal to root->data item exists already, return false else if item < root->data Recursively insert the item into the left subtree if height of left subtree has increased (increase variable is true) balance--; if balance == 0, reset increase variable to false if balance < -1 reset increase variable to false perform rebalanceLeft else if item > root->data (symmetric to left subtree case, incrementing balance) rebalanceLeft and rebalanceRight are the rotations to correct the 4 imbalance cases October 17, 2017 Cinda Heeren / Geoffrey Tien
9
Cinda Heeren / Geoffrey Tien
AVL insertion example Insert(65) 47 32 71 65 93 October 17, 2017 Cinda Heeren / Geoffrey Tien
10
Cinda Heeren / Geoffrey Tien
AVL insertion example Insert(65) Insert(82) 47 RR imbalance 32 71 OK 65 93 OK 82 OK October 17, 2017 Cinda Heeren / Geoffrey Tien
11
Cinda Heeren / Geoffrey Tien
AVL insertion example Insert(65) Insert(82) 71 Insert(87) 47 93 LR imbalance 32 65 82 OK 87 OK October 17, 2017 Cinda Heeren / Geoffrey Tien
12
Cinda Heeren / Geoffrey Tien
AVL insertion example Insert(65) Insert(82) 71 OK Insert(87) 47 87 OK 32 65 82 93 October 17, 2017 Cinda Heeren / Geoffrey Tien
13
Cinda Heeren / Geoffrey Tien
AVL tree height The height of an AVL tree containing ๐ key values is ๐ log ๐ Intuition: For a fixed height โ, a tree containing fewer nodes has a larger height-to-node ratio ๐=15 ๐=4 โ=3= log ๐ =๐( log ๐ ) โ=3=๐โ1=๐ ๐ Attempt to achieve the worst ratio by making an AVL tree of height โ with the minimum number of nodes October 17, 2017 Cinda Heeren / Geoffrey Tien
14
Cinda Heeren / Geoffrey Tien
AVL tree height Theorem: The height of an AVL tree with ๐ nodes is ๐ log ๐ Proof: Let ๐ โ represent the minimum number of nodes in an AVL tree of height โ Since the AVL property must be satisfied at every node, the children of such a tree must also be minimal, and the height difference between the children must be 1 Thus ๐ โ =1+ ๐ โโ1 + ๐ โโ2 1 Nh-2 Nh-1 October 17, 2017 Cinda Heeren / Geoffrey Tien
15
Cinda Heeren / Geoffrey Tien
AVL tree height ๐ โ =1+ ๐ โโ1 + ๐ โโ2 ๐ โโ1 =1+ ๐ โโ2 + ๐ โโ3 ๐ โ =1+ 1+ ๐ โโ2 + ๐ โโ3 + ๐ โโ2 =2+2 ๐ โโ2 + ๐ โโ3 >2 ๐ โโ2 ๐ โโ2 =1+ ๐ โโ3 + ๐ โโ4 =2+2 ๐ โโ4 + ๐ โโ5 >2 ๐ โโ4 ๐ โ >2โ2 ๐ โโ4 ๐ โ >2โ2โ2 ๐ โโ6 How many times can we subtract 2 from โ before we reach 0? โ 2 times. ๐ โ >2โ2โ2โ2 ๐ โโ8 โฆ ๐ โ > 2 โ 2 โ< log ๐ โ โโ๐ log ๐ October 17, 2017 Cinda Heeren / Geoffrey Tien
16
Cinda Heeren / Geoffrey Tien
Exercise Draw the AVL tree resulting from the following sequence of insertions: 71, 13, 65, 22, 49, 37, 45 Note the height recorded/updated at each node What would an ordinary BST look like with the same sequence of insertions? October 17, 2017 Cinda Heeren / Geoffrey Tien
17
Readings for this lesson
Koffman Chapter 11.2 (AVL trees) AVL visualisations: (AVL tree) Next class: AVL removal October 17, 2017 Cinda Heeren / Geoffrey Tien
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.