0% found this document useful (0 votes)
2 views100 pages

Unit 2

This document covers advanced data structures, focusing on hierarchical data structures such as Binary Search Trees (BST), Red-Black Trees, and B-Trees. It details operations like insertion, deletion, and searching within these structures, along with their properties and complexities. Additionally, it introduces concepts like rotations and the importance of maintaining tree height for efficiency.

Uploaded by

Punitha Brawin
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views100 pages

Unit 2

This document covers advanced data structures, focusing on hierarchical data structures such as Binary Search Trees (BST), Red-Black Trees, and B-Trees. It details operations like insertion, deletion, and searching within these structures, along with their properties and complexities. Additionally, it introduces concepts like rotations and the importance of maintaining tree height for efficiency.

Uploaded by

Punitha Brawin
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 100

CP4151-ADVANCED DATA

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

► For simplicity, we will assume that all keys are distinct.


Mrs.P.Joy Suganthy Bai,AP/CSE Grace College of
Engineering
Binary Search Tree Operations
► Given a binary search tree, there are several operations we want to
perform.
◮ Insert an element
◮ Delete an element
◮ Search for an element
◮ Find the minimum/maximum element
◮ Find the successor/predecessor of a node.
► Once we see how these are done, it will be apparent that the complexity of
each of these is O(h), where h is the height of the tree.
► The insert and delete operations are the hardest to implement.
► Finding the minimum/maximum and searching are the easiest, so we will
start with these.

Mrs.P.Joy Suganthy Bai,AP/CSE Grace College of


Engineering
BST: Minimum/Maximum
► The minimum element is the left-most node.
► The maximum element is the right-most node of the tree.
► Here are implementations of these methods:
node Find_Min(x) { node Find_Max(x) {
while(x.left!=null) while(x.right!=null)
x=x.left; x=x.right;
return x; return x;
} }

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;
}

► The predecessor operation is symmetric to successor.


Mrs.P.Joy Suganthy Bai,AP/CSE Grace College of
Engineering
BST: Successor Argument
► So, why is it that if x has an empty right subtree, then y is
the lowest ancestor of x whose left child is also an
ancestor of x?
► Let’s look at it the other way.
► Let y be the lowest ancestor of x whose left child is also an
ancestor of x.
► What is the predecessor of y?
► Since y has a left child, it must be the largest element in
the tree rooted at y.left
► If x is not the largest element in the subtree rooted at
y.left, then some ancestor of x (in the subtree) is the left
child of its parent.
► But y, which is not in this subtree, is the lowest such node.
► Thus x is the predecessor of y, and y is the successor of x.
Mrs.P.Joy Suganthy Bai,AP/CSE Grace College of
Engineering
BST: Successor Examples 13

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

Mrs.P.Joy Suganthy Bai,AP/CSE Grace College of


Engineering
BST: Deletion

► Deleting a node z is by far the most difficult operation.


► There are 3 cases to consider:
 ◮ If z has no children, just delete it.
 ◮ If z has one child, splice out z. That is, link z’s parent and child.
 ◮ If z has two children, splice out z’s successor y, and replace the contents of z

with the contents of y.


► The last case works because if z has 2 children, then its successor has no left child.
Why?
► Deletion is made worse by the fact that we have to worry about boundary
conditions
► To simplify things, we will first define a function called
 SpliceOut.

Mrs.P.Joy Suganthy Bai,AP/CSE Grace College of


Engineering
BST: Splice Out
► Any node with at most 1 child can be “spliced out”.
► Splicing out a node involves linking the parent and child of
a node.
SpliceOut(T,y) {
//Two children--can’t splice out.
if(y.left!=null && y.right!=null) return;

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;

//Set y’s parent’s child to y’s child


if(y.parent==null) x=T.root;
else {
if(y==y.parent.left) y.parent.left=x;
else y.parent.right=x;
}
}
Mrs.P.Joy Suganthy Bai,AP/CSE Grace College of
Engineering
BST: SpliceOut Examples
13 13

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

Mrs.P.Joy Suganthy Bai,AP/CSE Grace College of


Engineering
BST: Deletion Algorithm
► Once we have defined the function SpliceOut, deletion
looks simple.
► Here is the algorithm to delete z from tree T .
Delete(T,z) {
if(z.left==null || z.right==null)
SpliceOut(T,z);
else {
y=Successor(z);
z.key=y.key;
SpliceOut(T,y);
}
}

Mrs.P.Joy Suganthy Bai,AP/CSE Grace College of


Engineering
BST: Deletion Examples 13 13

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).

Mrs.P.Joy Suganthy Bai,AP/CSE Grace College of


Engineering
Red-Black Trees
► A red-black tree is a BST with the following properties:
◮ Each node is colored either red or black.
◮ If a node is red, both its children are black.
◮ Every simple path from a node to a descendent leaf has the
same number of black nodes.
13

6 23

3 8 30

1 5 7 10 27 41

4 9 11

Mrs.P.Joy Suganthy Bai,AP/CSE Grace College of


Engineering
Red-Black Trees Fact and Terms
► The black-height of a node x is the number of black
nodes, not including x, on a path to any leaf.
► A red-black tree with n nodes has height at most
2 log(n + 1).
► Since red-black trees are binary search trees, all of the
operations that can be performed on binary search trees
can be performed on them.
► Furthermore, the time complexity will be the
same–O(h)–where h is the height.
► Unfortunately, insertion and deletion as defined for regular
binary search trees will not work for red-black trees. Why
not?
► Fortunately, insertion and deletion can both be modified so
that they work, and still have time complexity O(h).
Mrs.P.Joy Suganthy Bai,AP/CSE Grace College of
Engineering
Insert and Delete in RB Trees
► Inserting a node into a red-black tree is not trivial.
11 11
Insert(9)
7 13 7 13

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

► Similar things happen when we try to delete nodes.


► We will not discuss in depth these operations.
► We will discuss some of the concepts, however.
Mrs.P.Joy Suganthy Bai,AP/CSE Grace College of
Engineering
Red-Black Tree Insertion: Method
► To insert a node x into a red-black tree, we do the
following:
◮ Insert x with the standard BST Insert.
◮ Color x red.
◮ If x’s parent is red, fix the tree.
► Notice that x’s children, null, are black.
► Since we colored x red, we have not changed the black
height.
► The only problem we have is (possibly) having a red node
with a red child.
► Fixing the tree involves re-coloring some of the nodes and
performing rotations.

Mrs.P.Joy Suganthy Bai,AP/CSE Grace College of


Engineering
Left- and Right-Rotations
► Rotations are best defined by an illustration:
Right-Rotate(T,y)
y x

x A y
C
Left-Rotate(T,x)
A B B C

► Here, the letters A, B, and C represent arbitrary subtrees.


They could even be empty.
► It is not too hard to see that the binary search tree property
will still hold after a rotation.

Mrs.P.Joy Suganthy Bai,AP/CSE Grace College of


Engineering
Rotation Example
13
x
7 23

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

Mrs.P.Joy Suganthy Bai,AP/CSE Grace College of 5 9


Engineering
Insertion Example
11 11
Insert(T,10) 7 13
7 13

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

Mrs.P.Joy Suganthy Bai,AP/CSE Grace College of


Engineering
Red Black Tree Summary
► Red-black trees are binary search trees which have height
Θ(log n) guaranteed.
► The basic operations can all be implemented in time
O(log n).
► Although inserting and deleting nodes only requires time
O(log n), they are nonetheless not trivial to implement.
► A regular binary search tree does not guarantee time
complexity of O(log n), only O(h), where h can be as large
as n.
► Thus red-black trees are useful if one wants to guarantee
that the basic operations will take O(log n) time.

Mrs.P.Joy Suganthy Bai,AP/CSE Grace College of


Engineering
B-‐Trees

Mrs.P.Joy Suganthy Bai,AP/CSE Grace College of


Engineering
Problem: Data base too big to fit memory
Disk reads are slow

Example: 1,000,000 records on disk


Binary search might take
20 disk reads

Mrs.P.Joy Suganthy Bai,AP/CSE Grace College of


Engineering
Disk reads are done in blocks

Example: One block read can retrieve


100 records

Mrs.P.Joy Suganthy Bai,AP/CSE Grace College of Engineering


1,000,000 Records

Mrs.P.Joy Suganthy Bai,AP/CSE Grace College of


Engineering
Block 0 Block 1 Block 2 ............. …B.…o
l …ck…9…9.………………...………...…………. Block 9999

1,000,000 Records

Mrs.P.Joy Suganthy Bai,AP/CSE Grace College of


Engineering
10,000 Records

Block 0 Block 1 Block 2 ............. …B.…o


l …ck…9…9.………………...………...…………. Block 9999

1,000,000 Records

Mrs.P.Joy Suganthy Bai,AP/CSE Grace College of


Engineering
Block
Block 1 ................................... Block 99
0

10,000 Records

Block 0 Block 1 Block 2 ............. …B.…o


l …ck…9…9.………………...………...…………. Block 9999

1,000,000 Records

Mrs.P.Joy Suganthy Bai,AP/CSE Grace College of


Engineering
Block
0
100 Records

Block 0 Block 1 ................................... Block 99

10,000 Records

Block 0 Block 1 Block 2 ............. …B.…o


l …ck…9…9.………………...………...…………. Block 9999

1,000,000 Records

Mrs.P.Joy Suganthy Bai,AP/CSE Grace College of


Engineering
DEF: A B-‐Tree of order m is an m-‐way tree such that
1. All leaf nodes are at the same level.
2. All non-‐leaf nodes (except the root) have at most m
and at least m/2 children.
3. The number of keys is one less than the number of
children for non-‐leaf nodes and at most m-‐1 and at
least m/2 for leaf nodes.
4. The root may have as few as 2 children unless the
tree is the root alone.

Mrs.P.Joy Suganthy Bai,AP/CSE Grace College of


Engineering
Example for m = 5
DEF: A B-‐Tree of order 5 is an 5-‐way tree such that
1. All leaf nodes are at the same level.
2. All non-‐leaf nodes (except the root) have at most 5
and at least 2 children.
3. The number of keys is one less than the number of
children for non-‐leaf nodes and at most 4 and at
least 2 for leaf nodes.
4. The root may have as few as 2 children unless the
tree is the root alone.

Mrs.P.Joy Suganthy Bai,AP/CSE Grace College of


Engineering
Creating a B-‐tree of order 5

AGFBKDHMJESIRXCLNTUP

Mrs.P.Joy Suganthy Bai,AP/CSE Grace College of Engineering


Creating a B-‐tree of order 5

AGFBKDHMJESIRXCLNTUP

A B F G

Mrs.P.Joy Suganthy Bai,AP/CSE Grace College of


Engineering
Creating a B-‐tree of order 5

AGFBKDHMJESIRXCLNTUP

A B F G K

Mrs.P.Joy Suganthy Bai,AP/CSE Grace College of


Engineering
Creating a B-‐tree of order 5

AGFBKDHMJESIRXCLNTUP

A B G K

Mrs.P.Joy Suganthy Bai,AP/CSE Grace College of


Engineering
Creating a B-‐tree of order 5

AGFBKDHMJESIRXCLNTUP

A B D G H K M

Mrs.P.Joy Suganthy Bai,AP/CSE Grace College of


Engineering
Creating a B-‐tree of order 5

AGFBKDHMJESIRXCLNTUP

A B D G H J K M

Mrs.P.Joy Suganthy Bai,AP/CSE Grace College of


Engineering
Creating a B-‐tree of order 5

AGFBKDHMJESIRXCLNTUP

F J

A B D G H K M

Mrs.P.Joy Suganthy Bai,AP/CSE Grace College of


Engineering
Creating a B-‐tree of order 5

AGFBKDHMJESIRXCLNTUP

F J

A B D E G H I K M R S

Mrs.P.Joy Suganthy Bai,AP/CSE Grace College of


Engineering
Creating a B-‐tree of order 5

AGFBKDHMJESIRXCLNTUP

F J

A B D E G H I K M R S X

Mrs.P.Joy Suganthy Bai,AP/CSE Grace College of


Engineering
Creating a B-‐tree of order 5

AGFBKDHMJESIRXCLNTUP

F J R

A B D E G H I K M S X

Mrs.P.Joy Suganthy Bai,AP/CSE Grace College of


Engineering
Creating a B-‐tree of order 5

AGFBKDHMJESIRXCLNTUP

F J R

A B C D E G H I K M S X

Mrs.P.Joy Suganthy Bai,AP/CSE Grace College of


Engineering
Creating a B-‐tree of order 5

AGFBKDHMJESIRXCLNTUP

C F J R

A B D E G H I K M S X

Mrs.P.Joy Suganthy Bai,AP/CSE Grace College of


Engineering
Creating a B-‐tree of order 5

AGFBKDHMJESIRXCLNTUP

C F J R

A B D E G H I K L M N S T U X

Mrs.P.Joy Suganthy Bai,AP/CSE Grace College of


Engineering
Creating a B-‐tree of order 5

AGFBKDHMJESIRXCLNTUP

C F J R

A B D E G H I K L M N P S T U X

Mrs.P.Joy Suganthy Bai,AP/CSE Grace College of


Engineering
Creating a B-‐tree of order 5

AGFBKDHMJESIRXCLNTUP

C F J M R

A B D E G H I K L N P S T U X

Mrs.P.Joy Suganthy Bai,AP/CSE Grace College of


Engineering
Creating a B-‐tree of order 5

AGFBKDHMJESIRXCLNTUP

C F M R

A B D E G H I K L N P S T U X

Mrs.P.Joy Suganthy Bai,AP/CSE Grace College of


Engineering
Deleting
Nodes
• Delete E from leaf node

C F M R

A B D E G H I K L N P S T U X

Mrs.P.Joy Suganthy Bai,AP/CSE Grace College of


Engineering
Deleting
Nodes
• Delete E

C F M R

A B D G H I K L N P S T U X

Mrs.P.Joy Suganthy Bai,AP/CSE Grace College of


Engineering
Deleting
Nodes
• Borrow from a neighbor

C G M R

A B D F H I K L N P S T U X

Mrs.P.Joy Suganthy Bai,AP/CSE Grace College of


Engineering
Deleting
Nodes
• Delete F -‐-‐-‐ but can’t borrow from a neighbor

C G M R

A B D H I K L N P S T U X

Mrs.P.Joy Suganthy Bai,AP/CSE Grace College of


Engineering
Deleting
Nodes
Combine and push the problem up one level

C M R

A B D G H I K L N P S T U X

Mrs.P.Joy Suganthy Bai,AP/CSE Grace College of


Engineering
Deleting
Nodes
Can’t borrow so combine

C J M R

A B D G H I K L N P S T U X

Mrs.P.Joy Suganthy Bai,AP/CSE Grace College of


Engineering
Deleting
Nodes
Delete M from non-‐leaf node
Note: immediate predecessor in non-‐leaf
Is always in a leaf.

C J M R

A B D G H I K L N P S T U X

Mrs.P.Joy Suganthy Bai,AP/CSE Grace College of


Engineering
Deleting
Nodes
Delete M from non-‐leaf node

Overwrite M with immediate predecessor

C J L R

A B D G H I K N P S T U X

Mrs.P.Joy Suganthy Bai,AP/CSE Grace College of


Engineering
Deleting
Nodes
Borrow from a neighbor

C I L R

A B D G H J K N P S T U X

Mrs.P.Joy Suganthy Bai,AP/CSE Grace College of


Engineering
Mrs.P.Joy Suganthy Bai,AP/CSE Grace College of
Engineering
Mrs.P.Joy Suganthy Bai,AP/CSE Grace College of
Engineering
Mrs.P.Joy Suganthy Bai,AP/CSE Grace College of
Engineering
Mrs.P.Joy Suganthy Bai,AP/CSE Grace College of
Engineering
Mrs.P.Joy Suganthy Bai,AP/CSE Grace College of
Engineering
Mrs.P.Joy Suganthy Bai,AP/CSE Grace College of
Engineering
Mrs.P.Joy Suganthy Bai,AP/CSE Grace College of
Engineering
Mrs.P.Joy Suganthy Bai,AP/CSE Grace College of
Engineering
Mrs.P.Joy Suganthy Bai,AP/CSE Grace College of
Engineering
Mrs.P.Joy Suganthy Bai,AP/CSE Grace College of
Engineering
Mrs.P.Joy Suganthy Bai,AP/CSE Grace College of
Engineering
Mrs.P.Joy Suganthy Bai,AP/CSE Grace College of
Engineering
Mrs.P.Joy Suganthy Bai,AP/CSE Grace College of
Engineering
Mrs.P.Joy Suganthy Bai,AP/CSE Grace College of
Engineering
Mrs.P.Joy Suganthy Bai,AP/CSE Grace College of
Engineering
Mrs.P.Joy Suganthy Bai,AP/CSE Grace College of
Engineering
Mrs.P.Joy Suganthy Bai,AP/CSE Grace College of
Engineering
Mrs.P.Joy Suganthy Bai,AP/CSE Grace College of
Engineering
Mrs.P.Joy Suganthy Bai,AP/CSE Grace College of
Engineering
Mrs.P.Joy Suganthy Bai,AP/CSE Grace College of
Engineering
Mrs.P.Joy Suganthy Bai,AP/CSE Grace College of
Engineering
Mrs.P.Joy Suganthy Bai,AP/CSE Grace College of
Engineering
Mrs.P.Joy Suganthy Bai,AP/CSE Grace College of
Engineering
Mrs.P.Joy Suganthy Bai,AP/CSE Grace College of
Engineering
Mrs.P.Joy Suganthy Bai,AP/CSE Grace College of
Engineering
Mrs.P.Joy Suganthy Bai,AP/CSE Grace College of
Engineering
Mrs.P.Joy Suganthy Bai,AP/CSE Grace College of
Engineering
Mrs.P.Joy Suganthy Bai,AP/CSE Grace College of
Engineering
Mrs.P.Joy Suganthy Bai,AP/CSE Grace College of
Engineering
Mrs.P.Joy Suganthy Bai,AP/CSE Grace College of
Engineering
Mrs.P.Joy Suganthy Bai,AP/CSE Grace College of
Engineering
Mrs.P.Joy Suganthy Bai,AP/CSE Grace College of
Engineering
Mrs.P.Joy Suganthy Bai,AP/CSE Grace College of
Engineering
Mrs.P.Joy Suganthy Bai,AP/CSE Grace College of
Engineering
Mrs.P.Joy Suganthy Bai,AP/CSE Grace College of
Engineering
Mrs.P.Joy Suganthy Bai,AP/CSE Grace College of
Engineering
Mrs.P.Joy Suganthy Bai,AP/CSE Grace College of
Engineering
Mrs.P.Joy Suganthy Bai,AP/CSE Grace College of
Engineering
Mrs.P.Joy Suganthy Bai,AP/CSE Grace College of
Engineering

You might also like