DAA Unit 2 Part 3
DAA Unit 2 Part 3
(DAA)
Red Black Tree
Binary Search Tree
• A Binary Search tree is organized in a Binary Tree.
• Such a tree can be defined by a linked data structure in which a particular
node is an object.
• Each node in a Binary Search Tree has at most two children, a left child and
a right child, with the left child containing values less than the parent node
and the right child containing values greater than the parent node.
• Each node contains field left, right, and p that point to the nodes
corresponding to its left child, its right child, and its parent, respectively.
• If a child or parent is missing, the appropriate field contains the value NIL.
• The root node is the only node in the tree whose parent field is Nil.
• No Duplicate Keys: Typically, BSTs do not contain duplicate keys. Each key
must be unique within the tree.
• Recursive Property: Both the left and right subtrees must also be binary
search trees.
• This hierarchical structure allows for efficient searching, insertion,
and deletion operations on the data stored in the tree.
Binary Search Tree
Binary Search Tree - Property
Let x be a node in a binary search tree.
• If y is a node in the left subtree of x, then key [y] ≤key [x].
• If z is a node in the right subtree of x, then key [x] ≤ key [z].
Binary Search Tree - Operations
INORDER-TREE-WALK (x) - Running time is θ(n)
If x ≠ NIL
then INORDER-TREE-WALK (left [x])
print key [x]
INORDER-TREE-WALK (right [x])
PREORDER-TREE-WALK (x):
If x ≠ NIL.
then print key [x]
PREORDER-TREE-WALK (left [x]).
PREORDER-TREE-WALK (right [x]).
POSTORDER-TREE-WALK (x):
If x ≠ NIL.
then POSTORDER-TREE-WALK (left [x]).
POSTORDER-TREE-WALK (right [x]).
print key [x]
Binary Search Tree - Operations
TREE-SEARCH (x, k)
If x = NIL or k = key [x].
then return x.
If k < key [x].
then return TREE-SEARCH (left [x], k)
else return TREE-SEARCH (right [x], k)
• Note: If the BST is empty, the new node becomes the root.
Binary Search Tree - Operations
Example:
15, 10, 20, 8, 12, 17, 25.
[15]
/ \
[10] [20]
/\ /\
[8] [12] [17] [25]
At Node 12:
•Compare 13 with 12.
•Since 13 > 12, move to the right child, which is currently null.
•Insert the Node:
•Place the new node [13] as the right child of [12].
[15]
/ \
[10] [20]
/\ /\
[8] [12] [17] [25]
\
[13]
Binary Search Tree - Deletion
The overall strategy for deleting a node z from a binary search tree T has three basic
cases :
Case 1 : If z has no children, then simply remove it by modifying its parent to
replace z with NIL as its child.
Steps:
Steps:
•Find node u (30) and its parent (50).
•Find node v (40), which will replace node 30.
•Check whether node u is the root or a left/right child:
•Node 30 is the left child of node 50, so update 50's left child to point to node 40.
•Set v.p (the parent of 40) to point to u.p (node 50), so now 40's parent is 50.
•Node 40’s original left child remains intact (it still has its left child 20).
Binary Search Tree - Deletion
BST-DELETE(T, z)
if z.left == NIL and z.right == NIL # Case 1: No children (leaf)
TRANSPLANT(T, z, NIL)
else if z.left == NIL # Case 2: One child (right child)
TRANSPLANT(T, z, z.right)
else if z.right == NIL # Case 2: One child (left child)
TRANSPLANT(T, z, z.left)
else # Case 3: Two children
y = TREE-MINIMUM(z.right) # Find successor (minimum of right subtree)
if y.p != z # If y is not the immediate right child
TRANSPLANT(T, y, y.right) # Replace y with its right child
y.right = z.right # Link y to z's right child
y.right.p = y
TRANSPLANT(T, z, y) # Replace z with y
y.left = z.left # Link y to z's left child
y.left.p = y
Binary Search Tree - Deletion
Deleting a Leaf Node (20)
1.Find node 20:
•Start from the root (50), move left to 30, and then left again to 20.
2.Case 1 (Leaf node):
•Node 20 is a leaf node with no children.
•Simply remove node 20 by setting the left child of node 30 to null.
Binary Search Tree - Deletion
Deleting a Node with One Child (30)
delete node 30, which has only one child (node 40).
1.Find node 30:
•Start from the root (50), move left to 30.
2.Case 2 (One child):
•Node 30 has only one right child (40).
•Replace node 30 with node 40 using TRANSPLANT(T, 30, 40).
Binary Search Tree - Deletion
Deleting a Node with Two Children (70)
delete node 70, which has two children (60 and 80).
1.Find node 70:
•Start from the root (50), move right to 70.
2.Case 3 (Two children):
•Node 70 has two children (60 and 80).
•Find the in-order successor of 70, which is 80 (the smallest node in the right
subtree).
•Replace node 70 with its in-order successor 80:
•Node 80 has no left child, so replace 70 with 80.
•Then, set node 80's left child to be node 60.
Red-Black Tree
• A Red-Black Tree is a type of self-balancing Binary Search Tree (BST) with additional
properties that help maintain balance during insertions and deletions.
• A red-black tree is a binary search tree with one extra bit of storage per node: its color,
which can be either RED or BLACK
Algorithm Working:
The Red-Black Tree insertion operation follows these key steps:
• Insert the new node like a standard binary search tree (BST).
• Color the newly inserted node red.
• Fix any violations of the Red-Black Tree properties using rotations and recoloring.
Red-Black Tree
Insertion
RB-INSERT(T, z)
1. y = NIL(T) // node being compared with z
2. x = T.root // y will be parent of z
3. while x ≠ NIL(T) // descend until reaching the sentinel
4. y=x
5. if z.key < x.key
6. x = x.left
7. else
8. x = x.right
9. z.parent = y // found the location insert z with parent y
10. if y == NIL(T)
11. T.root = z // tree T was empty
12. else if z.key < y.key
13. y.left = z
14. else
15. y.right = z
16. z.left = NIL(T) // both of ´’s children are the sentinel
17. z.right = NIL(T)
18. z.color = RED // the new node starts out red
19. RB-INSERT-FIXUP(T, z) // correct any violations of red-black properties
Red-Black Tree
Relationship among nodes
Red-Black Tree
Insertion
Explanation:
• Steps 1-8: Perform a standard binary search tree (BST) insertion. Find the
appropriate location in the tree where the new node z should be inserted.
• Traverse the tree starting at the root x.
• Move left if the key of z is smaller than x's key, and right otherwise.
• Keep track of the parent node y.
• Steps 9-15: Insert z as a child of y.
• If y is NIL, it means z is the root.
• Otherwise, insert z as either the left or right child of y, based on their
keys.
• Steps 16-18: Initialize z's pointers to NIL (its left and right children) and
color z red.
• Inserting the new node as red helps to avoid immediately violating
property 5, which prohibits two consecutive red nodes.
• Step 19: After insertion, call RB-INSERT-FIXUP to fix any Red-Black Tree
property violations.
Red-Black Tree
Step 1: Perform a Standard BST Insertion
1.Insert the new node into the tree in the same way as you would in a binary search
tree:
•Traverse the tree and insert the node in its appropriate position based on its
key.
•The new node is always inserted as a leaf.
•Initially, set the color of the new node to red.
2.Let the inserted node be called z. If z is the root node, simply color it black and
the process is complete.
Case 2.2: The Parent of the Node z is Red, and the Uncle of z is Red
•Recoloring occurs in this case.
•Let p[z] be the parent of z, and let u[z] be the uncle (the sibling of p[z]).
•Both p[z] and u[z] are red, which violates the Red-Black property that no red node
can have a red parent.
•Fix:
1.Recolor p[z] and u[z] to black.
2.Recolor the grandparent (g[z]) of z to red.
3.Move up the tree: now consider g[z] as the new node z, and repeat the
process from Step 2.
•If the grandparent is the root, recolor it to black to maintain property 2 (root is
black).
Red-Black Tree
Case 2.3: The Parent of the Node z is Red, and the Uncle of z is Black or NIL
In this case, a rotation is required to restore the Red-Black properties. There are two
subcases based on whether z is a left or right child of p[z].
Case 2.3.a: z is a Right Child, and p[z] is a Left Child (Right Rotation)
•Fix:
1.Perform a left rotation on p[z]. This transforms the situation into Case 3b.
Case 2.3.b: z is a Left Child, and p[z] is a Left Child (Left Rotation)
•Fix:
1.Perform a right rotation on the grandparent g[z].
2.After the rotation, swap the colors of p[z] and g[z].
Similar steps apply if z is a left child and p[z] is a right child, or if z is a right child
and p[z] is a right child. Rotations and color changes restore the Red-Black
properties.
Red-Black Tree
RB-INSERT-FIXUP(T, z)
1. while z.parent.color == RED
2. if z.parent == z.parent.parent.left
3. y = z.parent.parent.right // y is z's uncle
4. if y.color == RED // Case 1: z's uncle is red
5. z.parent.color = BLACK // Recolor parent and uncle
6. y.color = BLACK
7. z.parent.parent.color = RED // Recolor grandparent
8. z = z.parent.parent // Move z up the tree
9. else
10. if z == z.parent.right // Case 2: z is a right child
11. z = z.parent
12. LEFT-ROTATE(T, z) // Perform left-rotate on z's parent
13. z.parent.color = BLACK // Case 3: Recolor and rotate
14. z.parent.parent.color = RED
15. RIGHT-ROTATE(T, z.parent.parent)
16. else (mirror image of the above steps)
17. T.root.color = BLACK // Ensure the root is black
Red-Black Tree
Red-Black Tree
Explanation of the Fixup Process:
The fixup process is divided into three cases, depending on the color of z's uncle y
and the position of z relative to its parent.
• Case 1: z's uncle y is red:
• If z's parent and uncle are both red, we recolor them black and make the
grandparent red.
• Then, we move z up to the grandparent and repeat the process.
• Case 2: z is a right child and y (uncle) is black:
• If z is the right child of its parent, we perform a left rotation on the parent.
• After the rotation, z becomes a left child, and we proceed to case 3.
• Case 3: z is a left child and y is black:
• We recolor the parent black and the grandparent red.
• Then, we perform a right rotation on the grandparent.
After handling all the cases, we ensure that the root is always black (property 2).
Red-Black Tree – Insertion Example
Insert the keys 3, 21, 32, and 15 into a Red-Black Tree (RB Tree) step by step.
Fix:
• The uncle of 32 (left child of 3) is black (or NIL), so a left rotation on 3 is
required.
• After the left rotation, 21 becomes the root, 3 becomes the left child of 21, and 32
remains the right child of 21.
• Recolor 21 to black and 3 to red to maintain Red-Black properties.
• After left rotation and recoloring:
Red-Black Tree – Insertion Example
Step 4: Insert 15
• Step 1: Insert 15 as the left child of 21 but to the right of 3 (since 15 > 3 but 15 <
21). Color 15 red.
• Step 2: No immediate violation occurs, as 15’s parent (3) is red, but the uncle (32) is
also red.
Fix:
• The parent of 15 (3) is red, and the uncle (32) is also red. This situation requires
recoloring:
• Recolor 3 and 32 to black.
• Recolor their parent (21) to red.
• After recoloring:
Red-Black Tree – Insertion Example
Now, 21 is red, but it is the root, and the root must always be black.
•Recolor 21 to black.
Red-Black Tree – Insertion Example
Perform insertion in red black tree : 10,9,8,7,6,5,4,3,2,1
2. Inserting 9
• BST Insertion: Insert 9 as the left child of 10.
• Color: New node is always inserted as red.
Red-Black Tree – Insertion Example
3. Inserting 8
• BST Insertion: Insert 8 as the left child of 9.
• Color: Inserted as red.
Violation: Both 9 and 8 are red, violating the property that no two consecutive red nodes can
appear.
• Fix-up: Case 3 (Straight line, no uncle or uncle is black): Perform a right rotation around 10.
• Recolor 9 to black and 10 to red.
Red-Black Tree – Insertion Example
4. Inserting 7
• BST Insertion: Insert 7 as the left child of 8.
• Color: Inserted as red.
• A double-black node in a red-black tree is a placeholder for a node that has a black
shortage.
• It occurs when a black leaf node is deleted, causing a reduction in black heights and
an imbalance in the tree
Red-Black Tree – Deletion
Case 1: w (sibling of x) is Red
Scenario:
•The sibling w is red.
•The parent p of x is black.
Fix:
1.Recolor w black and the parent p red.
2.Perform a left rotation on the parent p (if x is the left child of p) or a right rotation (if x is the
right child of p).
Purpose:
•By rotating and recoloring, we convert the tree into a situation where the sibling w is black,
making it easier to handle the remaining cases.
Action:
1.Recolor w black and p red.
2.Perform a left rotation around p if x is the left child or a right rotation if x is the right child.
After the rotation, the new sibling of x (the original child of w) will now be black, and you proceed
to fix-up using one of the remaining cases.
Red-Black Tree – Deletion
Case 2: w is Black, and Both of w's Children are Black
Scenario:
•The sibling w is black.
•Both of w's children (if any) are black.
Fix:
1.Recolor w red.
2.Move the double black problem up to the parent p (i.e., treat p as the new x).
Purpose:
•Recoloring w red reduces the black height on that subtree, but since x is still double black, you push
the problem upward.
•If p is black, the double black moves up, but if p is red, you can recolor p black and stop the fix-up.
Action:
1.Recolor w red.
2.Move the double black problem to p. Now, p is treated as the new x, and you repeat the process.
Result:
•If p was red, recolor it black and stop.
•If p was black, continue the fix-up by applying one of the cases to the new x.
Red-Black Tree – Deletion
Case 3: w is Black, w's Left Child is Red, and w's Right Child is Black
Scenario:
•The sibling w is black.
•The left child of w is red, and the right
child of w is black (or NIL).
Fix:
1.Recolor w's left child black and w red.
2.Perform a right rotation on w.
Purpose:
•This prepares for Case 4, where the sibling’s right child becomes red, and the double black can
be fixed by a final rotation.
Action:
1.Recolor w's left child black and w red.
2.Perform a right rotation around w.
Result:
•The original sibling w becomes red, and its child becomes black, transforming the situation into
Case 4, which can then be resolved.
Red-Black Tree – Deletion
Case 4: w is Black, and w's Right Child is Red
Scenario:
•The sibling w is black.
•The right child of w is red.
Fix:
1.Recolor w with the color of the parent p.
2.Recolor p black.
3.Recolor w's right child black.
4.Perform a left rotation on p.
Purpose:
•This removes the double black from x and balances the tree while maintaining Red-Black properties.
Action:
1.Recolor w with the color of p.
2.Recolor p black.
3.Recolor w's right child black.
4.Perform a left rotation on p.
Result:
•The double black is eliminated, and the Red-Black properties are restored.
•The tree is now balanced.
Red-Black Tree – Deletion
Summary of Fix-Up Steps:
1.Case 1: If the sibling w is red, rotate the parent and recolor.
2.Case 2: If w is black, and both children of w are black, recolor w red and move the
double black problem up.
3.Case 3: If w is black, its left child is red, and its right child is black, rotate and recolor
to transform the situation into Case 4.
4.Case 4: If w is black, and w's right child is red, rotate and recolor to resolve the
double black.
B-Tree
• B-trees are balanced search trees designed to work well on magnetic disks or other direct-
access secondary storage devices.
• When it comes to storing and searching large amounts of data, traditional binary search trees
can become impractical due to their poor performance and high memory usage.
• B-Trees, also known Balanced Tree, are a type of self-balancing tree that was specifically
designed to overcome these limitations.
• Unlike traditional binary search trees, B-Trees are characterized by the large number of keys
that they can store in a single node.
• Each node in a B-Tree can contain multiple keys, which allows the tree to have a larger
branching factor and thus a shallower height.
• This shallow height leads to less disk I/O, which results in faster search and insertion
operations.
• B-Trees are particularly well suited for storage systems that have slow, bulky data access such
as hard drives, flash memory, and CD-ROMs.
• B-Trees maintains balance by ensuring that each node has a minimum number of keys, so the
tree is always balanced.
• This balance guarantees that the time complexity for operations such as insertion, deletion,
and searching is always O(log n), regardless of the initial shape of the tree.
B-Tree - Properties
B-Tree - Properties
B-Tree - Properties
B-Tree - Operations
Searching
B-Tree - Operations
In this tree:
•The root node contains the keys [10, 20].
•The root has three children: the first child contains the keys
• [5, 7], the second child contains [12, 15], and the
third child contains [22, 30].
Search for Key 15 (Key found at the second level):
We will now search for key 15.
Step 1: Start at the Root Node [10, 20]
•The root node contains [10, 20].
•Compare the search key 15 with the keys in the root:
•First, compare 15 with 10: 15 > 10, so move to the next key.
•Next, compare 15 with 20: 15 < 20, so we move to the middle child (the subtree
rooted at [12, 15]).
B-Tree - Operations
Step 2: Move to the Middle Child [12, 15]
•Now, we are in the middle child node, which contains [12, 15].
•Compare 15 with the keys in this node:
•First, compare 15 with 12: 15 > 12, move to the next key.
•Next, compare 15 with 15: 15 == 15, so the search is successful.
Result:
•The key 15 was found in the middle child node [12, 15] at the second level of the B-
tree.
B-Tree - Operations
Step 2: Move to the Middle Child [12, 15]
•Now, we are in the middle child node, which contains [12, 15].
•Compare 15 with the keys in this node:
•First, compare 15 with 12: 15 > 12, move to the next key.
•Next, compare 15 with 15: 15 == 15, so the search is successful.
Result:
•The key 15 was found in the middle child node [12, 15] at the second level of the B-
tree.
B Tree - Insertion
• Inserting a node in a B-Tree is a bit more complex than in binary search trees due to its nature
of having multiple children and keys in a node.
• The process ensures that the properties of the B-Tree (balanced and sorted structure) are
maintained after every insertion.
The process is as follows:
• If the B-Tree is empty:
• Allocate a root node, and insert the key.
• If the B-Tree is not empty:
• Find the proper node for insertion.
• If the node is not full:
• Insert the key in ascending order.
• If the node is full:
• Split the node at the median.
• Push the median key upward, and make the left keys a left child node and the right
keys a right child node.
B Tree - Insertion
B Tree - Insertion
B Tree - Insertion
In the B-TREE-SPLIT-CHILD(x, i) algorithm, both x and i have specific roles related to the B-tree
structure:
Here:
•x is the parent node containing the keys [N, W].
•i = 1 refers to the second child of x, which is full and will be split.
•The child node y = x.c[1] (the second child of x) will be split, and the middle key S will be moved
up to x.
B Tree - Insertion
Steps of B-TREE-SPLIT-CHILD
In this case, i = 1 because y is the second child of x.
2. Let y = x.c[1]
y is the second child of x, and it contains the keys [P, Q, R, S, T, U, V].
Binomial trees
• The binomial tree Bk is an ordered tree defined recursively.
• The binomial tree B0 consists of a single node.
• The binomial tree Bk consists of two binomial trees Bk-1 that are linked together: the root of
one is the leftmost child of the root of the other.
Binomial Heap - Introduction
Binomial Heap - Introduction
Properties of Binomial Tree