Download presentation
Presentation is loading. Please wait.
1
AVL Trees (a few more slides)
CSE 373 Data Structures Lecture 8.5
2
AVL Trees addendum - Lecture 8.5
Insertion in AVL Trees Insert at the leaf (as for all BST) only nodes on the path from insertion point to root node have possibly changed in height So after the Insert, go back up to the root node by node, updating heights If a new balance factor (the difference hleft-hright) is 2 or –2, adjust tree by rotation around the node 1/29/02 AVL Trees addendum - Lecture 8.5
3
AVL Trees addendum - Lecture 8.5
Insert in BST Insert(T : reference tree pointer, x : element) : integer { if T = null then T := new tree; T.data := x; return 1;//the links to //children are null case T.data = x : return 0; //Duplicate do nothing T.data > x : return Insert(T.left, x); T.data < x : return Insert(T.right, x); endcase } 1/29/02 AVL Trees addendum - Lecture 8.5
4
AVL Trees addendum - Lecture 8.5
Insert in AVL trees Insert(T : reference tree pointer, x : element) : { if T = null then T := new tree; T.data := x; height := 0; case T.data = x : return ; //Duplicate do nothing T.data > x : return Insert(T.left, x); if ((height(T.left)- height(T.right)) = 2){ if (T.left.data > x ) then //outside case T = RotatefromLeft (T); else //inside case T = DoubleRotatefromLeft (T);} T.data < x : return Insert(T.right, x); code similar to the left case Endcase T.height := max(height(T.left),height(T.right)) +1; return; } 1/29/02 AVL Trees addendum - Lecture 8.5
5
Example of Insertions in an AVL Tree
2 20 Insert 5, 40 1 10 30 25 35 1/29/02 AVL Trees addendum - Lecture 8.5
6
Example of Insertions in an AVL Tree
2 3 20 20 1 1 1 2 10 30 10 30 1 5 25 35 5 25 35 40 Now Insert 45 1/29/02 AVL Trees addendum - Lecture 8.5
7
Single rotation (outside case)
3 3 20 20 1 2 1 2 10 30 10 30 2 1 5 25 35 5 25 40 35 45 40 1 Imbalance 45 Now Insert 34 1/29/02 AVL Trees addendum - Lecture 8.5
8
Double rotation (inside case)
3 3 20 20 1 3 1 2 10 30 10 35 2 1 1 5 Imbalance 25 40 5 30 40 25 34 1 45 35 45 Insertion of 34 34 1/29/02 AVL Trees addendum - Lecture 8.5
9
j k Z X Y AVL Insertion: Outside Case Consider a valid AVL subtree h+2
1/29/02 AVL Trees addendum - Lecture 8.5
10
j k Z Y X AVL Insertion: Outside Case Inserting into X
destroys the AVL property at node j (h+2) - h k Becomes h + 2 h Z h+1 h Y X 1/29/02 AVL Trees addendum - Lecture 8.5
11
k j X Z Y Outside Case Completed AVL property has been restored!
“Right rotation” done! (“Left rotation” is mirror symmetric) h+2 j h+1 h+1 h h X Z Y AVL property has been restored! 1/29/02 AVL Trees addendum - Lecture 8.5
12
j k Z X Y AVL Insertion: Inside Case Consider a valid AVL subtree h+2
1/29/02 AVL Trees addendum - Lecture 8.5
13
j k Z X Y AVL Insertion: Inside Case Inserting into Y destroys the
AVL property at node j k Becomes h + 2 h Z h h+1 X Y 1/29/02 AVL Trees addendum - Lecture 8.5
14
j k Z X Y AVL Insertion: Inside Case Consider the structure
of subtree Y… k h Z h h+1 X Y 1/29/02 AVL Trees addendum - Lecture 8.5
15
j k Z i X V W AVL Insertion: Inside Case Y = node i and
subtrees V and W h+2 k h Z i h h+1 X h or h-1 V W 1/29/02 AVL Trees addendum - Lecture 8.5
16
j k Z i X V W AVL Insertion: Inside Case We will do a left-right
“double rotation” . . . k Z i X V W 1/29/02 AVL Trees addendum - Lecture 8.5
17
i k j V Z W X Double rotation : second rotation
double rotation complete Balance has been restored h+2 i h+1 k j h+1 h h h or h-1 V W Z X 1/29/02 AVL Trees addendum - Lecture 8.5
18
Non-recursive insertion or the hacker’s delight
Key observations; At most one rotation Balance factor: 2 bits are sufficient (-1 left, 0 equal, +1 right) There is one node on the path of insertion, say S, that is “critical”. It is the node where a rotation can occur and nodes above it won’t have their balance factors modified 1/29/02 AVL Trees addendum - Lecture 8.5
19
Non-recursive insertion
Step 1 (Insert and find S): Find the place of insertion and identify the last node S on the path whose BF ≠ 0 (if all BF on the path = 0, S is the root). Insert Step 2 (Adjust BF’s) Restart from the child of S on the path of insertion. (note: all the nodes from that node on on the path of insertion have BF = 0.)If the path traversed was left (right) set BF to –1 (+1) and repeat until you reach a null link (at the place of insertion) 1/29/02 AVL Trees addendum - Lecture 8.5
20
Non-recursive insertion (ct’d)
Step 3 (Balance if necessary): If BF(S) = 0 (S was the root) set BF(S) to the direction of insertion (the tree has become higher) If BF(S) = -1 (+1) and we traverse right (left) set BF(S) = 0 (the tree has become more balanced) If BF(S) = -1 (+1) and we traverse left (right), the tree becomes unbalanced. Perform a single rotation or a double rotation depending on whether the path is left-left (right-right) or left-right (right-left) 1/29/02 AVL Trees addendum - Lecture 8.5
21
Non-recursive Insertion with BF’s
Step 1 & 2 Step 3 +1 +1 20 S 20 -1 +1 -1 10 30 10 35 0 ->1 1 5 25 40 5 30 40 25 34 0->-1 45 35 45 Insertion of 34 34 1/29/02 AVL Trees addendum - Lecture 8.5
Similar presentations
© 2024 SlidePlayer.com Inc.
All rights reserved.