0% found this document useful (0 votes)
12 views32 pages

Advanced Data Structures

The document provides an overview of advanced data structures, specifically focusing on Min-Max Heaps, Leftist Trees, and Hash Tables. It describes the properties, operations, and applications of these structures, emphasizing their efficiency in various computational tasks. Additionally, it highlights the advantages and disadvantages of each data structure, particularly in relation to performance and use cases.

Uploaded by

srilalithas19
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)
12 views32 pages

Advanced Data Structures

The document provides an overview of advanced data structures, specifically focusing on Min-Max Heaps, Leftist Trees, and Hash Tables. It describes the properties, operations, and applications of these structures, emphasizing their efficiency in various computational tasks. Additionally, it highlights the advantages and disadvantages of each data structure, particularly in relation to performance and use cases.

Uploaded by

srilalithas19
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/ 32

ADVANCED DATA STRUCTURES

I. MIN-MAX HEAP (Continuation is in book)


What is Max Heap Data Structure?
In a Max-Heap:
• Each parent node has a value that is greater than or equal to its children.
• If you move up the tree, the values increase.
• If you move down the tree, the values decrease.
• The largest value is always at the root (top of the heap).

What is Min Heap Data Structures?


In a Min-Heap:
• Each parent node has a value that is less than or equal to its children.
• If you move up the tree, the values decrease.
• If you move down the tree, the values increase.
• The smallest value is always at the root (top of the heap).
II. HEAP STRUCTURES
A heap is a special type of binary tree where every level is fully filled, except maybe the last
one, which is filled from left to right.
In a heap:
• Each node follows a rule called the heap property:
o In a Max-Heap, the parent node is always bigger than its children.
o In a Min-Heap, the parent node is always smaller than its children.
• Each node contains a value, called a key, which determines its position in the heap.
Visualization of complete binary tree

Examples of In-complete binary tree


Examples of complete binary tree

There are two types of heap data structures: min heap and max heap
What is Max Heap Data Structure?
In a Max-Heap:
• Each parent node has a value that is greater than or equal to its children.
• If you move up the tree, the values increase.
• If you move down the tree, the values decrease.
• The largest value is always at the root (top of the heap).
What is Min Heap Data Structures?
In a Min-Heap:
• Each parent node has a value that is less than or equal to its children.
• If you move up the tree, the values decrease.
• If you move down the tree, the values increase.
• The smallest value is always at the root (top of the heap).

Array Implementation of Heap


A binary heap can be stored in an array, and the parent-child relationships are determined by
the indices in the array:
• The root of the heap is stored at A[0].
• For any element at index i:
o The left child is at A[2i + 1] (if it exists, i.e., 2i+1< n).
o The right child is at A[2i + 2] (if it exists, i.e., 2i+2<n).
o The parent is at A[(i - 1) / 2].
This simple formula makes it easy to navigate a heap stored in an array.
In a heap, using an array is space-efficient because it avoids the need for extra pointers per
node. Operations can also be optimized with bitwise operators.
For a Max-Heap:

• Each parent node satisfies:


A[i]≥A[2i+1] and A[i]≥A[2i+2] (if the children exist, i.e., their indices are less than n).
• The largest value is always at the root, stored in A[0].
For a Min-Heap:

• Each parent node satisfies:


A[i]≤A[2i+1] and A[i]≤A[2i+2] (if the children exist, i.e., their indices are less than n).
• The smallest value is always at the root, stored in A[0].
Operations Supported by Min-Max Heap
Various operations supported by max heap is described below on a high level and will be
covered in more detail in subsequent articles:
1. maxHeapify(A[], i):
o Fixes the heap when a node at iii disrupts it.
o Rearranges elements to maintain the max-heap property.
o Time Complexity: O(logn).
2. buildMaxHeap(A[]):
o Converts an array into a max-heap.
o Starts from the bottom nodes and uses maxHeapify.
o Time Complexity: O(n).
3. findMax(heap[]):
o Returns the maximum value at the root (index 0).
o Time Complexity: O(1).
4. extractMax(A[]):
o Removes and returns the maximum (root).
o Replaces the root with the last element and fixes the heap.
o Time Complexity: O(logn).
5. increaseKey(A[], i, v):
o Removes and returns the maximum (root).
o Replaces the root with the last element and fixes the heap.
o Time Complexity: O(logn).
6. insertKey(A[], v):
o Adds a new value v at the end.
o Moves the value up to maintain the heap.
o Time Complexity: O(logn)
7. deleteKey(A[], i):
o Deletes the value at iii.
o Replaces it with the last element and fixes the heap.
o Time Complexity: O(logn).
In a similar fashion min-heap support these operations:

• minHeapify(A[], i)
• buildMinHeap(A[])
• find Min(A[])
• extractMin(A[])
• decreaseKey(A[], i, v)
• insertKey(A[], v)
• deleteKey(A[], i)
Applications of Heap Data Structures
1. Priority Queues:
• Heaps are commonly used to implement priority queues where higher-priority
elements are processed first.
• Applications: Task scheduling, handling interruptions, and event processing.
2. Sorting:
• Heapsort uses heaps to sort elements in O(nlogn).
• Less commonly used than Quicksort but efficient for large datasets.
3. Graph Algorithms:
• Heaps are essential in algorithms like:
o Prim’s Algorithm (Minimum Spanning Tree),
o Dijkstra’s Algorithm (Shortest Path),
o A* search (Pathfinding).
4. Data Compression:
• Min-heaps are used in Huffman Coding to build Huffman trees for lossless
compression.
5. Medical Applications:
• Manage patient data based on priority, such as critical vital signs or urgent treatments.
6. Load Balancing:
• Distribute tasks or requests to servers by processing the lowest load first.
7. Order Statistics:
• Find the k-th smallest or largest element in an array efficiently using heaps.
8. Resource Allocation:
• Allocate resources like memory or CPU time by priority, ensuring efficient system
management.
9. Job Scheduling:
• Schedule tasks based on priority or deadlines, allowing quick access to the highest-
priority task.
III. LEFTIST TREES: (It is in book too)
A Leftist Tree is a special type of binary tree designed to make operations like merging two
trees efficient. It ensures that the tree is skewed to one side, prioritizing structural simplicity
over balance.
Key Properties
1. Min-Heap Property:
o Each node has a value smaller than or equal to the values of its children.
o This property ensures that the smallest element is always at the root.
2. Leftist Property:
o The rank of the left child is always greater than or equal to the rank of the right
child.
o The rank of a node is the length of the shortest path from that node to a leaf.
o This property makes the right path (right spine) as short as possible, ensuring
efficient merging.
Structure
• Rank: Defined as the shortest distance from a node to a leaf. A null node has a rank of
0.
• Left-heavy: To satisfy the leftist property, nodes are arranged such that the left subtree
is always "heavier" (has a greater rank) than the right subtree.
Operations
1. Merge:
o The primary operation for leftist trees.
o Merging is done recursively:
▪ Compare the roots of two trees.
▪ Attach the smaller root as the parent.
▪ Recursively merge the right subtree of the smaller root with the other
tree.
▪ After merging, swap the left and right children to maintain the leftist
property.
o The time complexity is O(logn), since the right path is always short.
2. Insertion:
o Insert an element by merging the new element (as a single-node tree) with the
existing tree.
o Time complexity: O(logn).
3. Deletion of Minimum:
o Remove the root (which holds the smallest value).
o Merge the left and right subtrees of the root.
o Time complexity: O(logn).
Advantages
• Leftist trees are useful for implementing priority queues.
• Efficient merging makes them a good choice for applications that require frequent
union or merge operations.
Example:
Leftist Tree Structure:
2
/ \
4 5
/\
7 8
Rank of Each Node:
• Root (2): Rank = 2 (shortest path to a leaf is through 5).
• Left child (4): Rank = 1.
• Right child (5): Rank = 0.
This structure satisfies the leftist property as the left child always has a rank greater than or
equal to the right child.
(OR)
What is a Binary Tree?
A binary tree is a tree data structure where each node has at most two children: a left child
and a right child. It is a hierarchical structure with a single root, and each node can have zero,
one, or two child nodes.
Key Characteristics:
• Root: The top node of the tree.
• Nodes: Elements of the tree.
• Edges: Connections between nodes.
• Leaf Nodes: Nodes with no children.
• Parent Node: A node that has one or more children.
• Child Nodes: Nodes that are directly connected to another node in a downward
direction.
Drawbacks of Binary Heap
A binary heap is a complete binary tree that satisfies the heap property, but it has some
limitations:
1. Inefficient Searching:
o Searching for an element is not efficient. In a binary heap, finding an arbitrary
element requires scanning all nodes, which is O(n)O(n)O(n) in the worst case.
2. Fixed Structure:
o While it efficiently supports insertion and removal of the root (max/min
element), binary heaps don't support efficient deletion or modification of
arbitrary elements.
3. Not Balanced:
o A binary heap doesn't guarantee balance (like AVL or Red-Black trees), so the
tree can be "unbalanced," which may affect performance for certain
operations.
What is a Leftist Tree?
A Leftist Tree is a type of binary tree that satisfies two main properties:
• Heap Property: The value of each node is smaller than or equal to the values of its
children (for a min-heap).
• Leftist Property: The left subtree of every node is "heavier" than the right subtree.
Specifically, the left child's rank (the shortest distance from the node to a leaf) is
greater than or equal to the right child's rank.
The Leftist Tree is designed for efficient merging operations, making it useful for implementing
priority queues and similar applications.
Properties of Leftist Tree:
1. Heap Property:
o In a min-leftist tree, each node's value is smaller than or equal to the values of
its children, ensuring the smallest value is always at the root.
2. Leftist Property:
o The rank of the left child is greater than or equal to the rank of the right child.
The rank of a node is defined as the shortest distance to a leaf node.
3. Merging Efficiency:
o Leftist trees are designed for fast merging operations. Merging two leftist trees
takes O(log⁡n)O(\log n)O(logn), as the shorter right path is always kept
minimal.
4. Rank of a Node:
o The rank of a node is the length of the shortest path from that node to a leaf.
A null node has rank 0.
What is a Leftist Heap?
A Leftist Heap is a type of leftist tree specifically used for implementing priority queues. It
combines the heap property with the leftist property to optimize the process of merging two
heaps.
Key Characteristics:
• It is a self-adjusting binary tree structure.
• Like a binary heap, it satisfies the heap property.
• It maintains the leftist property for efficient merging operations.
Operations:
• Merge: The merge operation is the most important in leftist heaps and is performed
efficiently in O(log⁡n)O(\log n)O(logn).
• Insertion: Can be done by merging a new element into the heap.
• Deletion of Minimum: The minimum element (root) is removed, and the left and right
subtrees are merged.
Advantages:
• Efficient Merging: Leftist heaps are particularly efficient when frequent merging is
required, such as in applications like Dijkstra's shortest path algorithm or priority
queue management.
Summary of Leftist Heap vs Binary Heap:
• A binary heap is a complete binary tree with a heap property that ensures the largest
(max-heap) or smallest (min-heap) element is at the root.
• A leftist heap is a type of binary heap with additional properties (leftist property) that
optimize the merge operation.
• Leftist trees focus on efficient merging, whereas binary heaps are more focused on
efficient insertion and deletion of the root element.
IV. HASH TABLE: (It is in book too)
A Hash table is a data structure that stores some information, and the information has
basically two main components, i.e., key and value. The hash table can be implemented with
the help of an associative array. The efficiency of mapping depends upon the efficiency of the
hash function used for mapping.

Drawback of Hash function


A Hash function assigns each value with a unique key. Sometimes hash table uses an imperfect
hash function that causes a collision because the hash function generates the same key of two
different values.
Hashing
Hashing is one of the searching techniques that uses a constant time. The time complexity in
hashing is O(1). Till now, we read the two techniques for searching, i.e., linear
search and binary search. The worst time complexity in linear search is O(n), and O(logn) in
binary search. In both the searching techniques, the searching depends upon the number of
elements but we want the technique that takes a constant time. So, hashing technique came
that provides a constant time.
In Hashing technique, the hash table and hash function are used. Using the hash function, we
can calculate the address at which the value can be stored.
The main idea of hashing is to store data as key-value pairs. When a key is given, a hash
function calculates an index where the corresponding value is stored. This allows quick storage
and retrieval of data. It can be written as:
Index = hash(key)
Advantages of Hash Tables
1. Fast Lookups:
o Average-case time complexity for insertion, search, and deletion is O(1).
2. Efficient Space Usage:
o Stores data compactly in an array-like structure.
Disadvantages
1. Collisions:
o Can lead to performance degradation.
2. Fixed Size:
o Requires resizing when the table becomes full, which can be time-consuming.
3. Dependency on Hash Function:
o Poor hash functions can lead to uneven distribution and inefficiency.
Applications
1. Databases: For indexing and quick lookups.
2. Caching: To store frequently accessed data for faster retrieval.
3. Symbol Tables: In compilers and interpreters.
4. Sets and Maps: In programming languages (e.g., HashSet and HashMap in Java).
Example
If you want to store key-value pairs like {101: "Alice", 102: "Bob", 103: "Charlie"}:
1. Use a hash function to map keys to indices:
o hash(101)= 1
o hash(102)= 2
o hash(103)= 3
2. Store the values in the hash table:
Index Value
1 Alice
2 Bob
3 Charlie
3. To retrieve "Bob," use hash(102)= 2 to directly access the value at index 2.
V. HASH FUNCTIONS:
A Function that translates keys to array indices is known as a hash function. The keys should
be evenly distributed across the array via a decent hash function to reduce collisions and
ensure quick lookup speeds.
Types of Hash Functions:
1. Division Method:

• Calculates the hash code as: h(k)=k mod m


where k is the key, and m is the size of the hash table.

• Example: If k=42 and m=10, then h(42)=42 mod 10=2.

• Advantages: Simple and efficient.

• Disadvantages: Poor performance if mmm is not a prime number (can lead to


clustering).
2. Multiplication Method:

• Uses a constant A to calculate the hash code: h(k)=[m(k.Amod1)]


Where [] denotes floor division function.

• Example: If k=42, A=5, and m=10


h(42)=[10(42.5mod1)]

• Advantages: Less sensitive to the choice of m.

• Disadvantages: More complex than the division method.


3. Mid-Square Method:

• In the mid-square method, the key is squared, and the middle digits of the result are
taken as the hash value.

• Steps:
1. Square the key.
2. Extract the middle digits of the squared value.
3. Map to Hash Table.

• Example: If k=1234
K2=1522756
Extract the middle value = 227

• Advantages: Produces a good distribution of hash values.


• Disadvantages: May require more computational effort.
4. Folding Method:

• The folding method involves dividing the key into equal parts, summing the parts, and
then taking the modulo with respect to m.

• Steps:
1. Divide the key into equal parts.
2. Sum the parts.
3. Take the modulo m of the sum.

• Example: If k=123456
Sum=123+456=579
h(579)=579 mod m

• Advantages: Simple and easy to implement.

• Disadvantages: Depends on the choice of partitioning scheme.


Characteristics of a Good Hash Function
• Uniform Distribution: Spreads keys evenly across the table to minimize clustering.
• Deterministic: Always produces the same hash code for the same key.
• Fast Computation: Can be calculated quickly for efficiency.
• Minimal Collisions: Reduces the likelihood of two keys hashing to the same value.
Choosing the Right Hash Function
The choice of a hash function depends on:
1. The type of keys (numbers, strings, etc.).
2. The size of the hash table.
3. The likelihood of collisions in the data.
VI. MID-SQUARE METHOD:
The Mid-Square Method is a hashing technique where the key is squared, and the middle
digits of the result are extracted to determine the hash value. This method works well for
numeric keys and spreads the values more uniformly across the hash table.
Steps:
1. Square the key: Compute the square of the key (key × key).
2. Extract the middle digits of the squared value: Identify the middle portion of the squared
result. The number of digits to extract depends on the size of the hash table.
3. Map to Hash Table: Use the extracted digits (usually in decimal form) as the hash value or
compute the hash index using h(k) = middle digits mod m, where m is size of hash table.
Example: Suppose we want to hash the key 123 for a hash table of size 10:
1. Square the key:
1232=15129
2. Extract middle digits:
Take the middle two digits (e.g, 51.)
3. Map to hash table:
Compute the hash index: h(123)=51 mod 10= 1
The key 123 will be stored at the index 1 in the hash table.
Advantages:
1. Uniform Distribution:
• The squaring process randomizes the key, leading to better spread and fewer collisions.
2. Simplicity:
• Easy to implement for numeric keys.
Disadvantages:
1. Key Dependence:
• Performance depends on the distribution of the keys. Repetitive patterns in keys may
still lead to clustering.
2. Costly Computation:
• Squaring large keys can be computationally expensive.
VII. COLLISIONS: (Open Addressing continuation is in book)
Since a hash function gets us a small number for a key which is a big integer or string, there is
a possibility that two keys result in the same value. The situation where a newly inserted key
maps to an already occupied slot in the hash table is called collision and must be handled
using some collision handling technique.
How to handle Collisions?
There are mainly two methods to handle collision:
• Separate Chaining
• Open Addressing
1. Separate Chaining:
Separate Chaining is a collision handling technique. Separate chaining is one of the most
popular and commonly used techniques in order to handle collisions. The idea behind
separate chaining is to implement the array as a linked list called a chain.
Here, all those elements that hash into the same slot index are inserted into a linked list. Now,
we can use a key K to search in the linked list by just linearly traversing. If the intrinsic key for
any entry is equal to K then it means that we have found our entry. If we have reached the
end of the linked list and yet we haven’t found our entry then it means that the entry does
not exist. Hence, the conclusion is that in separate chaining, if two different elements have
the same hash value then we store both the elements in the same linked list one after the
other.
Advantages:
• Handles an unlimited number of collisions.
• Simple to implement.
• Works well if the hash table isn't overly full.
Disadvantages:
• Poor performance if chains become long.
• Requires extra memory for the linked list.
2. Open Addressing:
Open Addressing is a method for handling collisions. In Open Addressing, all elements are
stored in the hash table itself. So, at any point, the size of the table must be greater than or
equal to the total number of keys (Note that we can increase table size by copying old data if
needed). This approach is also known as closed hashing. This entire procedure is based upon
probing. There are 3 types in open addressing. They are:

• Linear Probing
• Quadratic Probing
• Double Hashing
VIII. FOLDING METHOD
The Folding Method involves dividing the key into equal parts, summing the parts and then
taking the modulo with respect to M.
Algorithm:
The folding method for creating hash functions works like this:
1. Divide the key into equal parts (the last part can be smaller).
2. Add up the parts to get a sum.
3. Calculate the hash value using:
H(x)=(a+b+c) mod M
where a,b,c are the parts, and M is the table size.
4. The remainder after dividing the sum by M is the hash value.
Explanation:
Example: The task is to fold the key 123456789 into a Hash Table of ten spaces (0 through 9).
• It is given that the key, say X is 123456789 and the table size (i.e., M = 10).
• Since it can break X into three parts in any order. Let’s divide it evenly.
• Therefore, a = 123, b = 456, c = 789.
• Now, H(x) = (a + b + c) mod M i.e., H(123456789) =(123 + 456 + 789) mod 10 = 1368
mod 10 = 8.
• Hence, 123456789 is inserted into the table at address 8.
XI. AVL TREES:
AVL Tree is invented by GM Adelson - Velsky and EM Landis in 1962. The tree is named AVL in
honour of its inventors.
AVL Tree can be defined as height balanced binary search tree in which each node is associated
with a balance factor which is calculated by subtracting the height of its right sub-tree from
that of its left sub-tree.
Tree is said to be balanced if balance factor of each node is in between -1 to 1, otherwise, the
tree will be unbalanced and need to be balanced.
Balance Factor (k) = height (left(k)) - height (right(k))
If balance factor of any node is 1, it means that the left sub-tree is one level higher than the
right sub-tree.
If balance factor of any node is 0, it means that the left sub-tree and right sub-tree contain
equal height.
If balance factor of any node is -1, it means that the left sub-tree is one level lower than the
right sub-tree.
An AVL tree is given in the following figure. We can see that, balance factor associated with
each node is in between -1 and +1. therefore, it is an example of AVL tree.

Complexity:

Algorithm Average Case Worst Case


Space O(n) O(n)
Search O(logn) O(logn)
Insert O(logn) O(logn)
Delete O(logn) O(logn)

Operations on AVL Tree:


Due to the fact that, AVL tree is also a binary search tree therefore, all the operations are
performed in the same way as they are performed in a binary search tree. Searching and
traversing do not lead to the violation in property of AVL tree. However, insertion and deletion
are the operations which can violate this property and therefore, they need to be revisited.

S.No Operation Description


1. Insertion Insertion in AVL tree is
performed in the same way
as it is performed in a binary
search tree. However, it may
lead to violation in the AVL
tree property and therefore
the tree may need balancing.
The tree can be balanced by
applying rotations.
2. Deletion Deletion can also be
performed in the same way
as it is performed in a binary
search tree. Deletion may
also disturb the balance of
the tree therefore, various
types of rotations are used
to rebalance the tree.

AVL Rotations:
We perform rotation in AVL tree only in case if Balance Factor is other than -1, 0, and 1. There
are basically four types of rotations which are as follows:
1. LL rotation: Inserted node is in the left subtree of left subtree of A
2. RR rotation: Inserted node is in the right subtree of right subtree of A
3. LR rotation: Inserted node is in the right subtree of left subtree of A
4. RL rotation: Inserted node is in the left subtree of right subtree of A
Where node A is the node whose balance Factor is other than -1, 0, 1.
The first two rotations LL and RR are single rotations and the next two rotations LR and RL are
double rotations. For a tree to be unbalanced, minimum height must be at least 2, Let us
understand each rotation.
1. RR Rotation:
When BST becomes unbalanced, due to a node is inserted into the right subtree of the right
subtree of A, then we perform RR rotation, RR rotation is an anticlockwise rotation, which is
applied on the edge below a node having balance factor -2.

In above example, node A has balance factor -2 because a node C is inserted in the right
subtree of A right subtree. We perform the RR rotation on the edge below A.
2. LL Rotation:
When BST becomes unbalanced, due to a node is inserted into the left subtree of the left
subtree of C, then we perform LL rotation, LL rotation is clockwise rotation, which is applied
on the edge below a node having balance factor 2.

In above example, node C has balance factor 2 because a node A is inserted in the left subtree
of C left subtree. We perform the LL rotation on the edge below A.
3. LR Rotation:
Double rotations are bit tougher than single rotation which has already explained above. LR
rotation = RR rotation + LL rotation, i.e., first RR rotation is performed on subtree and then LL
rotation is performed on full tree, by full tree we mean the first node from the path of inserted
node whose balance factor is other than -1, 0, or 1.
Let us understand each and every step very clearly:
4. RL Rotation:
As already discussed, that double rotations are bit tougher than single rotation which has
already explained above. R L rotation = LL rotation + RR rotation, i.e., first LL rotation is
performed on subtree and then RR rotation is performed on full tree, by full tree we mean the
first node from the path of inserted node whose balance factor is other than -1, 0, or 1.
X. RED-BLACK TREE:
A Red-Black Tree is a type of self-balancing binary search tree with specific properties that
ensure it remains approximately balanced, providing efficient Insertion, deletion and lookup
operations. It was introduced by Rudolf Bayer in 1972.
Properties of Red-Black Trees:
1. Node Colour: Each node is either Red or Black.
2. Root Property: Root is always black.
3. Red Node Property: Red nodes cannot have red children. (i.e., No two red nodes can be
adjacent.)
4. Black Depth Property: Every path from a node to its descendant’s leaves must have the
same number of nodes.
5. Leaf Nodes: All Leaf nodes (NILL nodes) are black and do not store any data. They are
placeholders used to maintain the tree’s structure.

Time Complexity:

S.No Algorithms Time Complexity


1. Search O(logn)
2. Insert O(logn)
3. Delete O(logn)

Basic Operations on Red Black Tree:


The basic operations on Red Black Tree include:
1. Insertion
2. Searching
3. Deletion
4. Rotation

1) Insertion:
Inserting a new node in a Red-Black Tree involves a two-step process: performing a standard
binary search tree (BST) insertion, followed by fixing any violations of Red-Black properties.
Insertion Steps:
Step-1: Insert the new node with colour red.
Step-2:
Case-1: If node is root: Recolour it to black.
Case-2: Red-Red violation

• If the new node’s parent is red.


Case-2.1: If uncle is red:
o Recolour parent and uncle to black, and grandparent to red.
o Then repeat the fixup process from grandparent.
Case-2.2: If uncle is black or null:
o Perform rotations to balance the tree.
o Recolour the nodes accordingly.
2) Searching:
Searching for a node in a Red-Black Tree is similar to searching in a standard Binary Search
Tree (BST). The search operation follows a straightforward path from the root to a leaf,
comparing the target value with the current node’s value and moving left or right accordingly.
Searching Steps:
1. Start at the Root: Begin the search at the root node.
2. Traverse the Tree:
• If the target value is equal to the current node’s value, the node is found.
• If the target value is less than the current node’s value, move to the left child.
• If the target value is greater than the current node’s value, move to the right
child.
3. Repeat: Continue this process until the target value is found or a NIL node is reached
(indicating the value is not present in the tree).
3) Deletion:
1. If we need to delete leaf node without any children then we can easily do it without
any worries.
2. If we want to delete an internal node with one child then replace the value of the
internal node with the value of its child and delete the child node.
3. If we want to delete an internal node with two children then we can either replace the
internal node with:
• In-Order Predecessor: The largest element from the left subtree of the node.
• In-Order Successor: The smallest element from the right surface of the node.
Steps for deletion:
Step-1: Perform BST deletion.
Step-2:
Case-1: If node to be deleted is red, just delete it.
Case-2: If root is double black, just remove double black.
Case-3: If DB’s sibling is black & both of its children are black.

• Remove DB.
• Add black to parent.
o If parent is red, it becomes black.
o If parent is black, it becomes double black.
• Make sibling red.
• If DB still exists, apply other cases.
Case-4: If DB’s sibling is red:

• Swap colours of parent and sibling.


• Rotate parent in DB direction.
• Reapply cases.
Case-5: If DB’s sibling is black and the sibling’s child who is far from DB is black but
near child is red:

• Swap colour of DB’s sibling and child of sibling who is near to black.
• Rotate sibling in opposite direction.
• Apply case-6.
Case-6: DB’s sibling is black and far child is red:

• Swap colour of parent and sibling.


• Rotate the parent in DB’s direction.
• Remove DB.
• Change colour of red child to black.
4) Rotation:
Rotations are fundamental operations in maintaining the balanced structure of a Red-Black
Tree (RBT). They help to preserve the properties of the tree, ensuring that the longest path
from the root to any leaf is no more than twice the length of the shortest path. Rotations come
in two types: left rotations and right rotations.
Left Rotation:
A left rotation at node x moves x down to the left and its right child y up to take x’s place.
Before Rotation:
x
\
y
/\
a b
After Left Rotation:
y
/\
x b
\
A
Left Rotation Steps:
1. Set y to be the right child of x.
2. Move y’s left subtree to x’s right subtree.
3. Update the parent of x and y.
4. Update x’s parent to point to y instead of x.
5. Set y’s left child to x.
6. Update x’s parent to y.
Right Rotation:
A right rotation at node x moves x down to the right and its left child y up to take x’s place.
Before Right Rotation:
x
/
y
/\
a b
After Right Rotation:
y
/\
a x
/
b
Right Rotation Steps:
1. Set y to be the left child of x.
2. Move y’s right subtree to x’s left subtree.
3. Update the parent of x and y.
4. Update x’s parent to point to y instead of x.
5. Set y’s right child to x.
6. Update x’s parent to y.
Advantages:
• Balanced: Red-Black Trees are self-balancing, ensuring efficient search, insertion, and
deletion operations.
• Efficient: They offer O(log n) time complexity for most operations.
• Simple to implement: The rules for maintaining Red-Black Tree properties are
relatively straightforward.
• Widely used: They are a popular choice for implementing various data structures and
algorithms.
Disadvantages:
• Compared to simpler balanced trees like AVL trees, Red-Black Trees have more
complex insertion and deletion rules.
• Maintaining the Red-Black Tree properties adds a small overhead to every insertion
and deletion operation.
• For applications with frequent insertions and deletions, other balanced tree structures
might be more suitable.
Applications:
• Implementing maps and sets
• Priority queues
• File systems
• In-memory databases
• Graphics and game development (collision detection, pathfinding)

You might also like