Advanced Data Structures
Advanced Data Structures
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).
• 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(logn)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(logn)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.
• 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
• 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
• 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:
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:
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
• 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 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: