Unit 2
Unit 2
STRUCTURES AND
ALGORITHMS
UNIT 2
UNIT II
9
HIERARCHICAL DATA
STRUCTURES
Binary Search Trees: Basics – Querying a Binary search tree – Insertion
and Deletion- Red Black trees: Properties of Red-Black Trees –
Rotations – Insertion – Deletion -B-Trees: Definition of B -trees – Basic
operations on B-Trees – Deleting a key from a B-Tree- Heap – Heap
Implementation – Disjoint Sets - Fibonacci Heaps: structure –
Mergeable-heap operations- Decreasing a key and deleting a node-
Bounding the maximum degree.
Mrs.P.Joy Suganthy Bai,AP/CSE Grace College of
Engineering
Binary Search Trees
► A Binary Search Tree is a binary tree with the following
properties: Given a node x in the tree
◮ if y is a node in the left subtree of x, then key[y] ≤ key[x].
◮ if y is a node in the right subtree of x, then key[x] ≤ key[y].
13
7 23
4 8 17
10
3 5 21
9
1
13
7 Maximum 23
4 8 17
10
3 5 21
9
1 Minimum
Mrs.P.Joy Suganthy Bai,AP/CSE Grace College of
Engineering
BST: Searching
► Search finds the node with value k in the tree rooted at x.
node Search(x,k) {
while(x!=null && k !=x.key) {
if(k<x.key)
x=x.left; else
x=x.right;
}
return x;
}
Search(x,10) x 13 Search(x,16)
7 23
4 8 17
10 NIL
3 5 21
9
1
Mrs.P.Joy Suganthy Bai,AP/CSE Grace College of
Engineering
BST: Successor/Predecessor
► Finding the Successor/Predecessor of a node is harder.
► To find the successor y of a node x (if it exists)
◮ If x has a nonempty right subtree, then y is the smallest
element in the tree rooted at x.right. Why?
◮ If x has an empty right subtree, then y is the lowest
ancestor of x whose left child is also an ancestor of x.
Clearly.
node Successor(x) {
if(x.right!=null)
return Find_Min(x.right);
y=p[x];
while(y!=null && x=y.right) {
x=y;
y=y.parent;
}
return y;
}
Successor(7)=8 7 23
4 Find_Min 8 17
10
3 5 21
9
1
2 13
Successor(10)=13 7 23
1
4 2 8 y 17
1
10 x
3 5 21
9
Mrs.P.Joy Suganthy Bai,AP/CSE Grace College of
1
Engineering
BST: Insertion
► First, search the tree until we find a node whose
appropriate child is null. Then insert the new node there.
► Below, T is the tree, and z the node we wish to insert.
Insert(T,z) {
node y=null;
x=T.root;
while(x!=null) {
y=x;
if(z.key<x.key)
x=x.left;
else
x=x.right;
}
z.parent=y;
if(y==null)
T.root=z;
else
if(z.key<y.key)
y.left=z;
else
y.right=z;
}
Mrs.P.Joy Suganthy Bai,AP/CSE Grace College of
Engineering
BST: Insertion Example
Insert(T,z) 13
7 23
4 8 17
y
3 5 10 21
1 9
6 22
z
Insert(T,z) 13
7 23
4 8 17
y
3 10 21
5
1 6 9
18 22
z
if(y.left!=null) x=y.left;
else if (y.right!=null) x=y.right;
else x=null;
if(x!=null) x.parent=y.parent;
7 23 SpliceOut(T,z) 7 23
4 8 17 4 8 17
10 10
3 5 z 21 3 21
9 9
1 1
13 13
7 23 7 23
SpliceOut(T,z)
4 8 z 17 4 10 17
10 9
3 5 21 3 5 21
9
1 1
13 z 7
SpliceOut(T,z)
7 4 8
4 8 10
3
10
3
7 23 7 23
Delete(T,z)
4 8 17 4 8 17
10 10
3 5 21 3 5 21
z 9
1 1
13 13
7 23 7 23
Delete(T,z)
4 8 17 4 8 17
10 z 9
3 5 21 3 5 21
9
1 1
13 13
z
7 23 8 23
Delete(T,z)
4 10 17 4 10 17
9 12 9 12
3 21 3 21
y 11
8 11 1
Mrs.P.Joy Suganthy Bai,AP/CSE Grace College of
1
Engineering
BST: Time Complexity
► We stated earlier, and have now seen, that all of the BST
operations have time complexity O(h), where h is the
height of the tree.
► However, in the worst-case, the height of a BST is O(n),
where n is the number of nodes.
► In this case, the BST has gained us nothing.
► To prevent this worst-case behavior, we need to develop a
method which ensures that the height of a BST is kept to a
minimum.
► Red-Black Trees are binary search trees which have
height Θ(log n).
6 23
3 8 30
1 5 7 10 27 41
4 9 11
3 8 24 3 8 24
1 4 1 4 9
Insert(10) ?
?
11 11
7 13 7 13
3 8 24 3 8 24
1 4 9 1 4 9
10 10
x A y
C
Left-Rotate(T,x)
A B B C
4 8 17
10
3 5
9
Right-Rotate(T,x)
13
4 23
7 17
3
5 8
10
9
Mrs.P.Joy Suganthy Bai,AP/CSE Grace College of
Engineering
Rotation Example
13
4 23
7 17
3
z
5 8
10
Left-Rotate(T,z)
13
4 23
8 17
3
7 10
3 8 24 3 8 24
x
1 4 9 1 4 9
10
Left-Rotation(T,x)
11 11
7 13 Recolor 7 13
3 9 24 3 9 24
1 4 8 10 1 4 8 10
1,000,000 Records
1,000,000 Records
10,000 Records
1,000,000 Records
10,000 Records
1,000,000 Records
AGFBKDHMJESIRXCLNTUP
AGFBKDHMJESIRXCLNTUP
A B F G
AGFBKDHMJESIRXCLNTUP
A B F G K
AGFBKDHMJESIRXCLNTUP
A B G K
AGFBKDHMJESIRXCLNTUP
A B D G H K M
AGFBKDHMJESIRXCLNTUP
A B D G H J K M
AGFBKDHMJESIRXCLNTUP
F J
A B D G H K M
AGFBKDHMJESIRXCLNTUP
F J
A B D E G H I K M R S
AGFBKDHMJESIRXCLNTUP
F J
A B D E G H I K M R S X
AGFBKDHMJESIRXCLNTUP
F J R
A B D E G H I K M S X
AGFBKDHMJESIRXCLNTUP
F J R
A B C D E G H I K M S X
AGFBKDHMJESIRXCLNTUP
C F J R
A B D E G H I K M S X
AGFBKDHMJESIRXCLNTUP
C F J R
A B D E G H I K L M N S T U X
AGFBKDHMJESIRXCLNTUP
C F J R
A B D E G H I K L M N P S T U X
AGFBKDHMJESIRXCLNTUP
C F J M R
A B D E G H I K L N P S T U X
AGFBKDHMJESIRXCLNTUP
C F M R
A B D E G H I K L N P S T U X
C F M R
A B D E G H I K L N P S T U X
C F M R
A B D G H I K L N P S T U X
C G M R
A B D F H I K L N P S T U X
C G M R
A B D H I K L N P S T U X
C M R
A B D G H I K L N P S T U X
C J M R
A B D G H I K L N P S T U X
C J M R
A B D G H I K L N P S T U X
C J L R
A B D G H I K N P S T U X
C I L R
A B D G H J K N P S T U X