View
217
Download
1
Category
Preview:
Citation preview
2
Background
Binary Search Trees allow dynamic allocation (like linked lists), but O(log2(n)) average case search time (like arrays).
Problem: they still have O(n) worst case search time.
Solution: if we can balance the trees on insert, we can have worst case O(log2(n)) search time.
3
AVL Trees
Named for inventors Adelsson, Velski, and Landis.
One way to maintain a balanced tree.Idea: keep the height of every node in the
tree.When the difference between the height
of a node’s left and right subtrees exceeds 1, rotate the nodes to eliminate the imbalance.
4
AVL Details
Each node will have an additional piece of information – its height. It is calculated as: H(node) = MAX(H(left), H(right)) + 1
Each node’s balance factor will be calculated when needed; the formula is BF(node) = H(left) – H(right)
When BF(node) = +2 or –2, a fix is needed.
5
AVL Algorithm
Insert a new key value in the normal way Call the newly inserted node “newnode”
Set H(newnode) = 1;For each node from newnode back to root
Recalculate H(node) and BF(node) If BF(node) = +2 or BF(node) = –2
Perform appropriate rotationOnce a rotation is performed, stop.
6
AVL Rotations
The appropriate rotation depends on where “newnode” is with respect to the node that is out of balance.
The four types are Right-Right (newnode was inserted into the
right subtree of the right subtree of the node) Left-Left Left-Right Right-Left
7
AVL Rotations II
The rotations are usually specified in general, since a rotation can occur anywhere in the tree.
Subtrees move as whole unitsNote in the following examples that the
height of the tree after rotation is the same as before the insertion.
This means that no heights or balance factors above this node will change.
8
AVL Tree: LL Rotation
This is the general tree set up.Now suppose a new node is inserted
into BL that increases its height:
A
B
BL BR
AR
h
h
H : h+1BF : 0
H : h+2BF : +1
9
AVL Tree: LL Rotation
BF of node A is now +2, so a rotation is necessary:
A
B
BL BR
AR
h
h
H : h+2BF : +1
H : h+2BF : +2
New
10
AVL Tree: LL Rotation
This is the result of the rotation.Note the height of the root is the
same as before the insertion.
A
B
BL
BR AR
h
h
H : h+1BF : 0
H : h+2BF : +0
New
11
LL Rotation Algorithm
Set up pointers for A, B, BL, BR, ARA->left = BRB->right = ARoot = BSet H and BF for nodes A and B.
NOTE: the book does this more “cleverly”, with fewer pointers. I think this is more clear.
12
RR Rotation
This is the general tree set up.Now suppose a new node is inserted
into BR that increases its height:
A
B
BL BR
AL
h
h
H : h+1BF : 0
H : h+2BF : -1
13
RR Rotation
BF of node A is –2, so a rotation is needed
A
B
BL BR
AL
h
h
H : h+2BF : -1
H : h+3BF : -2
New
14
RR Rotation
This is the result of the rotation.Note the height of the root returns to h+2
B
A
BL
BR
AL
h
h
H : h+2BF : 0
H : h+1BF : 0
New
15
RR Rotation Algorithm
Set up pointers for A, B, BL, BR, ALA->right = BLB->left = ARoot = BSet H and BF for nodes A and B.
16
LR Rotation
This is the general tree set up.Now suppose a new node is inserted
into CL that increases its height:
A
B
BL CL
hAR
h
H : h+1BF : 0
H : h+2BF : +1
C
CRh–1
H : hBF : 0
17
LR Rotation
The balance factor of A goes to +2, so a left-right rotation is needed.
A
B
BL CL
hAR
h
H : h+2BF : -1
H : h+3BF : +2
C
CRh–1
H : h+1BF : +1
New
18
LR Rotation
Again, note the height of the root returns to h+2.
C
B
BL
CLhAR
h
H : h+1BF : 0
H : h+2BF : 0
A
CRh–1
H : h+1BF : -1
New
19
LR Rotation Algorithm
Set up pointers for A, B, C, BL, AR, CL, CR
A->left = CRB->right = CLC->left = BC->right = ARoot = CSet H and BF for nodes A, B and C.
20
RL Rotation
This is the general tree set up.Now suppose a new node is inserted
into CL that increases its height:
A
B
CLBR
h
H : h+1BF : 0
H : h+2BF : -1
C
CR
h–1
H : hBF : 0
AL
h
21
RL Rotation
BF of Node A is now –2, so a rotation is necessary.
A
B
CLBR
h
H : h+2BF : +1
H : h+3BF : -2
C
CR
h–1
H : h+1BF : +1
AL
h
New
22
RL Rotation
This shows the result of the rotation.
A B
CL
BR
h
H : h+1BF : -1
H : h+2BF : 0 C
CR
h–1
H : h+1BF : 0
AL
h
New
23
RL Rotation Algorithm
Set up pointers for A, B, C, BR, AL, CL, CR
A->right = CLB->left = CRC->left = AC->right = BRoot = CSet H and BF for nodes A, B and C.
24
RL Rotation Method 2
First, Perform a LL rotation around B:
A
B
CLBR
h
H : h+2BF : +1
H : h+3BF : -2
C
CR
h–1
H : h+1BF : +1
AL
h
New
25
RL Rotation Method 2
Next, perform a RR Rotation around A:
A
BCL
BR
h
H : h+1BF : -1
H : h+3BF : -2
C
CR
h–1
H : h+2BF : -1
AL
h
New
26
RL Rotation Method 2
This result is the same as the first method.
A B
CL
BR
h
H : h+1BF : -1
H : h+2BF : 0 C
CR
h–1
H : h+1BF : 0
AL
h
New
27
AVL Example
Now let’s do an extended example, inserting a series of key values into an AVL tree and rotating as necessary.
We will be inserting 25, 50, 90, 10, 15, 20, 75.
Let’s start with 25:
28
AVL Example
25 is a new root; no problem.Now, insert 50:
25
Left to insert: 50, 90, 10, 15, 20, 75
H : 1BF : 0
29
AVL Example
No problem here.Now, insert 90:
25
50
Left to insert: 90, 10, 15, 20, 75
H : 2BF : -1
H : 1BF : 0
30
AVL Example
BF(25) = –2; what do we do?This is a RR rotation:
25
50
90
Left to insert: 10, 15, 20, 75
H : 3BF : -2
H : 2BF : -1
H : 1BF : 0
31
AVL Example
This is the result; note height of the root.Next, insert 10:
50
25 90
Left to insert: 10, 15, 20, 75
H : 2BF : 0
H : 1BF : 0
H : 1BF : 0
32
AVL Example
No problems here.Next, insert 15:
50
25
10
90
Left to insert: 15, 20, 75
H : 3BF : +1
H : 2BF : +1
H : 1BF : 0
H : 1BF : 0
33
AVL Example
BF(25)=+2, so time to rotateThis is a Left-Right (LR) rotation:(Note that I didn’t update 50)
50
25
10
15
90
Left to insert: 20, 75
H : 3BF : +1
H : 3BF : +2
H : 1BF : 0
H : 2BF : -1
H : 1BF : 0
34
AVL Example
This shows the result.Note that I didn’t need to update 50…Now, insert 20:
50
15
10
90
25
Left to insert: 20, 75
H : 3BF : +1
H : 2BF : 0
H : 1BF : 0
H : 1BF : 0
H : 1BF : 0
35
AVL Example
BF(50) = +2, so time to rotate…This is a left right (LR) rotation again:
50
15
10
20
90
25
Left to insert: 75
H : 4BF : +2
H : 3BF : -1
H : 1BF : 0
H : 2BF : +1
H : 1BF : 0
H : 1BF : 0
36
AVL Example
This is the result. Note the movements of the subtrees.
Finally, insert 75:
25
15
10
50
20 90
Left to insert: 75
H : 3BF : 0
H : 2BF : 0
H : 2BF : -1
H : 1BF : 0
H : 1BF : 0
H : 1BF : 0
37
AVL Example
H(50) = –2, so time to rotate again. This is a right-left (RL) rotation:
25
15
10
75
50
20 90
Left to insert: done
H : 3BF : 0
H : 2BF : 0
H : 3BF : -2
H : 1BF : 0
H : 1BF : 0
H : 2BF : +1
H : 1BF : 0
Recommended