Data Structure- Advanced Topics in Tree -
Hanyang University
Jong-Il Park
AVL, Red-Black, B-tree
Division of Computer Science and Engineering, Hanyang University
AVL TREE
Division of Computer Science and Engineering, Hanyang University
Non-random insertion to BST can produce unbalanced trees
• Can we rebalance the tree so that the tree always has O(log n) height?
• We need balance information for each node.
Balancing the root is not enough Balancing every node is too strict
Balanced binary trees
Division of Computer Science and Engineering, Hanyang University
AVL tree is a binary search tree.
Def: For every node in the tree, the heights of its left subtree and right subtree differ by at most 1. (the height of a null subtree is –1)
Is this condition enough to guarantee the height of an AVL tree with n nodes is O(log n)?
5
2
7
8
1 4
3
7
2 8
1 4
3 5
AVL tree
4
Division of Computer Science and Engineering, Hanyang University
N1 = 2 N2 = 4 N3 = N1 + N2 + 1 = 7N0 = 1
height of left=? height of right=?
AVL tree with Minimum Number of Nodes
Division of Computer Science and Engineering, Hanyang University
Smallest AVL tree of height 9
Smallest AVL tree of height 7
Smallest AVL tree of height 8
Smallest AVL tree of height 9
Division of Computer Science and Engineering, Hanyang University7
• Denote Nh the minimum number of nodes in an AVL tree of height h
• N0 = 1, N1 =2 (base)Nh= Nh-1 + Nh-2 +1 (recursive definition)
• N > Nh= Nh-1 + Nh-2 +1> 2 × Nh-2 > 4 × Nh-4 >…> 2i ×Nh-2i
• If h is even, let i = h/2 – 1. The equation becomes N > 2h/2-1N2 N > 2h/2-1 × 4 h = O(log N)
• If h is odd, let i = (h – 1)/2. The equation becomes N > 2(h-1)/2N1 N > 2(h-1)/2 × 2 h = O(log N)
• Thus, many operations (i.e. searching) on an AVL tree will take O(log N) time
Height of AVL tree
Division of Computer Science and Engineering, Hanyang University
Insertion routine is the same as BST, but if the resulting tree is unbalanced, we should rebalance it through rotating
• start from the node inserted, travel up the tree, and update balance info.
• if there’s a bad node, rotate to resume balance
• Rotating is a local operation and takes a constant amount of time
5
2
7
8
1 4
3
6
Insertion into AVL tree
8
Division of Computer Science and Engineering, Hanyang University
If the root of T is unbalanced after insertion of x,
• Single Rotation: if x < T->Left->Element (Rotate_Left) [LL] or if x > T->Right->Element (Rotate_Right) [RR]
k2
xy
z
k1
y z
k2
k1
x
Rotation
9
Division of Computer Science and Engineering, Hanyang University
If the root of T is unbalanced after insertion of y,
Does Single Rotation work?
k2
xy
z
k1
yz
k2
k1
x
Rotation
10
Division of Computer Science and Engineering, Hanyang University
• Double Rotation: if x > T->Left->Element (Rotate_Left) [LR] or if x < T->right->Element (Rotate_Right) [RL]
y
z
k3
k1
wk2
x
y
z
w
x
w x y z
w x y z
k3
k1
k2
k2
k1 k3
k2
k3 k1
Rotation
11
Division of Computer Science and Engineering, Hanyang UniversityFrom Wikipedia
Division of Computer Science and Engineering, Hanyang University
Single rotation
• Insert sequence: 3, 2, 1, 4, 5, 6, 7
Double rotation
• Insert sequence: 4, 2, 6, 1, 3, 5, 7, 16, 15, 14, 13, 12, 11
Another exercise
• Insert sequence: 2, 1, 4, 5, 9, 3, 6, 7
• Insert sequence: 3, 2, 1, 4, 5, 6, 7, 16, 15, 14, 13, 12, 11, 10, 8, 9
Homework: Rotation exercises
13
Division of Computer Science and Engineering, Hanyang University
struct AvlNode;typedef struct AvlNode *Position;typedef struct AvlNode *AvlTree;
struct AvlNode{
ElementType Element;AvlTree Left;AvlTree Right;int Height;
}
static int Height(Position P){
if (P == NULL) return –1;
elsereturn P->Height;
}
Code for Rotations
14
Division of Computer Science and Engineering, Hanyang University
SingleRotateWithLeft( Position K2 ){
Position K1;
K1 = K2->Left;K2->Left = K1->Right;K1->Right = K2;
K2->Height = Max( Height( K2->Left ), Height( K2->Right ) ) + 1;K1->Height = Max( Height( K1->Left ), K2->Height ) + 1;
return K1; /* New root */}
DoubleRotateWithLeft( Position K3 ){
K3->Left = SingleRotateWithRight( K3->Left );return SingleRotateWithLeft( K3 );
}
k2
xy
z
k1
y z
k2
k1
x
y
z
k3
k1
wk2
xw x y z
k2
k1 k3
Code for Rotations
15
Division of Computer Science and Engineering, Hanyang University
Insert( ElementType X, AvlTree T ){
if( T == NULL ) {T = malloc( sizeof( struct AvlNode ) );if( T == NULL )
FatalError( “Out of space!!!” );else{
T->Element = X; T->Height = 0;T->Left = T->Right = NULL;
}}
else if ( X < T->Element ) {T->Left = Insert( X, T->Left );if( Height( T->Left )–Height( T->Right ) == 2 )
if( X < T->Left->Element )T = SingleRotateWithLeft( T );
elseT = DoubleRotateWithLeft( T );
}
else if( X > T->Element ) {T->Right = Insert( X, T->Right );if( Height( T->Right )–Height( T->Left ) == 2 )
if( X > T->Right->Element )T = SingleRotateWithRight( T );
elseT = DoubleRotateWithRight( T );
}/* Height adjustment when inserted without
rotation */T->Height =
Max(Height( T->Left ), Height( T->Right ))+1;return T;
}
Code for insertion
16
Division of Computer Science and Engineering, Hanyang University
May need O(log n) rotations when deleting a node, in the worst case.
D
E
A
I
M
F
X G
KJH
B
C
L
Deletion from an AVL tree
17
Division of Computer Science and Engineering, Hanyang University
RED BLACK TREE
Division of Computer Science and Engineering, Hanyang University
Red black tree is a binary search tree in which every node is colored either red or black.
All properties are based on the extended binary search tree; each null pointer is replaced with an external node.
A pointer to a black child (including the external node) is black; a pointer to a red child is red.
Red black tree
19
Properties of colored nodes– root and all external nodes are black– no consecutive red node is on the root-to-external node path– all root-to-external node paths have the same number of black
nodes
65
80
70
50
6010
5 62
Division of Computer Science and Engineering, Hanyang University
Rank (black height) of a node is the number of black pointers on any path from the node to any external node
the rank of an external node is 0
Lemma 1Let the length of a root-to-external-node path be the number of
pointers on the path. If P and Q are two root-to-external-node paths in a red-black tree,length(P) ≤ 2 length(Q)
Proof: When r is the rank of the root, each root-to-external-node path has between r and 2r pointers
Red black tree
20
Division of Computer Science and Engineering, Hanyang University
Red black tree
21
65
80
70
50
6010
5 62
Division of Computer Science and Engineering, Hanyang University
If a new node is colored in black, we will have an extra black node on paths → require recoloring
If a new node is colored in red, we might have two consecutive red nodes → may or may not need recoloring
Insert first make a normal insert into a binary search tree color it with red fix red-black properties
Insertion
Division of Computer Science and Engineering, Hanyang University
LLr
LRr
Insertion
new node
new node
Division of Computer Science and Engineering, Hanyang University
LLb
LRb
Insertion
new node
new node
a
b
c
b
ac
a
b
c
a
c
b
c
ab
Division of Computer Science and Engineering, Hanyang University
Insertion
25
insert 70, 60, 65, and 62 in order.
50
8010
90
Division of Computer Science and Engineering, Hanyang University
We can delete a node with any external node in binary search tree→ delete a node without any child or with one child in the binary search tree
When the node with two internal nodes is deleted, find the node of its predecessor or successor and delete that node→ delete a node with both children in the binary search tree
Deletion
65
80
70
50
6010
60
80
70
50
10
delete 65 delete
Division of Computer Science and Engineering, Hanyang University
If a red node is deleted, no rebalancing is needed since the rank is not changed (property #3)
If a black node is deleted, rebalancing is needed→ color the edge double black
Deletion
Division of Computer Science and Engineering, Hanyang University
black sibling with a red child
Deletion: how to remove the double edge
- a node in blue can be either black or red - a node in dotted line can exist or not exist
p
v s
z
s
p z
v
p
v s
z
z
p s
v
a b
a b
Division of Computer Science and Engineering, Hanyang University
black sibling with black children (including any internal node)
Deletion: how to remove the double edge
29
p
v s
p
v s
p
v s
p
v s
propagate upward!
Division of Computer Science and Engineering, Hanyang University
red sibling→ restructure to have a black sibling
Deletion: how to remove the double edge
30
p
v s
a b
s
p b
v a
s
p b
av
Division of Computer Science and Engineering, Hanyang University
Homework: Deletion
31
6
8
7
4
52 9
delete 9, 8, 7
Division of Computer Science and Engineering, Hanyang University
B TREES
Division of Computer Science and Engineering, Hanyang University
Binary Trees are not quite appropriate for data stored on disks
– disk access is MUCH slower than memory access
– disk is partitioned into blocks (pages) and the access time of a word is the same as that of the entire block containing the word.
We have to reduce the number of disk accesses Make each node of the tree wider (multi-way search tree)
k
x < k x > k
k1 k2 k3
x < k1 k1<x<k2 k2<x<k3 k3 < x
p0 p1 p2p3
B-Trees
33
Division of Computer Science and Engineering, Hanyang University
B-Trees (Rudolf Bayer 1972)
34
Division of Computer Science and Engineering, Hanyang University
2-3 Tree
18 : –
13 : – 30 : 45
8 : 11 16 : – 22 : 23 41 : – 58 : 59
An Example B-Tree with m = 3
35
Division of Computer Science and Engineering, Hanyang University
Height of a B-Tree
36
Division of Computer Science and Engineering, Hanyang University
#define order 32
struct B_node {int n_child; /* number of children */B_node *child[order]; /* children pointers */int key[order-1]; /* keys */
}
Node Structure
37
Division of Computer Science and Engineering, Hanyang University
When we arrive at an internal node with key k1 < k2 ,... < km-1, search for x in this list (either linearly or by binary search)
• if you found x, you are done
• otherwise, find the index i such that ki < x < ki+1 (k0 = – and km = ),and recursively search the subtree pointed by pi.
Complexity = log m · logmn = O(log n)
Search
38
Division of Computer Science and Engineering, Hanyang University
Do a search to find the appropriate leaf into which to insert the node
• if the leaf is not full (has < m – 1 keys), simply insert it
• if the node overflows, restore the balance
(1) Key-Rotation: Check for Siblings for rotation
Key-Rotation is convenient but neither sufficient nor necessary
15 : 42
9 : -- 18 : 22 : 30
18 : 42
9 : 15 22 : 30
T1 T1
Insertion
39
Division of Computer Science and Engineering, Hanyang University
15 : 42
9 : -- 18 : 22 : 30
18 : 42
9 : 15 22 : 30
T1 T1– : 9 9 : 15
15 : 18
18 : 22
42: –
30 : 42
22 : 30
40
Insertion
Do a search to find the appropriate leaf into which to insert the node
• if the leaf is not full (has < m – 1 keys), simply insert it
• if the node overflows, restore the balance
(1) Key-Rotation: Check for Siblings for rotation
Key-Rotation is convenient but neither sufficient nor necessary
Division of Computer Science and Engineering, Hanyang University41
Insertion
Division of Computer Science and Engineering, Hanyang University
8 : 19
3 : – 12 : 16
1 : – 5 : – 9 : – 14 : – 17 : –
27 : 40
20 : 24 30 : 35 51 : 55
8 : 19
3 : – 12 : 16
1 : – 5 : – 9 : – 14 : – 17 : –
27 : 35 : 40
20 : 24 30 : 36 : 51 : 55
overflow!
overflow!
3 : – 12 : 16
1 : – 5 : – 9 : – 14 : – 17 : – 20 : 24 30 : 36 : 51 : 55
27 : 40 :
8 : 19 : 35
3 : – 12 : 16
1 : – 5 : – 9 : – 14 : – 17 : – 20 : 24 30 : 36 : 51 : 55
27 : 40 :
8 : – 35 : –
19 : –
insert(36)
42
Division of Computer Science and Engineering, Hanyang University
We still need to find a suitable replacement which is the largest key in the left child (or the smallest in the right) and move it to fill the hole. The replacement is always at the leaf level and creates a hole in a leaf node.
• if the leaf still has sufficient capacity, we are done.
• otherwise, node merging is performed.
43
Deletion
Division of Computer Science and Engineering, Hanyang University44
Merging
Division of Computer Science and Engineering, Hanyang University
8 : 19
3 : – 12 : 16
1 : – 5 : – 9 : – 14 : – 17 : –
27 : 40
20 : 24 30 : 35 51 : 55
8 : 19
3 : – 12 : 16
1 : – 9 : – 14 : – 17 : –
27 : 40
20 : 24 30 : 35 51 : 55
8 : 19
12 : 16
1 : 3 9 : – 14 : – 17 : –
27 : 40
20 : 24 30 : 35 51 : 55
12 : 19
8 : 16 :
1 : 3 14 : – 17 : –
27 : 40
20 : 24 30 : 35 51 : 559 : –
underflow! Node Merge
Key Rotation
delete(5)
45
Division of Computer Science and Engineering, Hanyang University
• All records are allowed to be stored only in the leaves.
• All non-leaf nodes include only the key values which can be found in the subtrees of the node.
• Leaf nodes can have a pointer to its next sibling so that a sequential access is possible.
25 30 50 55 60 65 75 80 85 905 10 15 20
25 50 75
Difference from Weiss textbook (B+ Tree)
46
Division of Computer Science and Engineering, Hanyang University
Practical Use of B-Tree
47
Top Related