AVL Trees (a few more slides)
description
Transcript of AVL Trees (a few more slides)
AVL Trees (a few more slides)
CSE 373
Data Structures
Lecture 8.5
1/29/02 AVL Trees addendum - Lecture 8.5
2
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
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 nullcase 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
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 caseEndcase 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
1
0
2
20
10 30
25
0
35
0
Insert 5, 40
1/29/02 AVL Trees addendum - Lecture 8.5
6
Example of Insertions in an AVL Tree
1
0
2
20
10 30
25
1
35
0
50
20
10 30
25
1
355
40
0
0
01
2
3
Now Insert 45
1/29/02 AVL Trees addendum - Lecture 8.5
7
Single rotation (outside case)
2
0
3
20
10 30
25
1
35
2
50
20
10 30
25
1
405
40
0
0
0
1
2
3
45
Imbalance35 45
0 0
1
Now Insert 34
1/29/02 AVL Trees addendum - Lecture 8.5
8
Double rotation (inside case)
3
0
3
20
10 30
25
1
40
2
50
20
10 35
30
1
405
45
0 1
2
3
Imbalance
45
0
1
Insertion of 34
35
34
0
0
1 25 340
1/29/02 AVL Trees addendum - Lecture 8.5
9
j
k
X Y
Z
Consider a validAVL subtree
AVL Insertion: Outside Case
h
hh
h+1
h+2
1/29/02 AVL Trees addendum - Lecture 8.5
10
j
k
XY
Z
Inserting into Xdestroys the AVL property at node j (h+2) - h
AVL Insertion: Outside Case
h
h+1 h
Becomes h + 2
1/29/02 AVL Trees addendum - Lecture 8.5
11
j
k
X Y Z
“Right rotation” done!(“Left rotation” is mirror symmetric)
Outside Case Completed
AVL property has been restored!
h
h+1
h
h+1
h+2
1/29/02 AVL Trees addendum - Lecture 8.5
12
j
k
X Y
Z
Consider a validAVL subtree
AVL Insertion: Inside Case
h
hh
h+1
h+2
1/29/02 AVL Trees addendum - Lecture 8.5
13
Inserting into Y destroys theAVL propertyat node j
j
k
XY
Z
AVL Insertion: Inside Case
h
h+1h
Becomes h + 2
1/29/02 AVL Trees addendum - Lecture 8.5
14
Consider the structureof subtree Y… j
k
XY
Z
AVL Insertion: Inside Case
h
h+1h
1/29/02 AVL Trees addendum - Lecture 8.5
15
j
k
X
V
Z
W
i
Y = node i andsubtrees V and W
AVL Insertion: Inside Case
h
h+1h
h or h-1
h+2
1/29/02 AVL Trees addendum - Lecture 8.5
16
j
k
X
V
Z
W
i
AVL Insertion: Inside Case
We will do a left-right “double rotation” . . .
1/29/02 AVL Trees addendum - Lecture 8.5
17
jk
X V ZW
i
Double rotation : second rotation
double rotation complete
Balance has been restored
hh h or h-1
h+1h+1
h+2
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
+1
0
+1
20
10 30
25
-1
40
0 ->1
50
20
10 35
30
-1
405
45
0 0
0
+1
45
0
1
Insertion of 3435
34
00->-1 25 340
Step 1 & 2
S
0
0
Step 3