Unit 2
Unit 2
Unit 2
Introduction
A red-black tree is a self-balancing binary search tree with one extra bit at each node, which is
commonly regarded as the color (red or black). These colors are used to keep the tree balanced as
insertions and deletions are made.
Red-Black tree=Binary Search Tree +One Extra Bit for color (either Red or Black)
RB Tree is also a self-Balancing tree like B-Tree. But, it uses coloring scheme to balance itself.
Properties of Red- Black Tree
Property 1: Red black tree is the binary search tree
6 11
2
5 7 10 13
Property 6. Every simple path from a node to a descendant leaf contains the same number of black nodes.
6 11
5 7 10 13
13
17
8
25
1 11 15
NI NI NI NI
L L L L
The above tree is a Red-Black tree if it satisfies all the properties of RB Tree.
Property 1 says that every red-black tree is a binary search tree i.e. left child node is smaller
than root node and right child node is greater than the root node.
// given red-black tree fulfils this property
Property 2 says that there can be only two types of nodes i.e. red or black
// given tree fulfil this property as there are only red and black nodes present in the tree.
Property 3 says that every leaf node(NIL) should be black in colour
// in the above given tree all the NIL nodes are represented by black colour.
Property 4 tells that head node of the tree should be black in colour
// Here root node 13 is black in colour so it follows this property.
Property 5 says that there should be no red –red parent child relationship.
// in the above given tree all the red nodes has black children.
Property 6 says every path from node to leaf node has same no. of black nodes
// in the given tree every path from root node has 2 black nodes in total so it fulfils this property
also.
After checking all the above properties, we can say that the given tree is a red-black tree
Is This a Red Black Tree?
13
17
8
25
1 11 15
NIL 22 27
NIL 6 NIL NIL NIL
L
The above tree is a Red-Black tree if it satisfies all the properties of RB Tree.
Property 1 says that every red-black tree is a binary search tree i.e. left child node is smaller
than root node and right child node is greater than the root node.
// given red-black tree fulfils this property
Property 2 says that there can be only two types of nodes i.e. red or black
// given tree fulfil this property as there are only red and black nodes present in the tree.
Property 3 says that every leaf node(NIL) should be black in colour
// in the above given tree all the NIL nodes are represented by black colour.
Property 4 tells that head node of the tree should be black in colour
// Here root node 13 is black in colour so it follows this property.
Property 5 says that there should be no red –red parent child relationship.
// in the above given tree all the red nodes has black children.
Property 6 says every path from node to leaf node has same no. of black nodes
// in the given tree left path from the root node has total two black nodes and right path has total 3
black nodes. So 2≠ 3 due to which it violate this property.
After checking all the above properties we can say that the given tree is not a red-black
tree.
RB Tree Creation / Insertion in RB Tree
Types of Sub-Issue
Type 1:- When the color of Type 2:- When the color of
sibling is red. sibling is black.
Solution:- This issue is fixed by Solution:- This issue is fixed by
changes in color. both rotations and re-coloring.
RB-INSERT algorithm will insert a new node with red color in the same manner as we have studied in
binary search tree.
RB Tree INSERT-FIXUP algorithm will be called when the main issue will occur i.e. the parent’s color
of the newly inserted node is also red. After then, we will explore the type of sub-issue and will
resolve it either by recoloring the nodes or by performing rotations.
RB-INSERT (T, z) RB-INSERT-FIXUP (T, z)
Terminology Description
Z Newly inserted node
P[z] Parent of node z
P[P[z]] Grandparent of node z
If P[z] ← left[P[P[z]]] If parent of node z is left child of its parent, then uncle y
then y ← right[P[P[z]]] will be right child of grandparent of z.
If P[z] ← right[P[P[z]]] If parent of node z is right child of its parent, then uncle
then y ← left[P[P[z]]] y will be left child of grandparent of z.
root[T] Root of the tree
color[x] Color of node x
NOTE:- MAIN CASE 2 is just a mirror image of MAIN CASE 1 (means left is exchanged with right and
right is exchanged with left). And at last of both MAIN CASES, we will make color of root node
as BLACK.
SUBCASES 2 & 3 use rotations and re-coloring to maintain the properties of RB tree after
insertion of a new node into it. The following diagram will recall the LEFT & RIGHT rotations
(studied in Data Structure Course under topic AVL Tree)
After performing any of the rotation, the structural properties of BST should also be maintained.
Now, we will explore RB Tree Insertion using various examples complying with a particular case.
Example 1.
41 41
Insert 50
32 32 50
50 will be inserted as right child of node 41 following BST property. Here, z = 50 & p[z] = 41. Because,
color[p[z]] = RED. So, there is no need to call INSERT-FIXUP. Node 50 will be inserted simply with red
color.
Example 2.
Insert 12 operation in given RB tree will lead to MAIN CASE 1 → SUB CASE 1. Because, P[z] = 31 is
left child of its parent i.e. p[p[z]] = 38 that complies to MAIN CASE 1. Further, the color of uncle y =
right[p[p[z]]] = 41 is RED that complies to SUB CASE 1. So, change the color of both nodes 31 (parent)
and 41 (uncle) to BLACK. Also, change the color of grandparent node 38 as RED. But, after executing
MAIN CASE & SUB CASE, we will check the color of root node. If it is RED, make it BLACK. So, the
root node 38 will be colored as BLACK.
Example 3.
Insert 19 will comply with MAIN CASE 1. Because, P[z] = 12 is left child of its parent node 31. Further,
right child of node 31 is NIL that means uncle y = NIL of BLACK color. Z is right child of its parent.
Further, it complies with SUBCASE 2. So, perform left rotation along new z i.e. 12. After this, SUBCASE
3 will be called. Change the color of node 19 (parent) as BLACK and of node 31 (grandparent) as RED.
After re-coloring of nodes, perform right rotation along grandparent 31. At last, check for root node’s
color.
Perform Left-Rotate
Insert 19 38 38 (T,12)
38 Main Case 1
P[P[Z]] P[P[Z]]
Sub Case 2
31 ]] ]]]]]
41 31 41
41 31
P [Z] P [Z]
12
12 NIL NIL
19 Y
Y
19 12
Z Z
Main Case 1
38
38
Sub Case 3
P[P[Z]]
Right-Rotate (T,31) ]]
19 41 31 41
P [Z]
12 31 19
Z
40 40
Step 2 Insert 50
// 50 >40 so it is inserted as the right child of root node 40
40
50
Step 3 Insert 70
// after inserting 70 property 5 violates as no red parent should have red child node.
P[P[Z]]
40 ]]
P [Z]
Here P [Z] <- right [P[P[Z]]]
50 So, y<- left [P [P [Z]]]
Y<- NIL (Black)
Z
(Main Case 2)
Y (uncle) is black & Z
70 =right[P[Z]]
So (Sub case 3)
Color P [Z] =Black & color
P[P[Z]]=red
Case 3 Left Rotate (T, P [P[Z]])
50
40 70
Step 4 Insert 30
P[P[Z]]
]] 50
50
Y
P [Z]
Main Case
40 70
40 70
Z
P Sub Case 1
30 Root. Color =Black
30
Step 5 Insert 42
50
30 42
Step 6 Insert 15
50
50
P[P[Z]]
]] Main Case 1
40 70
40 70
P [Z] Y
Sub Case 1
30 42
Z
30 42
15
15
Step 7 Insert 20
50
50
Main Case 1
40 70
P[P[Z]] 40 70
]] Sub Case 2
30 42 P[P[Z]]
P [Z] ]]
30 42
Y= NIL (Black)
15
P [Z]
Z 20 20
Sub
Z Case 3
15
50
40 70
20 42
15 20
Types of Deletion
Note: While swapping, swap only the keys but not the colors.
Terminology Description
v Node to be Deleted
u Be the child of v that replaces v
s Sibling of v
r Child of sibling s
Doubly Black
Important:
1. If v.color= Red then no issue
2. If v.color= Black then call Delete_FIXUP Procedure
20 40
10 40
u
0
10
20 40
NIL 40
u
NIL NIL
50 Doubly Black
50
Case 2a: If sibling is black and atleast one of sibling’s children is red then perform rotation.
Types of Rotation
P[s] P[s]
30 30
v s u s
Delete 20
NIL
20 40 40
NIL 35 35
u
r r
Sub case 3
R-R(T, s)
35
P[s]
L-R(T,P[s]) 30
u
30 40 r
NIL
35
40
Case 2b: If sibling is black and both of it’s children are also black.
a. Re-color sibling as red
b. Recolor parent as doubly black and call DELETE_FIX _UP on parent.
Note:
B+B-> Double Black ( Two black nodes)
R+ Double Black-> (Single black node)
P[s] P[s]
20 20
v s Delete 10 s
v
10 30 NIL 30
Case 2b
20
Case 4
P[s]
20
NIL 30
u s
NIL
30
NIL NIL
NIL NIL
r r
Sub cases
NIL NIL 35
25 35 25
r r r r
Exchange color
of s and P[s] Case 3b
30 Case 2b
s 30
P[s] s
20 35 s-> red
P->Double Black ]
20 35
NIL v
25
NIL
25
NIL NIL
r r
Case 4: If root is double black, make it single black.
Ques Perform deletion operation red black tree in order 30, 25, 20, 15, 10 , 5.
10
5 20
15 25
30
Solution
10
Delete 30 10
P[s]
5 20
5 20
v
15 25
s
15 25
30
Delete 25
Case 2b
P[s]
P[s]
Case 1
10
Case 1 10
s v
s v
5 15
Delete 20 20
5
Delete 15 15 u
Case 2b
v
Case 4
10
10
u
5
5
Delete 10
Case 1
Empty Tree 5
Delete 5
Searching Red Black tree does not provide AVL trees provide efficient searching
efficient searching as Red Black as it is strictly balanced tree.
Trees are roughly balanced.
Insertion and Insertion and Deletion are easier in Insertion and Deletion are complex in
Deletion Red Black tree as it requires fewer AVL tree as it requires multiple
rotations to balance the tree. rotations to balance the tree.
Color of the In the Red-Black tree, the color of the In the case of AVL trees, there is no
node node is either Red or Black. color of the node.
Balance It does not contain any balance factor. Each node has a balance factor in AVL
factor It stores only one bit of information tree whose value can be 1, 0, or -1. It
that denotes either Red or Black color requires extra space to store the
of the node. balance factor per node.
Strictly Red-black trees are not strictly AVL trees are strictly balanced, i.e., the
balanced balanced. left subtree's height and the height of
the right subtree differ by at most 1.
B Tree
INTRODUCTION
A B-tree is a self-balancing tree where all the leaf nodes are at the same level which allows for
efficient searching, insertion and deletion of records. Because of all the leaf nodes being on the same
level, the access time of data is fixed regardless of the size of the data set. B Tree is a specialized m-
way tree that can be widely used for disk access. A B-Tree of order m can have at most m-1 keys and
m children. One of the main reason of using B tree is its capability to store large number of keys in a
single node and large key values by keeping the height of the tree relatively small.
A B tree of order m contains all the properties of an M way tree. In addition, it contains the following
properties.
It is not necessary that, all the nodes contain the same number of children but, each node must have
m/2 number of nodes.
Characteristics of B-Tree
B-trees have several important characteristics that make them useful for storing and retrievin g large
amounts of data efficiently. Some of the key characteristics of B-trees are:
Balanced: B-trees are balanced, meaning that all leaf nodes are at the same level. This
ensures that the time required to access data in the tree remains constant, regardless of the
size of the data set.
Self-balancing: B-trees are self-balancing, which means that as new data is inserted or old
data is deleted, the tree automatically adjusts to maintain its balance.
Multiple keys per node: B-trees allow multiple keys to be stored in each node. This allows for
efficient use of memory and reduces the height of the tree, which in turn reduces the number
of disk accesses required to retrieve data.
Ordered: B-trees maintain the order of the keys, which makes searching and range queries
efficient.
Efficient for large data sets: B-trees are particularly useful for storing and retrieving large
amounts of data, as they minimize the number of disk accesses required to find a particular
piece of data.
Application of B-Tree:
B tree is used to index the data and provides fast access to the actual data stored on the disks since,
the access to value stored in a large database that is stored on a disk is a very time consuming process.
Searching an un-indexed and unsorted database containing n key values needs O(n) running time in
worst case. However, if we use B Tree to index this database, it will be searched in O(log n) time in
worst case.
The limitations of traditional binary search trees can be frustrating. Meet the B-Tree, the multi-talented
data structure that can handle massive amounts of data with ease. 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 as B-Tree or 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, which is why they are also known as “large key” trees. 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 maintain 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-trees are commonly used in applications where large amounts of data need to be stored and
retrieved efficiently. Some of the specific applications of B-trees include:
Databases: B-trees are widely used in databases to store indexes that allow for efficient
searching and retrieval of data.
File systems: B-trees are used in file systems to organize and store files efficiently.
Operating systems: B-trees are used in operating systems to manage memory efficiently.
Network routers: B-trees are used in network routers to efficiently route packets through
the network.
DNS servers: B-trees are used in Domain Name System (DNS) servers to store and
retrieve information about domain names.
Compiler symbol tables: B-trees are used in compilers to store symbol tables that allow for
efficient compilation of code.
Advantages of B-Tree:
B-trees have several advantages over other data structures for storing and retrieving large amounts
of data. Some of the key advantages of B-trees include:
Sequential Traversing: As the keys are kept in sorted order, the tree can be traversed
sequentially.
Minimize disk reads: It is a hierarchical structure and thus minimizes disk reads.
Partially full blocks: The B-tree has partially full blocks which speed up insertion and
deletion.
Disadvantages of B-Tree:
Complexity: B-trees can be complex to implement and can require a significant amount of
programming effort to create and maintain.
Overhead: B-trees can have significant overhead, both in terms of memory usage and
processing time. This is because B-trees require additional metadata to maintain the tree
structure and balance.
Not optimal for small data sets: B-trees are most effective for storing and retrieving large
amounts of data. For small data sets, other data structures may be more efficient.
Limited branching factor: The branching factor of a B-tree determines the number of child
nodes that each node can have. B-trees typically have a fixed branching factor, which can
limit their performance for certain types of data.
A B tree of order 4 is shown in the following image.
While performing some operations on B Tree, any property of B Tree may violate such as number of
minimum children a node can have. To maintain the properties of B Tree, the tree may split or join.
Operations
1.Searching:
Searching in B Trees is similar to that in Binary search tree. For example, if we search for an item 49
in the following B Tree. The process will something like following :
1. Compare item 49 with root node 78. since 49 < 78 hence, move to its left sub-tree.
2. Since, 40<49<56, traverse right sub-tree of 40.
3. 49>45, move to right. Compare 49.
4. match found, return.
Searching in a B tree depends upon the height of the tree. The search algorithm takes O(log n) time to
search any element in a B tree.
Examples:
Input: Search 120 in the given B-Tree.
Solution:
In this example, we can see that our search was reduced by just limiting the chances where the key
containing the value could be present. Similarly if within the above example we’ve to look for 180,
then the control will stop at step 2 because the program will find that the key 180 is present within the
current node. And similarly, if it’s to seek out 90 then as 90 < 100 so it’ll go to the left subtree
automatically, and therefore the control flow will go similarly as shown within the above example.
2. B-Tree Insertion
There are two conventions to define a B-Tree, one is to define by minimum degree, second is to
define by order.
A new key is always inserted at the leaf node. Let the key to be inserted be k. Like BST, we start from
the root and traverse down till we reach a leaf node. Once we reach a leaf node, we insert the key in
that leaf node. Unlike BSTs, we have a predefined range on the number of keys that a node can contain.
So before inserting a key to the node, we make sure that the node has extra space.
How to make sure that a node has space available for a key before the key is inserted?
We use an operation called splitChild() that is used to split a child of a node. See the following diagram
to understand split. In the following diagram, child y of x is being split into two nodes y and z. Note that
the splitChild operation moves a key up and this is the reason B-Trees grow up, unlike BSTs which
grow down.
As discussed above, to insert a new key, we go down from root to leaf. Before traversing down to a
node, we first check if the node is full. If the node is full, we split it to create space. Following is the
complete algorithm.
Insertions are done at the leaf node level. The following algorithm needs to be followed in order to insert
an item into B Tree.
1. Traverse the B Tree in order to find the appropriate leaf node at which the node can be inserted.
2. If the leaf node contains less than m-1 keys then insert the element in the increasing order.
3. Else, if the leaf node contains m-1 keys, then follow the following steps.
o Insert the new element in the increasing order of elements.
o Split the node into the two nodes at the median.
o Push the median element upto its parent node.
o If the parent node also contain m-1 number of keys, then split it too by following the same
steps.
Insertion
1) Initialize x as root.
2) While x is not leaf, do following
a) Find the child of x that is going to be traversed next. Let the child be y.
b) If y is not full, change x to point to y.
c) If y is full, split it and change x to point to one of the two parts of y. If k is smaller than mid key in y,
then set x as the first part of y. Else second part of y. When we split y, we move a key from y to its
parent x.
3) The loop in step 2 stops when x is leaf. x must have space for 1 extra key as we have been
splitting all nodes in advance. So simply insert k to x.
EXAMPLE:
Let us understand the algorithm with an example tree of minimum degree ‘t’ as 3 and a sequence of
integers 10, 20, 30, 40, 50, 60, 70, 80 and 90 in an initially empty B-Tree.
Initially root is NULL. Let us first insert 10.
Let us now insert 20, 30, 40 and 50. They all will be inserted in root because the maximum number of
keys a node can accommodate is 2*t – 1 which is 5.
Let us now insert 60. Since root node is full, it will first split into two, then 60 will be inserted into the
appropriate child.
Let us now insert 70 and 80. These new keys will be inserted into the appropriate leaf without any split.
Let us now insert 90. This insertion will cause a split. The middle key will go up to the parent.
The B-tree is a data structure that is similar to a binary search tree, but allows multiple keys per node
and has a higher fanout. This allows the B-tree to store a large amount of data in an efficient manner,
and it is commonly used in database and file systems.
The B-tree is a balanced tree, which means that all paths from the root to a leaf have the same length.
The tree has a minimum degree t, which is the minimum number of keys in a non-root node. Each
node can have at most 2t-1 keys and 2t children. The root can have at least one key and at most 2t-
1 keys. All non-root nodes have at least t-1 keys and at most 2t-1 keys.
Example:2:
Insert the node 8 into the B Tree of order 5 shown in the following image.
3.Deletion
Deletion is also performed at the leaf nodes. The node which is to be deleted can either be a leaf node
or an internal node. Following algorithm needs to be followed in order to delete a node from a B tree.
If the the node which is to be deleted is an internal node, then replace the node with its in-order
successor or predecessor. Since, successor or predecessor will always be on the leaf node hence, the
process will be similar as the node is being deleted from the leaf node.
Example 1
Delete the node 53 from the B Tree of order 5 shown in the following figure.
53 is present in the right child of element 49. Delete it.
Now, 57 is the only element which is left in the node, the minimum number of elements that must be
present in a B tree of order 5, is 2. it is less than that, the elements in its left and right sub-tree are also
not sufficient therefore, merge it with the left sibling and intervening element of parent i.e. 49.
1. Search O(log n)
2. Insert O(log n)
3. Delete O(log n)
Q. The smallest number of keys that will force a B-Tree of order 3 to have a height of 3 are
_______
Solution:
Q. the order of B-tree is 32 and the B-tree is 63% full. What will be the number of <key,data
pointer> entries the 2 level B tree holds?
SOLUTION:
]here, order = 32 and Assuming that B tree node is 63% full.
so P = ceil ( 0.63 * 32 ) = 21
( If we take order P = 20, then utilization will be 62.5 % which is not allowed. )
Ggggggghjgjhbxjkhjm
Binomial Heaps
What is Binomial Heap?
Binomial heap was introduced in 1978 by Jean Vullemin. Jean Vullemin is a professor in mathematics
and computer science. The other name of Binomial Heap is Mergeable heaps.
A binomial heap is a collection of binomial trees.
What is Binomial tree?
Binomial tree 𝐵𝑘 is an ordered tree defined recursively. The binomial tree 𝐵0 has one node. The
binomial tree 𝐵𝑘 consists of two binomial trees 𝐵𝑘−1 and they are connected such that the root of one
tree is the leftmost child of the other.
Binomial tree properties:
𝐵𝑘 has 2𝑘 nodes
𝐵𝑘 has height 𝑘
𝑘
There are exactly ( )nodes at depth i for i=0, 1, 2,…,k.
𝑖
For Example:
Lets check in 𝐵4 and depth 2(𝑖. 𝑒. 𝑘 = 4 𝑎𝑛𝑑 𝑖 = 2)
𝑘 𝑘! 4! 4𝑋3𝑋2𝑋1
( ) = 𝑖!(𝑘−𝑖)! = 2! (4−2)! = 2 𝑋 1 𝑋 2 𝑋 1 = 6
𝑖
Hence 6 numbers of nodes are available in depth 2 of 𝐵4 .
The root has degree k which is greater than other node in the tree. Each of the root’s child is
the root of a subtree Bi.
Binomial Heap Properties
A Binomial Heap H is a set of binomial trees that satisfies the following properties:
Each binomial tree in H obeys the min heap property.
(i.e. key of a node is greater or equal to the key of its parent. Hence the root has the smallest
key in the tree).
For any non negative integer k, there is at most one binomial tree whose root has degree 𝑘.
(e.g. it implies that an n node Binomial heap H consists of at most ⌊log 𝑛⌋ + 1 binomial Tree.
The binomial trees in the binomial heap are arranged in increasing order of degree
Example of binomial Heap
Here n=13
So ⌊log 𝑛⌋ + 1 = ⌊log 13⌋ + 1 = 3 + 1 = 4 (So at most 4). The Above Figure of Binomial Heap consists
of 𝐵0 , 𝐵2 and 𝐵3
Representation of Binomial Heap
Each binomial tree within a binomial heap is stored in the left-child, right-sibling representation.
Each node 𝑋 contains POINTERS
• 𝑝[𝑥] 𝑜𝑟 𝑥 → 𝑝𝑎𝑟𝑒𝑛𝑡 to its parent
• 𝑘𝑒𝑦[𝑥] 𝑜𝑟 𝑥 → 𝑘𝑒𝑦 to its key value
• 𝑐ℎ𝑖𝑙𝑑[𝑥] 𝑜𝑟 𝑥 → 𝑐ℎ𝑖𝑙𝑑 to its leftmost child
• 𝑠𝑖𝑏𝑙𝑖𝑛𝑔[𝑥] 𝑜𝑟 𝑥 → 𝑠𝑖𝑏𝑙𝑖𝑛𝑔 to its immediately right sibling
• 𝑑𝑒𝑔𝑟𝑒𝑒[𝑥] 𝑜𝑟 𝑥 → 𝑑𝑒𝑔𝑟𝑒𝑒 to its degree value (i.e. denotes the number of children of X)
Operations on Binomial Heap
Binomial Heap support the following operations:
1. MAKE-HEAP () creates and returns a new heap containing no elements.
2. INSERT(H, x) inserts node x, whose key field has already been filled in, into heap H.
3. MINIMUM(H) returns a pointer to the node in heap H whose key is minimum.
4. EXTRACT-MIN(H) deletes the node from heap H whose key is minimum, returning a pointer to
the node.
5. UNION (H1, H2) creates and returns a new heap that contains all the nodes of heaps H1 and
H2. Heaps H1 and H2 are "destroyed" by this operation.
6. DECREASE-KEY(H, x, k) assigns to node x within heap H the new key value k, which is
assumed to be no greater than its current key value.
7. DELETE (H, x) deletes node x from heap H
1. MAKE-BINOMIAL-HEAP
This is the process of creating and returning a new heap containing no elements.
To make an empty binomial heap, the MAKE-BINOMIAL-HEAP procedure simply allocates and
returns an object H, where head[H ] = NIL.
The running time is Θ(1).
2. BINOMIAL-HEAP-INSERT
For inserting a new node x, Create a new heap H’ and set Head(H’) to the new node x. Now, we can
easily perform union of new Heap H’ with the existing heap H.
Time complexity for this operation is:-
For creating new heap = O(1)
For union of two heaps = O(log n)
So, for inserting a new node = O(1)+O(log n)= O(log n).
Binomial-Heap-Insert(H,x)
1. H’ <- Make-Binomial-Heap()
2. p[x] <- NIL
3. child[x] <- NIL
4. sibling[x] <- NIL
5. degree[x] <- 0
6. Head[H’] <- x
7. H <- Binomial-Heap-Union(H,H’)
First step is to create a heap with key 15. First, we have to combine both of the heaps.
As both node 12 and node 15 are of degree 0, so node 15 is attached to node 12 as shown below.
Now, assign x to B0 with value 12, next(x) to B0 with value 15, and assign sibling(next(x)) to B1 with
value 7. As the degree of x and next(x) is equal. The key value of x is smaller than the key value of
next(x), so next(x) is removed and attached to the x. It is shown in the below image –
Now, x points to node 12 with degree B1, next(x) to node 7 with degree B1, and sibling(next(x)) points
to node 15 with degree B2. The degree of x is equal to the degree of next(x) but not equal to the degree
of sibling(next(x)). The key value of x is greater than the key value of next(x); therefore, x is removed
and attached to the next(x) as shown in the below image -
Now, x points to node 7, and next(x) points to node 15. The degree of both x and next(x) is B2, and the
key value of x is less than the key value of next(x), so next(x) will be removed and attached to x as
shown in the below image –
Now, the degree of the above heap is B3, and it is the final binomial heap after inserting node 15.
3. BINOMIAL-HEAP-MINIMUM
The procedure BINOMIAL-HEAP-MINIMUM() returns a pointer to the node with the minimum key in an
n-node binomial heap H. This implementation assumes that there are no keys with value ∞.
Since the binomial heap is a min-heap-order, the minimum key of each binomial tree must be at the
root. This operation checks all the roots to find the minimum key.
Pseudocode: This implementation assumes that there are no keys with value ∞.
BINOMIAL-HEAP-MINIMUM(H)
1 y ← NIL
2 x ← head[H]
3 min ← ∞
4 while x ≠ NIL
5 do if key[x] < min
6 then min ← key[x]
7 y←x
8 x ← sibling[x]
9 return y
Running Time- Since, binomial heap is Heap-ordered and the minimum key must reside in a ROOT
node. The BINOMIAL-HEAP-MINIMUM(H) checks all roots in 𝑂 (𝑙𝑔𝑛).Because,
𝑁𝑢𝑚𝑏𝑒𝑟 𝑜𝑓 𝑅𝑜𝑜𝑡𝑠 𝑖𝑛 𝐵𝑖𝑛𝑜𝑚𝑖𝑎𝑙 𝐻𝑒𝑎𝑝 𝑖𝑠 𝑎𝑡 𝑙𝑒𝑎𝑠𝑡 ⌊log 𝑛⌋ + 1 Hence 𝑅𝑈𝑁𝑁𝐼𝑁𝐺– 𝑇𝐼𝑀𝐸 = 𝑂 (𝑙𝑔𝑛)
This procedure extracts the node with the minimum key from binomial heap H and returns a pointer to
the extracted node.
BINOMIAL-HEAP-EXTRACT-MIN(H)
1. find the root x with the minimum key in the root list
of H, and remove x from the root list of H
2. H' MAKE-BINOMIAL-HEAP()
3. reverse the order of the linked list of x's children,
and set head[H'] to point to the head of the
resulting list
4. H BINOMIAL-HEAP-UNION(H,H')
5. return x
The procedure is shown in following example. The input binomial heap H is shown in Figure (a). Figure
(b) shows the situation after line 1: the root x with the minimum key has been removed from the root
list of H. If x is the root of a Bk-tree, then by property 4 of Lemma 20.1, x's children, from left to right,
are roots of Bk-1-, Bk-2-,…, B0-trees. Figure (c) shows that by reversing the list of x's children in line 3,
we have a binomial heap H' that contains every node in x's tree except for x itself. Because x's tree is
removed from H in line 1, the binomial heap that results from uniting H and H' in line 4, shown in Figure
(d), contains all the nodes originally in H except for x. Finally, line 5 returns x.
Figure. The action of BINOMIAL-HEAP-EXTRACT-MIN.
Since each of lines 1-4 takes O(lg n) time if H has n nodes, BINOMIAL-HEAP-EXTRACT-MIN runs in
O(lg n) time.
5.Uniting two binomial heaps
The operation of uniting two binomial heaps is used as a subroutine by most of the remaining
operations. The BINOMIAL-HEAP-UNION procedure repeatedly links binomial trees whose roots have
the same degree. The following procedure links the Bk-1 tree rooted at node y to the Bk-1 tree rooted
at node z; that is, it makes z the parent of y. Node z thus becomes the root of a Bk tree.
The BINOMIAL-LINK procedure makes node y the new head of the linked list of node z's children in
O(1) time.
Union procedure- it unites binomial heaps H1 and H2, returning the resulting heap. It destroys the
representations of H1 and H2 in the process. Besides BINOMIAL-LINK, the procedure uses an auxiliary
procedure BINOMIAL-HEAP-MERGE that merges the root lists of H1 and H2 into a single linked list that
is sorted by degree into monotonically increasing order.
BINOMIAL-HEAP-UNION procedure with four cases is given in the pseudocode above.
The BINOMIAL-HEAP-UNION procedure has two phases. The first phase, performed by the call of
BINOMIAL-HEAP-MERGE, merges the root lists of binomial heaps H1 and H2 into a single linked list H
that is sorted by degree into monotonically increasing order. There might be as many as two roots (but
no more) of each degree, however, so the second phase links roots of equal degree until at most one
root remains of each degree. Because the linked list H is sorted by degree, we can perform all the link
operations quickly.
Lines 1-3 start by merging the root lists of binomial heaps H1 and H2 into a single root list H. The root
lists of H1 and H2 are sorted by strictly increasing degree, and BINOMIAL-HEAP-MERGE returns a root
list H that is sorted by monotonically increasing degree. If the root lists of H1 and H2 have m roots
altogether, BINOMIAL-HEAP-MERGE runs in O(m) time by repeatedly examining the roots at the
heads of the two root lists and appending the root with the lower degree to the output root list, removing
it from its input root list in the process.
The BINOMIAL-HEAP-UNION procedure next initializes some pointers into the root list of H. First, it
simply returns in lines 4-5 if it happens to be uniting two empty binomial heaps. From line 6 on,
therefore, we know that H has at least one root. Throughout the procedure, we maintain three pointers
into the root list:
x points to the root currently being examined,
prev-x points to the root preceding x on the root list: sibling[prev-x] = x, and
next-x points to the root following x on the root list: sibling[x] = next-x.
Initially, there are at most two roots on the root list H of a given degree: because H1 and H2 were
binomial heaps, they each had only one root of a given degree. Moreover, BINOMIAL-HEAP-MERGE
guarantees us that if two roots in H have the same degree, they are adjacent in the root list.
In fact, during the execution of BINOMlAL-HEAP-UNION, there may be three roots of a given degree
appearing on the root list H at some time. We shall see in a moment how this situation could occur. At
each iteration of the while loop of lines 9-21, therefore, we decide whether to link x and next-x based
on their degrees and possibly the degree of sibling[next-x]. An invariant of the loop is that each time we
start the body of the loop, both x and next-x are non-NIL.
Case 1, shown in Figure 20.6(a), occurs when degree[x] degree[next-x], that is, when x is the root of
a Bk-tree and next-x is the root of a Bl-tree for some l > k. Lines 11-12 handle this case. We don't link x
and next-x, so we simply march the pointers one position further down the list. Updating next-x to point
to the node following the new node x is handled in line 21, which is common to every case.
Case 2, shown in Figure 20.6(b), occurs when x is the first of three roots of equal degree, that is, when
degree[x] = degree[next-x] = degree[sibling[next-x]].
We handle this case in the same manner as case 1: we just march the pointers one position further
down the list. Line 10 tests for both cases 1 and 2, and lines 11-12 handle both cases.
Cases 3 and 4 occur when x is the first of two roots of equal degree, that is, when
degree[x] = degree[next-x] degree[sibling[next-x]].
These cases may occur on the next iteration after any case, but one of them always occurs immediately
following case 2. In cases 3 and 4, we link x and next-x. The two cases are distinguished by whether x
or next-x has the smaller key, which determines the node that will be the root after the two are linked.
In case 3, shown in Figure 20.6(c), key[x] key[next-x], so next-x is linked to x. Line 14 removes next-
x from the root list, and line 15 makes next-x the leftmost child of x.
Figure. The execution of BINOMIAL-HEAP-UNION
In case 4, next-x has the smaller key, so x is linked to next-x. Lines 16-18 remove x from the root list,
which has two cases depending on whether x is the first root on the list (line 17) or is not (line 18). Line
19 then makes x the leftmost child of next-x, and line 20 updates x for the next iteration. Following
either case 3 or case 4, the setup for the next iteration of the while loop is the same. We have just
linked two Bk-trees to form a Bk+l-tree, which x now points to. There were already zero, one, or two
other Bk+1-trees on the root list from the output of BINOMIAL-HEAP-MERGE, so x is now the first of
either one, two, or three Bk+l-trees on the root list. If x is the only one, then we enter case 1 in the next
iteration: degree [x] degree[next-x]. If x is the first of two, then we enter either case 3 or case 4 in the
next iteration. It is when x is the first of three that we enter case 2 in the next iteration. The running time
of BINOMIAL-HEAP-UNION is O(1g n), where n is the total number of nodes in binomial heaps H1 and
H2. We can see this as follows. Let H1 contain n1 nodes and H2 contain n2 nodes, so that n = n1 + n2.
Then, H1 contains at most 1g n1 + 1 roots and H2 contains at most 1g n2 + 1 roots, so H contains at most 1g
n2 + 1g n1 + 2 2 1g n + 2 = O(1g n) roots immediately after the call of BINOMIAL-HEAP-MERGE. The
time to perform BINOMIAL-HEAP-MERGE is thus O(lg n). Each iteration of the while loop takes O(1)
time, and there are at most 1g n1 + 1g n2 + 2 iterations because each iteration either advances the pointers one position down the
root list of H or removes a root from the root list. The total time is thus O(lg n).
6. 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.
BINOMIAL-HEAP-DECREASE-KEY (H, x, k)
1. if k > key[x]
2. then error "new key is greater than current key"
3. key[x] k
4. y x
5. z p[y]
6. while z NIL and key[y] < key[z]
7. do exchange key[y] key[z]
8. If y and z have satellite fields, exchange them,
too.
9. y z
10. z p[y]
As shown in Figure 20.8, this procedure decreases a key in the same manner as in a binary heap: by
"bubbling up" the key in the heap. After ensuring that the new key is in fact no greater than the
current key and then assigning the new key to x, the procedure goes up the tree, with y initially
pointing to node x. In each iteration of the while loop of lines 6-10, key[y] is checked against the key
of y's parent z. If y is the root or key[y] key[z], the binomial tree is now heap-ordered. Otherwise,
node y violates heap ordering, so its key is exchanged with the key of its parent z, along with any
other satellite information. The procedure then sets y to z, going up one level in the tree, and
continues with the next iteration.
The BINOMIAL-HEAP-DECREASE-KEY procedure takes O(lg n) time. By property 2 of Lemma 20.1,
the maximum depth of x is lg n , so the while loop of lines 6-10 iterates at most lg n times.
Figure. The action of BINOMIAL-HEAP-DECREASE-KEY. (a) The situation just before line 5 of the
first iteration of the while loop. Node y has had its key decreased to 7, which is less than the key of y's
parent z. (b) The keys of the two nodes are exchanged, and the situation just before line 5 of the second
iteration is shown. Pointers y and z have moved up one level in the tree, but heap order is still violated.
(c) After another exchange and moving pointers y and z up one more level, we finally find that heap
order is satisfied, so the while loop terminates.
7.Deleting a key
It is easy to delete a node x's key and satellite information from binomial heap H in O(lg n) time. The
following implementation assumes that no node currently in the binomial heap has a key of - .
BINOMIAL-HEAP-DELETE(H,x)
1 BINOMIAL-HEAP-DECREASE-KEY(H,x,- )
2 BINOMIAL-HEAP-EXTRACT-MIN(H)
The BINOMIAL-HEAP-DELETE procedure makes node x have the unique minimum key in the entire
binomial heap by giving it a key of - . It then bubbles this key and the associated satellite information
up to a root by calling BINOMIAL-HEAP-DECREASE-KEY. This root is then removed from H by a call
of BINOMIAL-HEAP-EXTRACT-MIN.
The BINOMIAL-HEAP-DELETE procedure takes O(lg n) time.
Important Questions
1. 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? [AKTU 2021-22, 2018-19, 2017-18]
2. Explain properties of Binomial Heap. Write an algorithm to perform uniting two Binomial Heaps. And
also, to find Minimum Key. [AKTU 2017-18]
3. Discuss the Cases and algorithm for Binomial Heap Union and Finding the Minimum Key in Binomial
Heap with their Complexity. Also Perform Binomial Heap Insert on A=<
10,6,7,8,9,11,14,15,16,2,3,4,5>
4. Define Binomial Tree. Explain properties of Binomial Heap with example.
5. Discuss the Binomial Heap Insert, Binomial Heap Extract Min and Binomial Heap Delete Key
Operation with its Complexity.
6. Show the binomial heap that results when the node with key 28 is deleted from the binomial heap
shown in the given figure.
7. Show the binomial heap that results when a node with key 24 is inserted into the binomial heap shown
in given figure.
8. Show that if root lists are kept in strictly decreasing order by degree (instead of strictly increasing
order), each of the binomial heap operations can be implemented without changing its asymptotic
running time.
Fibonacci Heap:
Fibonacci heap is designed and developed by Fredman and Tarjan in the year 1986.
Ingenious data structure and analysis.
Like a binomial heap, a Fibonacci heap is a collection of trees with looser structure.
Fibonacci heaps are called lazy data structures because they delay work as long as possible using the
field mark (i.e. black node).
Write down the properties of Fibonacci Heap.
Fibonacci Heap - A Fibonacci heap is defined as the collection of rooted-tree in which all the trees must
hold the property of Min-heap. That is, for all the nodes, the key value of the parent node should be
greater than the key value of the parent node:
1. It can have multiple trees of equal degrees, and each tree doesn't need to have 2^k nodes.
2. All the trees in the Fibonacci Heap are rooted but not ordered.
3. All the roots and siblings are stored in a separated circular-doubly-linked list.
4. The degree of a node is the number of its children. Node X -> degree = Number of X's children.
5. Each node has a mark-attribute in which it is marked TRUE or FALSE. The FALSE indicates the
node has not any of its children. The TRUE represents that the node has lost one child. The
newly created node is marked FALSE.
6. The potential function of the Fibonacci heap is F(FH) = t[FH] + 2 * m[FH]
7. The Fibonacci Heap (FH) has some important technicalities listed below:
1. min[FH] - Pointer points to the minimum node in the Fibonacci Heap
2. n[FH] - Determines the number of nodes
3. t[FH] - Determines the number of rooted trees
4. m[FH] - Determines the number of marked nodes
• It shows that although some individual operations may be expensive, on average the cost
per operation is small.
Average in this context does not mean that we’re averaging over a distribution of inputs.
• No probability is involved.
• We’re talking about average cost in the worst case.
The potential method is used to analyze the performance of Fibonacci heap operations.
• An upper bound on the total amortized cost is thus an upper bound on the total actual
cost for the sequence of operations.
Maximum degree
• The amortized analyses was performed with a known upper bound D(n) on the maximum
degree of any node in an n-node Fibonacci heap. (i.e. D(n) ≤ ⌊lg n⌋).
• To make an empty Fibonacci heap, the MAKE-FIB-HEAP procedure allocates and returns
the Fibonacci heap object H ,
• Because t(H) = 0 and m(H) = 0, the potential of the empty Fibonacci heap is
• Φ(𝐻) = 0.
• The amortized cost of MAKE-FIB-HEAP is thus equal to its O(1) actual cost.
Inserting a node: The following procedure inserts node x into Fibonacci heap H , assuming that the
node has already been allocated and that key[x] has already been filled in.
FIB-HEAP-INSERT(H, x)
1 degree[x] ← 0
2 p[x] ← NIL
3 child[x] ← NIL
4 left[x] ← x
5 right[x] ← x
6 mark[x] ← FALSE
7 concatenate the root list containing x with root list H
8 if min[H] = NIL or key[x] < key[min[H]]
9 then min[H] ← x
10 n[H] ← n[H] + 1
Inserting a node (Analysis)
To determine the amortized cost of FIB-HEAP-INSERT,
let
𝐻 be the input Fibonacci heap and
𝐻′ be the resulting Fibonacci heap.
Then,
𝑡(𝐻′) = 𝑡(𝐻) + 1 and
𝑚(𝐻′) = 𝑚(𝐻),
and the difference in potential cost is=
((𝑡(𝐻) + 1) + 2 𝑚(𝐻)) − (𝑡(𝐻) + 2 𝑚(𝐻)) = 1.
Since the actual cost is 𝑂(1),
the amortized cost = Actual cost+ Difference in potential cost (𝛥(𝛷))
= 𝑂(1) + 1 = 𝑂(1).
3.) Finding the minimum node
This procedure unites Fibonacci heaps H1 and H2, destroying H1 and H2 in the process. It simply
concatenates the root lists of H1 and H2 and then determines the new minimum node.
Basic Idea:
Step 1: Concatenate two Fibonacci heaps.
Step 2: Root lists are circular, doubly linked lists.
Uniting two Fibonacci heaps
FIB-HEAP-UNION(H1, H2)
1 H ← MAKE-FIB-HEAP()
2 min[H] ← min[H1]
3 Concatenate the root list of H2 with the root list of H
4 if (min[H1] = NIL) or (min[H2] ≠ NIL and min[H2] < min[H1])
5 then min[H] ← min[H2]
6 n[H] ← n[H1] + n[H2]
7 free the objects H1 and H2
8 return H
Uniting two Fibonacci heaps (Analysis)
FIB-HEAP-UNION(H1, H2)
1 H ← MAKE-FIB-HEAP()
2 min[H] ← min[H1]
3 Concatenate the root list of H2 with the root list of H
4 if (min[H1] = NIL) or (min[H2] ≠ NIL and min[H2] < min[H1])
5 then min[H] ← min[H2]
6 n[H] ← n[H1] + n[H2]
7 free the objects H1 and H2
8 return H
Lines 1-3 concatenate the root lists of H1 and H2 into a new root list H.
Lines 2, 4, and 5 set the minimum node of H ,
and line 6 sets n[H] to the total number of nodes.
The Fibonacci heap objects H1 and H2 are freed in line 7, and line 8 returns the resulting Fibonacci
heap H.
As in the FIB-HEAP-INSERT procedure, no consolidation of trees occurs. The change in potential is
= Φ(𝐻) − (Φ(𝐻1) + Φ(𝐻2))
= (𝑡(𝐻) + 2𝑚(𝐻)) − ((𝑡(𝐻1) + 2 𝑚(𝐻1)) + (𝑡(𝐻2) + 2 𝑚(𝐻2)))
= 0,
because 𝑡(𝐻) = 𝑡(𝐻1) + 𝑡(𝐻2) and 𝑚(𝐻) = 𝑚(𝐻1) + 𝑚(𝐻2).
The amortized cost of FIB-HEAPUNION is therefore equal to its 𝑂(1) actual cost.
5.) Extracting the minimum node
The process of extracting the minimum node is the most complicated of the operations presented in
this section.
Basic Idea:
Step 1: Delete min and concatenate its children into root list.
Step 2: Consolidate trees so that no two roots have same degree.
Extracting the minimum node
FIB-HEAP-EXTRACT-MIN(H)
1 z ← min[H]
2 if z ≠ NIL
3 then for each child x of z
4 do add x to the root list of H
5 p[x] ← NIL
6 remove z from the root list of H
7 if z = right[z]
8 then min[H] ← NIL
9 else min[H] ← right[z]
10 CONSOLIDATE(H)
11 n[H] ← n[H] - 1
12 return z.
Extracting the minimum node
CONSOLIDATE(H)
1 for i ← 0 to D(n[H])
2 do A[i] ← NIL
3 for each node w in the root list of H
4 do x ← w
5 d ← degree[x]
6 while A[d] ≠ NIL
7 do y ← A[d] ▹ Another node with the same degree as x.
8 if key[x] > key[y]
9 then exchange x ↔ y
10 FIB-HEAP-LINK(H, y, x)
11 A[d] ← NIL
12 d←d+1
13 A[d] ← x
14. min[H] ← NIL
15. for i ← 0 to D(n[H])
16. do if A[i] ≠ NIL
17. then add A[i] to the root list of H
18. if min[H] = NIL or key[A[i]] < key[min[H]]
19. then min[H] ← A[i]
FIB-HEAP-LINK(H, y, x)
1. remove y from the root list of H
2. make y a child of x, incrementing degree[x]
3. mark[y] ← FALSE
Extracting the minimum node(Analysis)
Notation:
𝐷(𝑛) = 𝑚𝑎𝑥𝑑𝑒𝑔𝑟𝑒𝑒 𝑜𝑓 𝑎𝑛𝑦 𝑛𝑜𝑑𝑒 𝑖𝑛 𝐹𝑖𝑏𝑜𝑛𝑎𝑐𝑐𝑖 ℎ𝑒𝑎𝑝 𝑤𝑖𝑡ℎ 𝑛 𝑛𝑜𝑑𝑒𝑠.
𝑡(𝐻) = 𝑁𝑢𝑚𝑏𝑒𝑟 𝑜𝑓 𝑡𝑟𝑒𝑒𝑠 𝑖𝑛 ℎ𝑒𝑎𝑝 𝐻.
𝑚(𝐻) = 𝑡ℎ𝑒 𝑛𝑢𝑚𝑏𝑒𝑟 𝑜𝑓 𝑚𝑎𝑟𝑘𝑒𝑑 𝑛𝑜𝑑𝑒𝑠 𝑖𝑛 𝐻.
𝛷(𝐻) = 𝑡(𝐻) + 2𝑚(𝐻).
Actual cost :
𝑂(𝐷(𝑛)): for loop in Fib-Heap-Extract-Min
𝐷(𝑛) + 𝑡(𝐻) − 1 : size of the root list
Total actual cost: 𝑂(𝐷(𝑛)) + 𝑡(𝐻)
Potential before extracting : 𝑡(𝐻) + 2𝑚(𝐻)
Potential after extracting : ≤ 𝐷(𝑛) + 1 + 2𝑚(𝐻)
At most D(n)+1 nodes remain on the list and no nodes become marked.
Thus the amortized cost is at most:
= 𝑂(𝐷(𝑛)) + 𝑡(𝐻) + [(𝐷(𝑛) + 1 + 2𝑚(𝐻)) – (𝑡(𝐻) + 2𝑚(𝐻))]
= 𝑂(𝐷(𝑛) + 𝑡(𝐻) − 𝑡(𝐻))
= 𝑂(𝐷(𝑛))
= 𝑂(log 𝑛)
[Note: An n node Binomial heap H consists of at most
⌊𝑙𝑜𝑔 𝑛⌋ + 1 binomial Tree]
6.) Decreasing a key
This pseudocode for the operation FIB-HEAP-DECREASE-KEY, work with an assumption that
removing a node from a linked list does not change any of the structural fields in the removed node.
Basic Idea:
Case 1: parent of x is unmarked.
decrease key of x to k
cut off link between x and its parent
mark parent
add tree rooted at x to root list, updating heap min pointer
Decreasing a key
Basic Idea:
Case 2: parent of x is marked.
decrease key of x to k
cut off link between x and its parent p[x], and add x to root list
cut off link between p[x] and p[p[x]], add p[x] to root list
> If p[p[x]] unmarked, then mark it.
CASCADING-CUT(H, y)
1 z ← p[y]
2 if z ≠ NIL
3 then if mark[y] = FALSE
4 then mark[y] ← TRUE
5 else CUT(H, y, z)
6 CASCADING-CUT(H, z)
Decreasing a key (Analysis)
The FIB-HEAP-DECREASE-KEY procedure takes O(1) time, plus the time to perform the cascading
cuts.
Suppose that CASCADING-CUT is recursively called c times from a given invocation of FIB-HEAP-
DECREASE-KEY.
Each call of CASCADING-CUT takes O(1) time exclusive of recursive calls.
Thus, the actual cost of FIB-HEAP-DECREASE-KEY, including all recursive calls, is O(c)
Each recursive call of CASCADING-CUT except for the last one, cuts a marked node and clears the
mark bit.
[Note: Last call of CASCADING-CUT may have marked a node]
After Decrease-key, there are at most 𝑡(𝐻) + 𝑐 trees, and at most 𝑚(𝐻) − 𝑐 + 2 marked nodes.
Thus the difference in potential cost is:
= [𝑡(𝐻) + 𝑐 + 2(𝑚(𝐻) − 𝑐 + 2)] − [𝑡(𝐻) + 2𝑚(𝐻)]
= 4−𝑐
Amortized cost:
𝑂(𝑐) + 4 − 𝑐 = 𝑂(1)
Data compression: It is used in Huffman encoding for compressing data. This is relevant in
industries dealing with data storage and transmission, such as telecommunications and data
centers.
Skip list in Data structure
A skip list is a probabilistic data structure. The skip list is used to store a sorted list of elements or data
with a linked list. It allows the process of the elements or data to view efficiently. In one single step, it
skips several elements of the entire list, which is why it is known as a skip list.
The skip list is an extended version of the linked list. It allows the user to search, remove, and insert
the element very quickly. It consists of a base list that includes a set of elements which maintains the
link hierarchy of the subsequent elements.
The lowest layer of the skip list is a common sorted linked list, and the top layers of the skip list are like
an "express line" where the elements are skipped.
sorted order.
– In skip list if a key x appears in level i, then it also appears in all levels below i.
More Rules
p=top
While(1){
p=p->next;
If (p->down == NULL )
return p->next;
p=p->down ; }
It will return 117.
Inserting new element X:
th level
Let's take an example to understand the working of the skip list. In this example, we have 14 nodes,
such that these nodes are divided into two layers, as shown in the diagram.
The lower layer is a common line that links all nodes, and the top layer is an express line that links only
the main nodes, as you can see in the diagram.
Suppose you want to find 47 in this example. You will start the search from the first node of the express
line and continue running on the express line until you find a node that is equal a 47 or more than 47.
You can see in the example that 47 does not exist in the express line, so you search for a node of less
than 47, which is 40. Now, you go to the normal line with the help of 40, and search the 47, as shown
in the diagram.
o Insertion operation: It is used to add a new node to a particular location in a specific situation.
o Deletion operation: It is used to delete a node in a specific situation.
o Search Operation: The search operation is used to search a particular node in a skip list.
1. It is used in distributed applications, and it represents the pointers and system in the distributed
applications.
2. It is used to implement a dynamic elastic concurrent queue with low lock contention.
3. It is also used with the QMap template class.
4. The indexing of the skip list is used in running median problems.
5. The skip list is used for the delta-encoding posting in the Lucene search.
1. A skip list is a data structure for maps that uses a randomized insertion algorithm q
1. Looking up data in a trie is faster in worst case as compared to imperfect hash table.
3. In trie if single key is associated with more than one value then it resembles buckets in hash table.
5. Sometimes data retrieval from tries is very much slower than hashing.
6. Representation of keys a string is complex. For example, representing floating point numbers using
strings is really complicated in tries.
8. Tries are not available in programming tool it. Hence implementation of tries has to be done from
scratch.
Trie
Why study Trie
In computer science, a trie, also called digital tree or prefix tree,isa type of k-arysearch tree, a tree data
structure used for locating specific keys from within a set. These keys are most often strings, with links
between nodes defined not by the entire key, but by individual characters. In order to access a key (to
recover its value, change it, or remove it), the trieis traversed depth-first, following the links between
nodes, which represent each character in the key.
Trie data structures are commonly used in predictive text or autocomplete dictionaries, and
approximate matching algorithms. Tries enable faster searches, occupy less space, especially when
the set contains large number of short strings, thus used in spell checking, hyphenation applications
and longest prefix match algorithms. However, if storing dictionary words is all that is required (i.e. there
is no need to store metadata associated with each word), a minimal deterministic acyclic finite state
automaton
(DAFSA) or radix tree would use less storage space than a trie. This is because DAFSAs and radix
trees can compress identical branches from the trie which correspond to the same suffixes (or parts)
of different words being stored. String dictionaries are also utilized in natural language processing, such
as finding lexicon of a text corpus.
Trie’s Properties:
• A multi-way tree.
Trie Introduction
A Trie is an advanced data structure that is sometimes also known as prefix tree or digital tree. It
is a tree that stores the data in an ordered and efficient way. We generally use trie's to store
strings. Each node of a trie can have as many as 26 references (pointers).
A character
A boolean value is used to implement whether this character represents the end of the word.
Tries in general are used to store English characters, hence each character can have 26 references.
Nodes in a trie do not store entire keys, instead, they store a part of the key(usually a character of the
string). When we traverse down from the root node to the leaf node, we can build the key from these
small parts of the key.
Let's build a trie by inserting some words in it. Below is a pictorial representation of the same, we
have 5 words, and then we are inserting these words one by one in our trie.
an have at most 26 references. Tries are not balanced in nature, unlike AVL trees.
There won't be any collisions hence making the worst performance better than a hash table
that is not implemented properly.
No need for hash functions.
Lookup time for a string in trie is O(k) where k = length of the word.
It can take even less than O(k) time when the word is not there in a trie.
Types:
• Standard Tries
• Compressed/Compact Tries
• Suffix Tries
Standard Trie Deletion
Three cases
class TrieNode {
boolean isEndOfWord;
TrieNode children[];
public TrieNode(){
isEndOfWord = false;
Note that we have two fields in the above TrieNode class as explained earlier, the boolean
isEndOfWord keyword and an array of Trie nodes named children. Now let's initialize the root node of
the trie class.
TrieNode root;
public Trie() {
There are two key functions in a trie data structure, these are:
Search
Insert
Insert in trie:
When we insert a character(part of a key) into a trie, we start from the root node and then search for a
reference, which corresponds to the first key character of the string whose character we are trying to
insert in the trie. Two scenarios are possible:
A reference exists, if, so then we traverse down the tree following the reference to the next
children level.
A reference does not exist, then we create a new node and refer it with parents reference
matching the current key character. We repeat this step until we get to the last character of the
key, then we mark the current node as an end node and the algorithm finishes.
Consider the code snippet below:
if (node.children[c-'a'] == null) {
node = node.children[c-'a'];
}
node.isEndOfWord = true;
Search in trie:
A key in a trie is stored as a path that starts from the root node and it might go all the way to the leaf
node or some intermediate node. If we want to search a key in a trie, we start with the root node and
then traverses downwards if we get a reference match for the next character of the key we are
searching, then there are two cases:
1. A reference of the next character exists, hence we move downwards following this link, and
proceed to search for the next key character.
2. A reference does not exist for the next character. If there are no more characters of the key
present and this character is marked as isEndOfWord = true, then we return true, implying that
we have found the key. Otherwise, two more cases are possible, and in each of them we
return false. These are:
1. There are key characters left in the key, but we cannot traverse down as the path is
terminated, hence the key doesn't exist.
2. No characters in the key are left, but the last character is not marked as isEndOfWord =
false. Therefore, the search key is just the prefix of the key we are trying to search in
the trie.
Consider the code snippet below:
public boolean isMatch( String s, TrieNode node, int index, boolean isFullMatch) {
if (node == null)
return false;
if (index == s.length())
return !isFullMatch || node.isEndOfWord;
The method startsWith() is used to find if the desired key prefix is present in the trie or not. Also, both
the search() and startsWith() methods make use of isMatch() method.
• The index of a search engine (collection of all searchable words) is stored into a compressed trie
• Each leaf of the trie is associated with a word and has a list of pages (URLs) containing that word,
called occurrence list
• The occurrence lists are kept in external memory and are ranked by relevance
• Boolean queries for sets of words (e.g., Java and coffee) correspond to set operations (e.g.,
intersection) on the occurrence lists
• Additional information retrieval techniques are used, such as – stopword elimination (e.g., ignore
“the” “a” “is”) – stemming (e.g., identify “add” “adding” “added”) – link analysis (recognize authoritative
pages)
University Questions