Unformatted text preview: Balanced Search Trees
• The height of a binary search tree is sensi2ve to the order of inser2ons and dele2ons. – The height of a binary search tree is between Ⱥ log2(N+1) Ⱥ and N. – So, the worst case behavior of some BST opera2ons are O(N). • There are various search trees that can retain their balance at the end of each inser2on and dele2on. –
–
–
– AVL Trees 2
3 Trees 2
3
4 Trees Red
Black Trees • In these height balanced search trees, the run 2me complexity of inser2on, dele2on, and retrieval opera2ons is O(log2N) at the worst case. 4/1/11 CS202  Fundamental Structures of Computer Science II 1 AVL Trees
• An AVL tree is a binary search tree with a balance condi2on. • AVL is named for its inventors: Adel’son
Vel’skii and Landis • AVL tree approximates the ideal tree (completely balanced tree). • AVL Tree maintains a height close to the minimum. 4/1/11 CS202  Fundamental Structures of Computer Science II 2 AVL Trees
Deﬁni'on: An AVL tree is a binary search tree such that for any node in the tree, the height of the le< and right subtrees can diﬀer by at most 1. An AVL tree 4/1/11 NOT an AVL tree
(unbalanced nodes
are darkened)
CS202  Fundamental Structures of Computer Science II 3 AVL Trees
Proper5es
• The depth of a typical node in an AVL tree is very close to the op2mal log2N. • Consequently, all searching opera2ons in an AVL tree have logarithmic worst
case bounds. • An update (insert or delete) in an AVL tree could destroy the balance. It must then be rebalanced before the opera5on can be considered complete. 4/1/11 CS202  Fundamental Structures of Computer Science II 4 AVL Trees
Balance Opera5ons
• Balance is restored by tree rota'ons. • There are four diﬀerent cases for rota2ons: 1. Single Right Rota2on 2. Single LeU Rota2on 3. Double Right
LeU Rota2on 4. Double LeU
Right Rota2on 4/1/11 CS202  Fundamental Structures of Computer Science II 5 AVL Trees
Single Rota5on
• A single rota2on switches the roles of the parent and the child while maintaining the search order. • We rotate between a node and its child (leU or right). – Child becomes parent – Parent becomes right child in Case 1 (single right rota2on) Parent becomes leU child in Case 2 (single leU rota2on) • The result is a binary search tree that sa2sﬁes the AVL property. 4/1/11 CS202  Fundamental Structures of Computer Science II 6 Case 1
Single Right Rota5on
Child becomes parent Parent becomes right child 4/1/11 CS202  Fundamental Structures of Computer Science II 7 Case 1
Single Right Rota5on
Child becomes parent Parent becomes right child 4/1/11 CS202  Fundamental Structures of Computer Science II 8 Case 2 – Single LeE Rota5on
Before Rotation After Rotation Child becomes parent Parent becomes leE child 4/1/11 CS202  Fundamental Structures of Computer Science II 9 Case 3
Double Right
LeE Rota5on The height of
B (or C) is the
same as the
height of D First perform single right rota5on on k2 and k3 Then perform single leE rota5on on k2 and k1 4/1/11 CS202  Fundamental Structures of Computer Science II 10 Case 4
Double LeE
Right Rota5on
The height of
B (or C) is the
same as the
height of A First perform single leE rota5on on k2 and k1 Then perform single right rota5on on k2 and k3 4/1/11 CS202  Fundamental Structures of Computer Science II 11 Case 4
Double LeE
Right Rota5on
First perform single leE rota5on on k2 and k1 Then perform single right rota5on on k2 and k3 4/1/11 CS202  Fundamental Structures of Computer Science II 12 AVL Trees
Inser5on
• It is enough to perform rota2on only at the ﬁrst node – Where imbalance occurs – On the path from the inserted node to the root. • If imbalance occurs – Single rota2on is necessary – Single rota2on preserves the height of the original tree (before inser2on) • The height of the new subtree (aUer inser2on) is exactly the same as the height of the original subtree (before inser2on). – Thus the rota2on takes O(1) 2me. • AUer inser2on, only nodes that are on the path from the inser2on point to the root can have their balances changed. • Hence inser2on is O(logN) 4/1/11 CS202  Fundamental Structures of Computer Science II 13 AVL Trees
Inser5on
Exercise: Star2ng with an empty AVL tree, insert the following items 7 6 5 4 3 2 1 8 9 10 11 12 Check the following applet for more exercises. hap://www.site.uoaawa.ca/~stan/csi2514/applets/avl/BT.html 4/1/11 CS202  Fundamental Structures of Computer Science II 14 AVL Trees
Dele5on
• Dele2on is more complicated. – It requires both single and double rota2ons – We may need more than one rebalance opera2on (rota2on) on the path from the deleted node back to the root. • Steps: – First delete the node the same as dele2ng it from a binary search tree • Remember that a node can be either a leaf node or a node with a single child or a node with two children – Walk through from the deleted node back to the root and rebalance the nodes on the path if required • Since a rota2on can change the height of the original tree • Dele2on is O(logN) – Each rota2on takes O(1) 2me – We may have at most h (height) rota2ons, where h = O(logN) 4/1/11 CS202  Fundamental Structures of Computer Science II 15 AVL Trees
Dele5on
• For the implementa2on – We have a shorter ﬂag that shows if a subtree has been shortened – Each node is associated with a balance factor!
• le6
high
• right
high • equal the height of the leU subtree is higher than that of the right subtree the height of the right subtree is higher than that of the leU subtree the height of the leU and right subtrees is equal • In the dele2on algorithm – Shorter is ini2alized as true – Star2ng from the deleted node back to the root, take an ac2on depending on • The value of shorter • The balance factor of the current node • Some2mes the balance factor of a child of the current node – Un2l shorter becomes false 4/1/11 CS202  Fundamental Structures of Computer Science II 16 AVL Trees
Dele5on
Three cases according to the balance factor of the current node 1. The balance factor is equal no rota'on 2. The balance factor is not equal and the taller subtree was shortened no rota'on 3. The balance factor is not equal and the shorter subtree was shortened rota'on is necessary 4/1/11 CS202  Fundamental Structures of Computer Science II 17 AVL Trees
Dele5on
Case 1: The balance factor of p is equal. – Change the balance factor of p to right
high (or le<
high) – Shorter becomes false –p T1 T2 \ T1 p No rota5ons Height unchanged T2 deleted 4/1/11 CS202  Fundamental Structures of Computer Science II 18 AVL Trees
Dele5on
Case 2: The balance factor of p is not equal and the taller subtree is shortened. – Change the balance factor of p to equal – Shorter remains true / T1 p T2 –p T1 No rota5ons Height reduced T2 deleted 4/1/11 CS202  Fundamental Structures of Computer Science II 19 AVL Trees
Dele5on
Case 3: The balance factor of p is not equal and the shorter subtree is shortened. – Rota2on is necessary – Let q be the root of the taller subtree of p – We have three sub
cases according to the balance factor of q 4/1/11 CS202  Fundamental Structures of Computer Science II 20 AVL Trees
Dele5on
Case 3a: The balance factor of q is equal. – Apply a single rota2on – Change the balance factor of q to le<
high (or right
high) – Shorter becomes false \ h1 /
–q p Single rota5on Height unchanged q \ T1 deleted 4/1/11 p h
h T2 h T3 h1 CS202  Fundamental Structures of Computer Science II T1 h T3 T2 21 AVL Trees
Dele5on
Case 3b: The balance factor of q is the same as that of p. – Apply a single rota2on – Change the balance factors of p and q to equal – Shorter remains true p –q \
\
h1 p – T1
h1 deleted Single rota5on Height reduced 4/1/11 q T2 h T3 h1 T1 CS202  Fundamental Structures of Computer Science II h1 h T3 T2 22 AVL Trees
Dele5on
Case 3c: The balance factor of q is the opposite of that of p. – Apply a double rota2on – Change the balance factor of the new root to equal – Also change the balance factors of p and q – Shorter remains true \ p / h1
r T1 T2 4/1/11 p q T4
h1 deleted Double rota5on Height reduced q –r h1 h1 or
T3
h2 CS202  Fundamental Structures of Computer Science II T1 T2 h1 or
h2 T3 h1 T4 23 AVL Trees
Dele5on
Exercise: Delete o from the following AVL tree m
p e
c b
a j
d k h
g n i s
o l r u
t f Check the following applet for more exercises. hap://www.site.uoaawa.ca/~stan/csi2514/applets/avl/BT.html 4/1/11 CS202  Fundamental Structures of Computer Science II 24 AVL Trees
Analysis
H
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
30
40
50
4/1/11 minN
7
12
20
33
54
88
143
232
376
609
986
1.596
2.583
4.180
6.764
10.945
17.710
2.178.308
267.914.295
32.951.280.098 logN
2,81
3,58
4,32
5,04
5,75
6,46
7,16
7,86
8,55
9,25
9,95
10,64
11,33
12,03
12,72
13,42
14,11
21,05
28,00
34,94 H / logN
1,42
1,39
1,39
1,39
1,39
1,39
1,40
1,40
1,40
1,41
1,41
1,41
1,41
1,41
1,41
1,42
1,42
1,42
1,43
1,43 What is the minimum number of nodes in an AVL tree? minN(0) = 0 minN(1) = 1 minN(2) = 2 minN(3) = 4 ...
minN(h) = minN(h
1) + minN(h
2) + 1 Max height of an N
node AVL tree is less than 1.44 log N CS202  Fundamental Structures of Computer Science II 25 ...
View
Full Document
 Spring '11
 Pablo
 Computer Science, Computer Graphics, Computer Science II, the00

Click to edit the document details