Balanced Trees Balanced Trees
Balanced TreesBalanced Trees
Maintaining BalanceMaintaining Balance
Binary Search Tree– Height governed by
Initial order Sequence of insertion/deletion
– Changes occur at leaf nodes Need a structure that tends to maintain balance
– How? Grow in ‘width’ first, then height Accommodate horizontal growth More data at each level Nodes of two forms
– One data member and two children (“Two node”)– Two data members and three children (“Three node”)
2-3 Tree Nodes2-3 Tree Nodes
> L< S
S
> S< S>S <L
S L
BST InsertionBST Insertion60
30
5010
90
10080
704020
39
60
30
5010
90
10080
704020
39
39
60
30
5010
90
10080
704020
2-3 Tree Insertion (39)2-3 Tree Insertion (39)
70 90
60 10080
30
10 20
50
40
70 90
60 10080
30
10 20
50
39 40
2-3 Tree Insertion (38)2-3 Tree Insertion (38)30
10 20 39 4030
10 20 38 39 40
3810 20
30 39
40
2-3 Tree Insertion (37)2-3 Tree Insertion (37)
70 90
60 10080
50
3810 20
30 39
40
10 20
30 39
4037 38
50
70 90
60 10080
2-3 Tree Insertion (36)2-3 Tree Insertion (36)
10 20
30 39
4037 38
70 90
60 10080
50
2-3 Tree Insertion (36)2-3 Tree Insertion (36)
10 20
30 39
4037 38 10 20
30 39
4036 37 38
10 20
30 37 39
403836
50 37 50
10 20
30
36
39
4038
70 90
60 10080
2-3-4 Trees, Red-Black 2-3-4 Trees, Red-Black TreesTrees
2-3 Tree Insertion (38 – 2-3 Tree Insertion (38 – Leaf Node)Leaf Node)30
10 20 39 4030
10 20 38 39 40
3810 20
30 39
40
Generalized Insertion – Generalized Insertion – Leaf NodeLeaf Node
P
S M L e
M P
S eL
2-3 Tree Insertion (36 2-3 Tree Insertion (36 Internal Node)Internal Node)
10 20
30 39
4037 38
70 90
60 10080
50
2-3 Tree Insertion (36)2-3 Tree Insertion (36)
10 20
30 39
4037 38 10 20
30 39
4036 37 38
10 20
30 37 39
403836
50 37 50
10 20
30
36
39
4038
70 90
60 10080
Generalized InsertionGeneralized Insertion,, Internal NodeInternal Node
P
S M L e
cba d
e
cba d
LS
M P
2-3-4 Trees2-3-4 Trees If allowing
– 2 values/node– 3 children/node is better…
What about– 3 values/node– 4 children/node ???
Definition
S M L
> L< S >S <M >M <L
>L<S >S<L
S L
S
>S<S
–See 2-3 Tree
“3-Node”
…AND…
Insertion in 2-3-4 TreeInsertion in 2-3-4 Tree
Same scheme as 2-3 Tree– Traverse down tree– If node is 4-node
Split tree Insert into new tree
– This means what in terms of… Growth in Height versus Width? How far ‘up’ will insertion cascade?
Insertion in 2-3-4 Tree (20)Insertion in 2-3-4 Tree (20)
10 30 60
20
10
30
60 10 20
30
60
Insertion in 2-3-4 Tree (70)Insertion in 2-3-4 Tree (70)
10 20
30
40 50 60
10 20
30 50
40 60
10 20
30 50
40 60 70
70
Generalized Insertion in 2-Generalized Insertion in 2-3-4 Tree3-4 Tree
P
S M L e
cba d
e
cba d
LS
M P
Splitting a 4-Node with a 2-Node Parent
P
S M L e
cba d
Generalized Insertion in 2-Generalized Insertion in 2-3-4 Tree3-4 Tree
Splitting a 4-Node with a 3-Node Parent
f
cba d
P Q
S M L ee
cba d
M P Q
LS ff
cba d
P Q
S M L e
Binary Search Tree Binary Search Tree RepresentationRepresentation
Color-code node-type information– 2-Node
Ordinary binary search tree node
– 4-Node with values A, B, and C Center B is Black Children A and C are Red
– 3-Node with values A, and B (two choices) A is Black parent, B is Red right child
– or – B is Black parent, A is Red left child
– Ignore color-code Structure of binary search tree
Two ways to change tree– Color change– Rotations
B
A C
S T U V
Red-Black Tree Red-Black Tree RepresentationRepresentation
A B C
S T U V
4-Node
Red-Black Tree Red-Black Tree RepresentationRepresentation
A B
S T U
B
A U
S T
A
S B
T U
3-Node
Red-Black Tree TranslationRed-Black Tree Translation10
8 20
12 40
301541
3 9
8 10 20
1 3 4 12 15 30 409
Red-Black Tree InsertionRed-Black Tree Insertion
B
A C
S T U V
A B C
S T U V
Parent (root) is 4-NodeB
A C
S T U V
B
A C
S T U V
2-3
-4 R
ep
res
en
tati
on
Re
d-B
lac
k R
ep
res
en
tati
on
Red-Black Tree InsertionRed-Black Tree InsertionParent is 2-Node
2-3
-4 R
ep
res
en
tati
on
Re
d-B
lac
k R
ep
res
en
tati
on
P
S M L e
cba d
e
cba d
LS
M P
M
S L
A B C D
P
E M
S L
A B C D
P
E
Red-Black Tree InsertionRed-Black Tree InsertionParent is 3-Node (version 1 of 2)
2-3
-4 R
ep
res
en
tati
on
Re
d-B
lac
k R
ep
res
en
tati
on
e
cba d
M P Q
LS ff
cba d
P Q
S M L e
M
S L
A B C D
P
Q
FE
M
S L
A B C D
P
Q
FE
Red-Black Tree InsertionRed-Black Tree InsertionParent is 3-Node (version 2 of 2) - Rotation
M
S L
A B C D
P
Q
FEM
S L
A B C D
P
Q
F
Ef
cba d
P Q
S M L e
Adelson-Velskii and Landis Adelson-Velskii and Landis (AVL)(AVL)
30
10
20
10 30
20
40
10 30
20
AVL TreeAVL Tree Binary tree For every node x, define its
balance factor
balance factor of x =
height of left subtree of x - height of right subtree of x
Balance factor of every node x is -1, 0, or 1
The height of an AVL tree that has n nodes is at most 1.44 log2 (n+2).
The height of every n node binary tree is at least log2 (n+1).
0 0
0 0
1
0
-1 0
1
0
-1
1
-1
AVL Search TreeAVL Search Tree
0 0
0 0
1
0
-1 0
1
0
-1
1
-110
7
83
1 5
30
40
20
25
35
45
60
insert(9)insert(9)
0 0
0 0
1
0
-1 0
1
0
-1
1
-1
90
-1
0
10
7
83
1 5
30
40
20
25
35
45
60
insert(29)insert(29)
0 0
0 0
1
0
0
1
0
-1
1
-110
7
83
1 5
30
40
20
25
35
45
60
29
0
-1
-1-2
RR imbalance => new node is in right subtree of right subtree of blue node (node with bf = -2)
insert(29)insert(29)
0 0
0 0
1
0
0
1
0
-1
1
-110
7
83
1 5
30
40
2535
45
600
RR rotation. 20
029
AVL RotationsAVL Rotations
Single Rotation– RR– LL
Double Rotation– RL
new node in left subtree of right subtree Go right down and then go left down
– LR
LL Rotation AlgorithmLL Rotation AlgorithmBinaryNode LL_Rotate ( BinaryNode k2) {
BinaryNode k1 = k2.left;k2.left = k1.right;k1.right = k2;return k1;
}
RR Rotation AlgorithmRR Rotation AlgorithmBinaryNode RR_Rotate ( BinaryNode k1 ) {
BinaryNode k2 = k1.right;k1.right = k2.left;k2.left = k1;return k2;
}
Single Rotation Running Single Rotation Running ExampleExample
Consider the formation of an AVL tree by inserting the keys 1, 2, 3, 4, 5, 6 and 7 sequentially
1
0
-1
-21
2
3
1
20
2
13
0
2
13
0
4
5
2
14
5
3
Single Rotation (2)Single Rotation (2)
Consider the formation of an AVL tree by inserting the keys 1, 2, 3, 4, 5, 6 and 7 sequentially
2
14
5
3
6
2
1
4
5
3 6
-2
Why Double Rotation?Why Double Rotation?Single Rotation cannot solve LR or RL
LR Double RotationLR Double RotationBinaryNode LR_doubleRotate ( BinaryNode k3 ){
k3.left = RR_Rotate ( k3.left );return LL_Rotate ( k3 );
}
LR Double Rotation LR Double Rotation Example Example
RL Double RotationRL Double RotationBinaryNode RL_doubleRotate ( BinaryNode k1 ) {
k1.right = LL_Rotate ( k1.right );return RR_Rotate ( k1 );
}
More Double Rotation More Double Rotation ExampleExample
Consider the insertion of 15, 14, 13 into the AVL tree built by inserting 1, ...,7 sequentially
2
1
4
6
3 75
15
14
2
1
4
6
3 145
157
Double Rotation Example Double Rotation Example (2)(2)
Consider the insertion of 15, ..., 10, 9, 8, and 8.5 into the AVL tree built by inserting 1, ...,7 sequentially
2
1
4
6
3145
157
13
2
1
4
7
3146
15135
Single Left RotationSingle Left Rotation
50
10 40
20
60
30
50
10
40
20
6030
Double Rotation (Step Double Rotation (Step One)One)
50
10
40
20
6030
3525
22
50
20
40
30
6035
2510
22
Double Rotation (Step Double Rotation (Step Two)Two)
50
20
40
30
6035
2510
22
40
10
30
20
503525
22 60
B-TreesB-Trees
Binary tree is still not efficient enough for searching
A perfectly balanced binary tree has 5 levels for 25 nodes, while a 5-ary tree has only 3 levels
Especially useful for external usage
B-treeB-tree A B-tree of order m has the following properties: The data items are stored at the leaves in order The root is either a leaf or has 2 to m children All non-leaf nodes (except the root) have m/2 to m children Each non-leaf node has at most m-1 keys: key i is the data value
in subtree i+1 All leaves are at the same depth and have L/2 to L items
B-tree Insertion - SplitB-tree Insertion - Split L and M are determined by the sizes of a node, the size of an item
and the size of a key Requiring nodes to be half full guarantees that the B-tree will be
pretty balanced and will not degenerate into a simple binary tree, or even into a linked list
A B-tree of order 3 is also known as a 2-3 tree Insertion of 55 in the example B-tree causes a split into two leaves
B-tree Insertion - B-tree Insertion - Cascaded SplitCascaded Split Further insertion of 40 in the B-tree causes a split into
two leaves and then a split of the parent node
B-tree DeletionB-tree Deletion Deletion is done similarly If the number of items in a leaf falls below the
minimum, adopt an item from a neighboring leaf If the number of items in the neighboring leaves are
also minimum, combine two leaves. Their parent will then lose a child and it may need to be combined with its neighbor
This combination process should be recursively executed up the tree until:– Getting to the root– A parent has more than the minimum number of children