Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
11 views50 pages

DAA Unit 2 Part 2

The document provides an overview of Binary Search Trees (BST) and Red-Black Trees, detailing their structures, properties, and operations such as insertion, deletion, and traversal methods. It explains the characteristics of BSTs, including their hierarchical organization and the rules governing node relationships, as well as the balancing properties and applications of Red-Black Trees. Additionally, it covers the mechanics of rotations used to maintain balance in Red-Black Trees during modifications.
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)
11 views50 pages

DAA Unit 2 Part 2

The document provides an overview of Binary Search Trees (BST) and Red-Black Trees, detailing their structures, properties, and operations such as insertion, deletion, and traversal methods. It explains the characteristics of BSTs, including their hierarchical organization and the rules governing node relationships, as well as the balancing properties and applications of Red-Black Trees. Additionally, it covers the mechanics of rotations used to maintain balance in Red-Black Trees during modifications.
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/ 50

Design and Analysis of Algorithms

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

ITERATIVE-TREE- SEARCH (x, k)


while x ≠ NIL and k ≠ key [k].
if k < key [x].
then x ← left [x].
else x ← right [x].
return x.
Binary Search Tree - Operations
TREE- MINIMUM (x)
While left [x] ≠ NIL.
x←left [x].
return x.
TREE- MAXIMUM (x)
While right [x] ≠ NIL.
x←right [x].
return x.
Binary Search Tree - Operations
Successor and predecessor
If all keys are specific, the successor of a node x is the node with the smallest key
greater than key[x].

TREE SUCCESSOR (x)


If right [x] ≠ NIL.
Then return TREE-MINIMUM (right [x])) // leftmost node in right subtree
else y←p [x] // find the lowest ancestor of x whose left child is an ancestor of x
While y ≠ NIL and x = right [y]
do x←y
y←p[y]
return y.
Binary Search Tree - Operations
Successor and predecessor
If all keys are specific, the successor of a node x is the node with the smallest key
greater than key[x].

TREE SUCCESSOR (x)


If right [x] ≠ NIL.
Then return TREE-MINIMUM (right [x])) // leftmost node in right subtree
else y←p [x] // find the lowest ancestor of x whose left child is an ancestor of x
While y ≠ NIL and x = right [y]
do x←y
y←p[y]
return y.
Binary Search Tree - Operations
TREE-INSERT (T, z)
y ←NIL.
x←root [T]
while x ≠ NIL.
do y←x
if key [z]< key [x]
then x←left [x].
else x←right [x].
p [z]←y
if y == NIL.
then root [T]←z
else if key [z] < key [y]
then left [y]←z
Else right [y]←z
Binary Search Tree - Operations
Insertion of a node
Steps:
• Start at the Root: Compare the new key with the current node's key.
• Decide Direction:
• If the new key is less than the current node's key, move to the left child.
• If the new key is greater than the current node's key, move to the right
child.
• Find Insertion Point: Continue the comparison until reaching a null (empty)
spot where the new node can be inserted.
• Insert the Node: Place the new node in the identified

• 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]

Inserting a New Key: 13


Objective: Insert the key 13 into the existing BST.
Steps:
1.Start at the Root (15):
•Compare 13 with 15.
•Since 13 < 15, move to the left child (10).
Example
At Node 10:
•Compare 13 with 10.
•Since 13 > 10, move to the right child (12).

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:

•Find the parent of node z.


•Update the parent's reference to z to be null, effectively deleting z.
Binary Search Tree - Deletion
Case 2. Delete a Node with Single Child in BST
A node with one child means that the node has either a left child or a right child
If z has exactly one child, replace z with its child. This involves connecting z's parent
directly to z's child, bypassing z.
Steps:
•Find the parent of node z.
•Determine whether z has a left or right child.
•Replace z with its child by updating the parent's reference to point to z's child.
Binary Search Tree - Deletion
Case 3. Delete a Node with both children in BST
A node with two children has both left and right subtrees
Steps:
• Find the in-order successor of z: The in-order successor of z is the node with the
smallest key that is larger than z's key. This node is the leftmost node in z's right subtree.
Let this successor node be y.
• Replace z's key with y's key: Instead of deleting z directly, replace its key with the key
of y
• Delete y: Now, delete y from the tree. Since y will always have at most one child (it is the
leftmost node in the right subtree), this reduces to one of the simpler cases above (Case 1
or Case 2).
Binary Search Tree - Deletion
Transplant Subroutine
• It is a helper function used when performing certain manipulations on Binary
Search Trees (BSTs), particularly when deleting nodes.
• It replaces one subtree with another subtree in a BST, adjusting the parent
pointers accordingly.

• The TRANSPLANT(T, u, v) operation replaces subtree rooted at node u with subtree


rooted at node v in the BST T.
• It modifies the parent of u to point to v, and if v is not null, it also updates v's
parent to point to u's parent.
Binary Search Tree - Deletion
Steps: TRANSPLANT(T, u, v):
1.If u is the root of the tree, update the if u.p == NIL
root of the tree to be v. T.root = v
else if u == u.p.left
2.If u is a left child of its parent, set u's u.p.left = v
parent’s left child to be v. else u.p.right = v
3.If u is a right child of its parent, set u's if v != NIL
parent’s right child to be v. v.p = u.p

4.If v is not null, update v's parent to point


to u's parent. •u.p refers to the parent of
node u.
•T.root is the root of the tree
T.
•NIL represents a null node.
Binary Search Tree - Deletion
TRANSPLANT operation by replacing node 30 with its right child 40

• u = 30 (the node to be replaced).


• v = 40 (the node that will take u's place).

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

Properties of a Red-Black Tree


A Red-Black Tree satisfies the following five properties:
1. Each node is either red or black.
2. The root is always black.
3. All leaves (NIL nodes) are black. These are "null" leaf nodes, represented as black.
4. If a node is red, then both of its children must be black.
1. This is often called the no two red nodes in a row rule, ensuring that red nodes are
never adjacent.
5. Every path from a given node to its descendant NIL nodes must have the same
number of black nodes.
• This ensures the tree is balanced in terms of black node count along all paths.
Red-Black Tree
Applications
• Database systems: Red-black trees are used for indexing and efficient data
retrieval.
• Network routing algorithms: Red-black trees are used for fast lookup and
efficient routing decisions.
• Language compilers: Red-black trees are used for symbol table management
and dependency resolution.
• Spell checkers: Red-black trees can be used to build a spell checker for searching
words in a dictionary
Red-Black Tree
Red-Black Tree
Rotations
• Rotations in a Red-Black Tree are tree restructuring operations that help
restore balance while preserving the binary search tree property. There are two
types of rotations: left rotation and right rotation.
1. Left Rotation (LEFT-ROTATE)
• A left rotation is performed on a node x to shift its right child y into x's position.
This is typically done when the right subtree is "too tall" and we want to balance
the tree.
2. Right Rotation (RIGHT-ROTATE)
• A right rotation is the mirror operation of a left rotation. It is performed on a node
y to shift its left child x into y's position.
Red-Black Tree
Left- Rotations
LEFT-ROTATE(T, x)
y = x.right // Set y to be x's right child
x.right = y.left // Turn y's left subtree into x's right subtree
if y.left ≠ NIL // if y’s left subtree is not empty . . .
y.left.parent = x // Set x as the parent of y's left child, if it exists
y.parent = x.parent // Link x's parent to y
if x.parent == NIL
T.root = y // If x was the root, set y as the new root
else if x == x.parent.left
x.parent.left = y // If x was a left child, make y the left child of x's parent
else
x.parent.right = y // If x was a right child, make y the right child of x's parent
y.left = x // Put x on y's left
x.parent = y // Make y the parent of x
Red-Black Tree
Before Left Rotation:
x is the node being rotated.
y is the right child of x.
Steps of a Left Rotation:
• Let y=x.right (i.e., the right child of x).
• Set x.right=y.left (If y.left≠NIL, set y.left.parent=x).
• Set y.parent=x.parent
• If x is the root of the tree, set the root to y.
• Otherwise, if x is the left child of its parent, set y as the left child of x.parent
• Else, set y as the right child of x.parent
• Set y.left=x
• Set x.parent=y
Red-Black Tree
Red-Black Tree
Right- Rotations
RIGHT-ROTATE(T, y)
x = y.left // Set x to be y's left child
y.left = x.right // Turn x's right subtree into y's left subtree
if x.right ≠ NIL
x.right.parent = y // Set y as the parent of x's right child, if it exists
x.parent = y.parent // Link y's parent to x
if y.parent == NIL
T.root = x // If y was the root, set x as the new root
else if y == y.parent.right
y.parent.right = x // If y was a right child, make x the right child of y's parent
else
y.parent.left = x // If y was a left child, make x the left child of y's parent
x.right = y // Put y on x's right
y.parent = x // Make x the parent of y
Red-Black Tree
Before Right Rotation:
y is the node being rotated.
x is the left child of y.
Steps of a Right Rotation:
• Let x=y.left (i.e., the left child of y).
• Set y.left=x.right(If x.right≠NIL, set x.right.parent=y)
• Set x.parent=y.parent
• If y is the root, set the root to x.
• Otherwise, if y is the right child of its parent, set x as the right child of
y.parent.
• Else, set x as the left child of y.parent.
• Set x.right=y
• Set y.parent=x
Red-Black Tree
Insertion
• Insertion in a Red-Black Tree is more complex than in a standard binary search
tree because it needs to maintain the Red-Black Tree's properties.
• These properties ensure that the tree remains balanced, which allows operations
like search, insert, and delete to run in O(log n) time.

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.

Step 2: Fix Red-Black Tree Properties


After inserting the new node, the Red-Black Tree properties may be violated.
Specifically, two consecutive red nodes may appear in the tree.
The goal now is to restore the tree's balance while maintaining the Red-Black Tree
properties.
There are three main cases to handle based on the color of the new node's parent
(p[z]) and the color of its uncle (u[z]).
Red-Black Tree
Case 2.1: The Parent of the Node z is Black
•In this case, no Red-Black properties are violated, and no further action is
necessary. The tree remains balanced.

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.

Initial Tree: Empty


Step 1: Insert 3
• Step 1: Insert 3 as the root (since the tree is empty).
• Step 2: The root must be black, so color 3 black.
• Tree->
Step 2: Insert 21
• Step 1: Insert 21 as the right child of 3 (since 21 > 3). Color 21 red.
• Step 2: No Red-Black violation occurs, as the parent (3) is black.
Red-Black Tree – Insertion Example
Step 3: Insert 32
• Step 1: Insert 32 as the right child of 21 (since 32 > 21). Color 32 red.
• Step 2: Now, 21 and 32 are both red, violating the Red-Black property (no red
node can have a red parent).

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

Initial Tree: Empty


1. Inserting 10
• BST Insertion: Insert 10 as the root node.
• Color: As per Red-Black Tree properties, the root must always be black.

No violations occur because the root is always black.

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.

Violation: Both 8 and 7 are red.


• Fix-up:
• Case 3 (Straight line, no uncle or uncle is black): Perform a right rotation around 9.
• Recolor 8 to black and 9 to red.
Red-Black Tree – Insertion Example
Final Tree
After inserting all the nodes in descending order from 10 to 1, the Red-Black Tree looks as
follows:

You might also like