Red-Black Tree: Insertion and Deletion Methods
Red-Black Tree: Insertion and Deletion Methods
Lecture-15
Red-Black Tree
Red-Black Tree
Binary Search Tree(BST)
A binary tree is said to be binary search tree if the
value at the left child is less than value at the parent
node and value at the right child is greater or equal
than value at the parent node.
Example:
Red-Black Tree
Definition
A red-black tree is a binary search tree that satisfies
the following red-black properties:
1. Every node is either red or black.
2. The root is black.
[Link] color of every leaf node is black and its value is
always NIL.
4. If a node is red, then both its children are black.
[Link] each node, all simple paths from the node to
descendant leaves contain the same number of black
nodes.
Red-Black Tree
Example:
Red-Black Tree
Sentinel
For a red-black tree T, the sentinel is an object with
the same attributes as an ordinary node in the tree.
Its color is black and its value is nil. It is represented
by [Link] or nil[T].
A Sentinel is a special dummy node that replaces all null pointers in a
black tree
Red-Black Tree
Red-Black tree without leaf node
Red-Black Tree
Black height of a node
Black height of a node x in the red-black tree is the
number of black nodes on any downward path from
node x to a leaf node but not including node x. It is
denoted by bh(x).
nil nil
Lecture-16
z 8 8
⇒
18 z
5 18
5 18
y ⇒
z 15
15
5 18
⇒
15
Insertion operation
Now, Insert next element 17.
8
8
5 18
5 18
y
y ⇒ 17
15
15
17 z z
8
5 17
⇒ 15
18
z
Insertion operation
Now, Insert next element 25.
8
8
z
5 17 ⇒ 5 17
18
18 15
15
y 25
z 25
Insertion operation
Now, Insert next element 40.
8
5 17
8
17 25
5 15
⇒
18
15 18 40
y
25 z
40
z
Insertion operation
Now, Insert next element 80.
8 8
17 17
5
25
5 y z
25
15 ⇒ 15
18 40 18 40
y
80 17 80
z 25 z
8
40
18
⇒ 5 15
80
Final red-black tree
Insertion Algorithm
Insertion Algorithm
Time complexity of
insertion algorithm is
θ(log n).
Question
Insert the following elements into
Red-Black tree:
10,18,7,15,16,30,25,40,60,2,1,70
Design and Analysis of Algorithms
Lecture-17
25 60
10
72
35 50
15 65 80
47
Deletion operation
Example: Consider the following red-black tree
47
32 71
z
87
65
82
93 y
47
32 71
32 71
93
z 65
65
87 ⇒
82
x=nil
93 y
82
x=nil
Deletion operation
Deletion of 32
47
47
y w
x=nil 71
32
z
71
⇒
93
93 65
x=nil 65
82
82
Deletion operation
Deletion of 32(continue)
71
71
⇒ 93
47 93 ⇒ x 47
x=nil 65 82
65 82
w 71
47 93
⇒
65 82
Deletion operation
Deletion of 71
71
z
82
93
47 ⇒ 47 93
65 82 y 65
Final tree
x=nil
Deletion operation
Example: Consider the following red-black tree
61
52 85
20 76 95
55
16
90 98
71 82
65
61 52 85
⇒
52 85 16 55
z 76 95
x
20 55 76 95 71 82 90 98
y x
16 65
71 82 90 98
x
65
Deletion operation
Deletion of 85
61
52
61
⇒ 90
52 85
z 16 55 76 95
16 55 76 95 71 x=nil
82 98
71 65 w
82 90 98
65
y
x=nil
Deletion operation
Deletion of 85(continue)
61
61
52 90
52 90 ⇒
16 55
16 55 76 95
76 95
⇒ x 71 82
71 82 98
98
65
65
Deletion operation
Deletion of 95
61
61 52 90
⇒
52 90 16 55 76
y 98
16 55 76 95 71 82
z
71 82 65
98
x
65
Deletion operation
Deletion of 98
61
61
52 90
52 90
⇒ w
16 55
y 76 x = nil
16 55 76 98
71 82
z
71 82 x = nil
65
65
Deletion operation
Deletion of 98(continue)
61
61
52 76
⇒ 52 76
⇒ x
16 55 90
71
16 55 90
71 82
65
82
65 x = nil
w Final red-black tree
AKTU EXAM. QUESTION
Lecture-18
at h ………………………………………………………………………………………………………..………. 2th-1(t-1)
B-Tree
Therefore, the minimum number of the keys in B-tree with
minimum degree t and height h
= 1+ 2(t-1) + 2t(t-1) + 2t2(t-1) + 2t3(t-1) + …….+2th-1(t-1)
= 1 + 2(t-1) ( 1 + t + t2 + t3 + ………… + th-1 )
= 1 + 2(t-1) (th−1)
(t−1)
= 1 + 2(th-1)
= 1 + 2th-2
= 2th-1
Now, since the number of keys in given B-tree is n, therefore
(𝑛 +1) (𝑛 +1)
2t -1 ≤ n ⇒ t ≤
h h ⇒ h≤ logt
2 2
It is proved.
B-Tree
Question: As a function of the minimum degree t , what is
the maximum number of keys that can be stored in a B-tree
of height h?
Solution: Consider following structure of B-tree with
maximum keys:-
h=0 ………..………………… ……. (2t-1)
2t-1
…………………………………………………………………………………………..………. (2t)3(2t-1)
at h …………………………………………………………………………………………..………. (2t)h(2t-1)
B-Tree
Therefore maximum number of keys in B-tree with minimum degree t
and height h
= (2t-1) + 2t (2t-1) + (2t)2(2t-1) + (2t)3(2t-1) + …………….. + (2t)h(2t-1)
= (2t-1) (1+2t + (2t)2+ (2t)3 + …………………..+ (2t)h )
((2t)h +1 −1)
= (2t-1)
(2t−1)
= (2t)h +1 −1
B-Tree
Note:
The simplest B-tree occurs when t = 2. Every internal node
then has either 2, 3, or 4 children, and we have a 2-3-4 tree.
In practice, however, much larger values of t yield B-trees
with smaller height.
B-Tree of order m
B-tree of order m is a tree which satisfies the following properties:
Each internal node's keys act as separation values which divide its
subtrees. For example, if an internal node has 3 child nodes (or
subtrees) then it must have 2 keys: a1 and a2. All values in the
leftmost subtree will be less than a1, all values in the middle
subtree will be between a1 and a2, and all values in the rightmost
subtree will be greater than a2.
Searching operation in B-Tree
Searching a B-tree is much like searching a binary search tree, except
that instead of making a binary, or “two-way,” branching decision at
each node, we make a multiway branching decision according to the
number of the node’s children.
Searching operation in B-Tree
B-TREE-SEARCH takes as input a pointer to the root node x
of a subtree and a key k to be searched for in that subtree.
The initial call of this algorithm will be
B-TREE-SEARCH([Link], k)
If k is in the B-tree, B-TREE-SEARCH returns the ordered
pair (y, i) consisting of a node y and an index i such that
[Link] = k. Otherwise, the procedure returns NIL.
Design and Analysis of Algorithms
Lecture-19
F K S
B-Tree
After inserting next element C, the B-tree will be
Q
C F K S
After inserting next element L , the B-tree will be
F Q
C K L S
C H K L S T V
B-Tree
After inserting next element W, the B-tree will be
F Q T
C H K L S V W
Q
F T
C H K L S V W
B-Tree
. After splitting and inserting M, B-tree will be
Q
F K T
C H L M S V W
Q
F K T
C H L M N R S V W
B-Tree
After inserting next element P, the B-tree will be
Q
F K M T
C H L N P R S V W
After splitting and inserting next element A, B and X, the B-tree will be
K Q
F M T
AB C H L N P R S V WX
B-Tree
After splitting and inserting next element Y, the B-tree will be
K Q
F M T W
AB C H L N P RS V X Y
After splitting and inserting next element D, the B-tree will be
K Q
B F M T W
A C D H L N P RS V X Y
B-Tree
After inserting next element Z and E, the B-tree will be
K Q
B F M T W
A C D E H L N P R S V X Y Z
Final B-Tree
B-Tree
Example: Create B-tree for the following elements with minimum
degree t = 3.
F, S, Q, K, C, L, H, T, V, W, M, R, N, P, A, B, X, Y, D, Z, E, G, I.
Example: Insert the following keys in a 2-3-4 B Tree:
40, 35, 22, 90, 12, 45, 58, 78, 67, 60
Example: Using minimum degree ‘t’ as 3, insert following sequence of
integers
10, 25, 20, 35, 30, 55, 40, 45, 50, 55, 60, 75, 70, 65, 80, 85 and 90
in an initially empty B-Tree. Give the number of nodes splitting
operations that take place.
B-Tree Insertion Algorithm
B-Tree Insertion Algorithm
Splitting the root with t = 4. Root node r splits in two, and a new root
node s is created. The new root contains the median key of r and has
the two halves of r as children. The B-tree grows in height by one when
the root is split.
B-Tree Insertion Algorithm
B-Tree Insertion Algorithm
Splitting a node with t = 4. Node y = [Link] splits into two nodes,
y and z, and the median key S of y moves up into y’s parent.
B-Tree Insertion Algorithm
Time complexity of
insertion algorithm
= O(th)
= O(t logtn)
Design and Analysis of Algorithms
Lecture-20
Final B-tree
Time complexity of deletion operation is O(th) i.e.
O(th) = O(t logt n)
AKTU Questions
1. Discuss the advantages of using B-Tree. Insert the following
Information 86, 23, 91, 4, 67, 18, 32, 54, 46, 96, 45 into an empty
B-Tree with degree t = 2 and delete 18, 23 from it.
2. Define a B-Tree of order m. Explain the searching operation in a
B-Tree.
3. Using minimum degree ‘t’ as 3, insert following sequence of
integers 10, 25, 20, 35, 30, 55, 40, 45, 50, 55, 60, 75, 70, 65, 80,
85 and 90 in an initially empty B-Tree. Give the number of nodes
splitting operations that take place.
4. Insert the following information F, S, Q, K, C, L, H, T, V, W, M, R, N,
P, A, B, X, Y, D, Z, E, G, I. Into an empty B-tree with degree t=3.
5. Prove that if n>=1, then for any n-key B-Tree of height h and
minimum degree t >=2, h<=log t ((n +1)/2).
6. Insert the following keys in a 2-3-4 B Tree: 40, 35, 22, 90, 12, 45,
58, 78, 67, 60 and then delete key 35 and 22 one after other.
Design and Analysis of Algorithms
Lecture-21
B3:
B4:
Properties of binomial trees
For the binomial tree Bk,
1. There are 2k nodes.
2. The height of the tree is k.
𝑘
3. There are exactly nodes at depth i for i = 0, 1, ..., k.
𝑖
4. The root has degree k, which is greater than that of any other
node. Moreover if the children of the root are numbered from left
to right by k−1, k−2, ..., 2, 1, 0, then child i is the root of a subtree
Bi.
Properties of binomial trees(cont.)
Proof: The proof is by induction on k. For each property, the
basis is the binomial tree B0. Verifying that each property
holds for B0 is trivial. For the inductive step, we assume that
all the properties holds for Bk−1.
1. Since binomial tree Bk consists of two copies of binomial
tree Bk−1, therefore
Number of nodes in Bk = 2k−1 + 2k−1 = 2.2k-1 = 2k
2. Since in Bk, one Bk-1 is child of the other Bk-1, therefore
the height of Bk = the height of Bk-1 +1
= (k-1) + 1
=k
Properties of binomial trees(cont.)
[Link] D(k, i) be the number of nodes at depth i of binomial
tree Bk. Since Bk is composed of two copies of Bk−1 linked
together, a node at depth i in Bk−1 appears in Bk once at depth
i and i in Bk is the number of nodes at depth i in Bk−1 plus the
number of nodes at depth i−1 in Bk−1. Thus,
D(k,i) = D(k-1,i) + D(k-1, i-1)
𝑘−1 𝑘−1
= +
𝑖 𝑖−1
𝑘
=
𝑖
Properties of binomial trees(cont.)
4. The only node with greater degree in Bk than in Bk−1 is the root,
which has one more child than in Bk−1. Since the root of Bk−1 has
degree k−1, therefore the root of Bk has degree k.
p[x]
Key[x]
Degree[x]
Child[x] Sibling[x]
Representation of binomial heaps
The roots of the binomial trees within a binomial heap are
organized in a linked list, which we refer to as the root list.
The degrees of the roots strictly increase as we traverse the
root list.
The sibling field has a different meaning for roots than for
non roots. If x is a root, then sibling[x] points to the next
root in the root list.
A given binomial heap H is accessed by the field head[H],
which is simply a pointer to the first root in the root list of
H. If binomial heap H has no elements, then head[H] = NIL.
Representation of binomial heaps
Example: Consider the following binomial heap:-
Lecture-22
Time Complexity:
Time complexity of BINOMIAL-HEAP-UNION is O(lg n).
Inserting a node
The following procedure inserts node x into binomial heap H,
assuming that x has already been allocated and key[x] has
already been filled in.
Time Complexity:
Time complexity of this algorithm is O(lg n).
Inserting a node into Binomial min Heap
Lecture-23
Solution: Step-1: Find the node with minimum key and remove that
node.
Extracting the node with minimum key
Step-2: Make the binomial heap H’ from children of minimum node.
Time Complexity:
Time complexity of this algorithm is O(lg n).
Decreasing a key
Example: Decrease the value of a node y to be 7.
Decreasing a
key
Decreasing a key
The following procedure decreases the key of a node x in a binomial
heap H to a new value k. It signals an error if k is greater than x’s current
key.
Time Complexity:
Time complexity of this algorithm is O(lg n).
Deleting a key
Following procedure is used to delete the key value of a node.
This implementation assumes that no node currently in the
binomial heap has a key of −∞.
Time Complexity:
Time complexity of this algorithm is O(lg n).
EXAMPLE
136
AKTU examination questions
1. Explain various properties of Binomial Tree.
2. Prove that maximum degree of any node in an n node binomial tree
is log n.
3. Explain the algorithm to extract the minimum elements in a
binomial Heap. Give an example for the same.
4. Define Binomial Heap with example.
5. Explain the algorithm to delete a given element in a binomial Heap.
Give an example for the same.
6. Explain properties of Binomial Heap. Write an algorithm to perform
uniting two Binomial Heaps. And also to find Minimum Key.
7. Explain the different conditions of getting union of two existing
binomial Heaps. Also write algorithm for union of two Binomial
Heaps. What is its complexity?
Design and Analysis of Algorithms
Lecture-24
Lecture-25
Step-2:
Extracting the minimum node
Step-3:
Step-4:
Extracting the minimum node
Step-5:
Step-6:
Extracting the minimum node
Step-7:
Step-8:
Extracting the minimum node
Step-9:
Step-10:
Extracting the minimum node
Step-11:
Step-12:
167
Decreasing a key
Case 1: The New Key is Greater Than or Equal to the Parent's Key
Action: If the new key value is still greater than or equal to the key
of its parent, the structure of the heap remains unchanged.
Example: Suppose you have a Fibonacci Heap where the node x has
a key of 20, and its parent has a key of 15. If you decrease the key of
x to 18 (still greater than or equal to 15), no restructuring is needed
because the heap property is still satisfied.
Case 2: The New Key is Less Than the Parent's Key (Violating the
Min-Heap Property)
Action: If the new key is less than the key of its parent, the node x is
cut from its parent and added to the root list. This operation breaks
the node away from its parent and makes it a root node.
Example: Suppose x has a key of 20, and its parent has a key of 15. If
you decrease the key of x to 10, this violates the min-heap property.
So, x is cut from its parent and added to the root list. 168
Decreasing a key
Sub-case: Cascading Cuts
When a node is cut and added to the root list, if its parent was
already marked (indicating a previous child was cut), the parent
must also be cut and added to the root list. This process continues
up the tree until an unmarked node or the root is reached.
Example: Consider a Fibonacci Heap with the following structure:
Node P (parent) with a key of 5 has a child node C with a key of
10, and node C has another child node x with a key of 20.
If node x is decreased to 3, it violates the heap property. Node x
is cut and moved to the root list.
If node C was already marked, it must also be cut and moved to
the root list, and this process repeats for Parent(P) if it is
marked.
169
Decreasing a key
Case 3: The Node is Already a Root Node
Action: If the node whose key is being decreased is already a root
node, the operation simply updates the key without any further
action.
Example: Suppose a root node has a key of 25, and you decrease it
to 15. Since it's already a root node, the structure remains intact,
and only the key value is updated.
Case 4: Updating the Minimum Node
After the decrease key operation, if the node with the decreased
key value becomes smaller than the current minimum node in the
heap, the pointer to the minimum node is updated to this node.
Example: If the current minimum node has a key of 10, and you
decrease another node’s key to 5, the minimum pointer is updated
to point to the node with the key value of 5.
170
Decreasing a key
Example: Consider following Fibonacci heap.
(1) Decrease the node with key 46 to key value 15.
(2) After this, decrease node with key 35 to key value 5.
Decreasing a key
Solution:
(1) Decrease the node with key 46 to key value 15.
Decreasing a key
2. Decrease the node with key 35 to key value 5.
Step-1:
Step-2:
Decreasing a key
Step-3: