Advanced Data Structures
Advanced Data Structures
STRUCTURE
SOFTWARE ENGINEERING
Data Definition..................................................................................................................................... 20
Syntax ............................................................................................................................................. 28
Array Representation......................................................................................................................... 29
Basic Operations in the Arrays ......................................................................................................... 30
Algorithm ......................................................................................................................................... 31
Example ........................................................................................................................................... 32
Output................................................................................................................................................. 32
Algorithm ...................................................................................................................................... 33
Example ........................................................................................................................................... 33
Output................................................................................................................................................. 34
Search Operation................................................................................................................................. 34
Algorithm ...................................................................................................................................... 34
Example ........................................................................................................................................... 35
Output................................................................................................................................................. 36
Algorithm ...................................................................................................................................... 36
Example ........................................................................................................................................... 37
Output................................................................................................................................................. 37
Algorithm ...................................................................................................................................... 38
Example ........................................................................................................................................... 38
Output................................................................................................................................................. 39
Algorithm ......................................................................................................................................... 39
Example ........................................................................................................................................... 39
Output................................................................................................................................................. 40
Linked List Data Structure .................................................................................................................... 40
Algorithm ...................................................................................................................................... 46
Example ........................................................................................................................................... 46
Output................................................................................................................................................. 48
Algorithm ...................................................................................................................................... 48
Example ........................................................................................................................................... 49
Output................................................................................................................................................. 51
Algorithm ...................................................................................................................................... 51
Example ........................................................................................................................................... 52
Output................................................................................................................................................. 54
Algorithm ......................................................................................................................................... 56
Example ........................................................................................................................................... 56
Output................................................................................................................................................. 58
Deletion at Ending ................................................................................................................... 59
Algorithm ...................................................................................................................................... 59
Example ........................................................................................................................................... 59
Output................................................................................................................................................. 61
Algorithm ...................................................................................................................................... 62
Example ......................................................................................................................................... 62
Output................................................................................................................................................. 65
Algorithm ...................................................................................................................................... 66
Example ......................................................................................................................................... 67
Output................................................................................................................................................. 69
Search Operation................................................................................................................................. 69
Algorithm ...................................................................................................................................... 70
Example ......................................................................................................................................... 70
Output................................................................................................................................................. 72
Algorithm ...................................................................................................................................... 73
Example ........................................................................................................................................... 73
Output................................................................................................................................................. 75
Complete implementation................................................................................................................. 75
Output................................................................................................................................................. 79
Algorithm ...................................................................................................................................... 82
Example ........................................................................................................................................... 82
Output................................................................................................................................................. 84
Algorithm ...................................................................................................................................... 85
Example ......................................................................................................................................... 85
Output................................................................................................................................................. 88
Algorithm ...................................................................................................................................... 89
Example ........................................................................................................................................... 89
Output................................................................................................................................................. 92
Complete implementation................................................................................................................. 93
Output............................................................................................................................................... 102
Output............................................................................................................................................... 107
Output............................................................................................................................................... 114
Output............................................................................................................................................... 117
Output............................................................................................................................................... 121
Output............................................................................................................................................... 125
Output............................................................................................................................................... 127
Output............................................................................................................................................... 130
Output............................................................................................................................................... 132
Precedence................................................................................................................................. 135
Output............................................................................................................................................... 142
Output............................................................................................................................................... 146
Output............................................................................................................................................... 149
Output............................................................................................................................................... 152
Output............................................................................................................................................... 154
Output............................................................................................................................................... 155
Output............................................................................................................................................... 159
Output............................................................................................................................................... 171
Output............................................................................................................................................... 178
Output............................................................................................................................................... 186
Output............................................................................................................................................... 200
Output............................................................................................................................................... 204
Output............................................................................................................................................... 208
Output............................................................................................................................................... 212
Output............................................................................................................................................... 219
Output............................................................................................................................................... 222
Output............................................................................................................................................... 225
Output............................................................................................................................................... 227
Output............................................................................................................................................... 230
Output............................................................................................................................................... 234
LL Rotations............................................................................................................................... 235
Output............................................................................................................................................... 248
Output............................................................................................................................................... 259
Output............................................................................................................................................... 265
Output............................................................................................................................................... 284
B Trees..................................................................................................................................................... 285
Output............................................................................................................................................... 303
Insertion......................................................................................................................................... 305
Output............................................................................................................................................... 318
Insertion......................................................................................................................................... 329
Output............................................................................................................................................... 335
Output............................................................................................................................................... 340
Output............................................................................................................................................... 345
Output............................................................................................................................................... 349
Output............................................................................................................................................... 354
Output............................................................................................................................................... 358
Output............................................................................................................................................... 365
Output............................................................................................................................................... 370
Output............................................................................................................................................... 375
Output............................................................................................................................................... 381
Output............................................................................................................................................... 386
Data Structures
Data Structure is a systematic way to organize data in order to use it
efficiently. Following terms are the foundation terms of a data
structure.
As applications are getting complex and data rich, there are three
common problems that applications face now-a-days.
There are three cases which are usually used to compare various data
structure's execution time in a relative manner.
Basic Terminology
Environment Setup
Text Editor
The name and the version of the text editor can vary on different
operating systems. For example, Notepad will be used on Windows,
and vim or vi can be used on Windows as well as Linux or UNIX.
The files you create with your editor are called source files and contain
program source code. The source files for C programs are typically
named with the extension ".c".
Before starting your programming, make sure you have one text
editor in place and you have enough experience to write a computer
program, save it in a file, compile it, and finally execute it.
The C Compiler
The source code written in the source file is the human readable
source for your program. It needs to be "compiled", to turn into
machine language so that your CPU can actually execute the program
as per the given instructions.
Data Definition
Data Object
Data Type
Those data types for which a language has built-in support are known
as Built-in Data types. For example, most of the languages provide
the following built-in data types.
• Integers
• Boolean (true, false)
• Floating (Decimal numbers)
• Character and Strings
• List
• Array
• Stack
• Queue
Basic Operations
• Traversing
• Searching
• Insertion
• Deletion
• Sorting
• Merging
• Linear
• Non-Linear
Linear data structures are usually easy to implement but since the
memory allocation might become complicated, time and space
complexities increase. Few examples of linear data structures include
−
• Arrays
• Linked Lists
• Stacks
• Queues
Based on the data storage methods, these linear data structures are
divided into two sub-types. They are − static and dynamic data
structures.
Static Linear Data Structures
• Graphs
• Trees
• Tries
• Maps
Array Data Structure
Array is a type of linear data structure that is defined as a collection
of elements with same or different data types. They exist in both
single dimension and multiple dimensions. These data structures
come into picture when there is a necessity to store multiple elements
of similar nature together at one place.
Arrays are used as solutions to many problems from the small sorting
problems to more complex problems like travelling salesperson
problem. There are many data structures other than arrays that
provide efficient time and space complexity for these problems, so
what makes using arrays better? The answer lies in the random
access lookup time.
Array Representation
Insertion Operation
Algorithm
1. Start
2. Create an Array of a desired datatype and size.
3. Initialize a variable ‘i’ as 0.
4. Enter the element at ith index of the array.
5. Increment i by 1.
6. Repeat Steps 4 & 5 until the end of the array.
7. Stop
Here, we see a practical implementation of insertion operation, where
we add data at the end of the array −
Example
#include <stdio.h>
int main(){
int LA[3] = {}, i;
printf("Array Before Insertion:\n");
for(i = 0; i < 3; i++)
printf("LA[%d] = %d \n", i, LA[i]);
printf("Inserting Elements.. \n");
printf("The array elements after insertion :\n"); // prints array values
for(i = 0; i < 3; i++) {
LA[i] = i + 2;
printf("LA[%d] = %d \n", i, LA[i]);
}
return 0;
}
Output
Array Before Insertion:
LA[0] = 0
LA[1] = 0
LA[2] = 0
Inserting Elements..
The array elements after insertion :
LA[0] = 2
LA[1] = 3
LA[2] = 4
For other variations of array insertion operation, click here.
Deletion Operation
Algorithm
1. Start
2. Set J = K
3. Repeat steps 4 and 5 while J < N
4. Set LA[J] = LA[J + 1]
5. Set J = J+1
6. Set N = N-1
7. Stop
Example
#include <stdio.h>
void main(){
int LA[] = {1,3,5};
int n = 3;
int i;
printf("The original array elements are :\n");
for(i = 0; i<n; i++)
printf("LA[%d] = %d \n", i, LA[i]);
for(i = 1; i<n; i++) {
LA[i] = LA[i+1];
n = n - 1;
}
printf("The array elements after deletion :\n");
for(i = 0; i<n; i++)
printf("LA[%d] = %d \n", i, LA[i]);
}
Output
The original array elements are :
LA[0] = 1
LA[1] = 3
LA[2] = 5
The array elements after deletion :
LA[0] = 1
LA[1] = 5
Search Operation
Algorithm
Consider LA is a linear array with N elements and K is a positive
integer such that K<=N. Following is the algorithm to find an element
with a value of ITEM using sequential search.
1. Start
2. Set J = 0
3. Repeat steps 4 and 5 while J < N
4. IF LA[J] is equal ITEM THEN GOTO STEP 6
5. Set J = J +1
6. PRINT J, ITEM
7. Stop
Example
#include <stdio.h>
void main(){
int LA[] = {1,3,5,7,8};
int item = 5, n = 5;
int i = 0, j = 0;
printf("The original array elements are :\n");
for(i = 0; i<n; i++) {
printf("LA[%d] = %d \n", i, LA[i]);
}
for(i = 0; i<n; i++) {
if( LA[i] == item ) {
printf("Found element %d at position %d\n", item, i+1);
}
}
}
Output
The original array elements are :
LA[0] = 1
LA[1] = 3
LA[2] = 5
LA[3] = 7
LA[4] = 8
Found element 5 at position 3
Traversal Operation
Algorithm
1 Start
2. Initialize an Array of certain size and datatype.
3. Initialize another variable ‘i’ with 0.
4. Print the ith value in the array and increment i.
5. Repeat Step 4 until the end of the array is reached.
6. End
Example
C C++JavaPython
#include <stdio.h>
int main(){
int LA[] = {1,3,5,7,8};
int item = 10, k = 3, n = 5;
int i = 0, j = n;
printf("The original array elements are :\n");
for(i = 0; i<n; i++) {
printf("LA[%d] = %d \n", i, LA[i]);
}
}
Output
The original array elements are :
LA[0] = 1
LA[1] = 3
LA[2] = 5
LA[3] = 7
LA[4] = 8
Update Operation
1. Start
2. Set LA[K-1] = ITEM
3. Stop
Example
#include <stdio.h>
void main(){
int LA[] = {1,3,5,7,8};
int k = 3, n = 5, item = 10;
int i, j;
printf("The original array elements are :\n");
for(i = 0; i<n; i++) {
printf("LA[%d] = %d \n", i, LA[i]);
}
LA[k-1] = item;
printf("The array elements after updation :\n");
for(i = 0; i<n; i++) {
printf("LA[%d] = %d \n", i, LA[i]);
}
}
Output
The original array elements are :
LA[0] = 1
LA[1] = 3
LA[2] = 5 LA[3] = 7
LA[4] = 8
The array elements after updation :
LA[0] = 1
LA[1] = 3
LA[2] = 10
LA[3] = 7
LA[4] = 8
Display Operation
This operation displays all the elements in the entire array using a
print statement.
Algorithm
1. Start
2. Print all the elements in the Array
3. Stop
Example
Following are the implementations of this operation in various
programming languages −
#include <stdio.h>
int main(){
int LA[] = {1,3,5,7,8};
int n = 5;
int i;
printf("The original array elements are :\n");
for(i = 0; i<n; i++) {
printf("LA[%d] = %d \n", i, LA[i]);
}
}
Output
The original array elements are :
LA[0] = 1
LA[1] = 3
LA[2] = 5
LA[3] = 7
LA[4] = 8
• Singly Linked List − The nodes only point to the address of the
next node in the list.
• Doubly Linked List − The nodes point to the addresses of both
previous and next nodes.
• Circular Linked List − The last node in the list will point to the
first node in the list. It can either be singly linked or doubly
linked.
Doubly Linked Lists contain three “buckets” in one node; one bucket
holds the data and the other buckets hold the addresses of the
previous and next nodes in the list. The list is traversed twice as the
nodes in the list are connected to each other from both sides.
Circular linked lists can exist in both singly linked list and doubly
linked list.
Since the last node and the first node of the circular linked list are
connected, the traversal in this linked list will go on forever until it is
broken.
Basic Operations in the Linked Lists
Insertion Operation
Adding a new node in linked list is a more than one step activity. We
shall learn this with diagrams here. First, create a node using the
same structure and find the location where it has to be inserted.
Imagine that we are inserting a node B (NewNode), between A
(LeftNode) and C (RightNode). Then point B.next to C −
Now, the next node at the left should point to the new node.
Insertion in linked list can be done in three different ways. They are
explained as follows −
Insertion at Beginning
Algorithm
1. START
2. Create a node to store the data
3. Check if the list is empty
4. If the list is empty, add the data to the node and assign the head pointer to it.
5 If the list is not empty, add the data to a node and link to the current head. Assign the head to
the newly added node.
6. END
Example
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
struct node {
int data;
struct node *next;
};
struct node *head = NULL;
struct node *current = NULL;
//create a link
struct node *lk = (struct node*) malloc(sizeof(struct node));
lk->data = data;
// print list
printList();
}
Output
Linked List:
[ 50 44 30 22 12 ]
Insertion at Ending
Algorithm
1. START
2. Create a new node and assign the data
3. Find the last node
4. Point the last node to new node
5. END
Example
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
struct node {
int data;
struct node *next;
};
struct node *head = NULL;
struct node *current = NULL;
//create a link
struct node *lk = (struct node*) malloc(sizeof(struct node));
lk->data = data;
//create a link
struct node *lk = (struct node*) malloc(sizeof(struct node));
lk->data = data;
struct node *linkedlist = head;
// print list
printList();
}
Output
Linked List:
[ 12 22 30 44 50 ]
Algorithm
1. START
2. Create a new node and assign data to it
3. Iterate until the node at position is found
4. Point first to new first node
5. END
Example
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
struct node {
int data;
struct node *next;
};
struct node *head = NULL;
struct node *current = NULL;
//create a link
struct node *lk = (struct node*) malloc(sizeof(struct node));
lk->data = data;
// print list
printList();
}
Output
Linked List:
[ 22 12 30 ]
Deletion Operation
Deletion is also a more than one step process. We shall learn with
pictorial representation. First, locate the target node to be removed,
by using searching algorithms.
The left (previous) node of the target node now should point to the
next node of the target node −
Algorithm
1. START
2. Assign the head pointer to the next node in the list
3. END
Example
C C++JavaPython
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
struct node {
int data;
struct node *next;
};
struct node *head = NULL;
struct node *current = NULL;
//create a link
struct node *lk = (struct node*) malloc(sizeof(struct node));
lk->data = data;
// print list
printList();
deleteatbegin();
printf("\nLinked List after deletion: ");
// print list
printList();
}
Output
Linked List:
[ 55 40 30 22 12 ]
Linked List after deletion:
[ 40 30 22 12 ]
Deletion at Ending
Algorithm
1. START
2. Iterate until you find the second last element in the list.
3. Assign NULL to the second last element in the list.
4. END
Example
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
struct node {
int data;
struct node *next;
};
struct node *head = NULL;
struct node *current = NULL;
//create a link
struct node *lk = (struct node*) malloc(sizeof(struct node));
lk->data = data;
// print list
printList();
deleteatend();
printf("\nLinked List after deletion: ");
// print list
printList();
}
Output
Linked List:
[ 55 40 30 22 12 ]
Linked List after deletion:
[ 55 40 30 22 ]
Deletion at a Given Position
Algorithm
1. START
2. Iterate until find the current node at position in the list
3. Assign the adjacent node of current node in the list to its previous node.
4. END
Example
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
struct node {
int data;
struct node *next;
};
struct node *head = NULL;
struct node *current = NULL;
//create a link
struct node *lk = (struct node*) malloc(sizeof(struct node));
lk->data = data;
// print list
printList();
deletenode(30);
printf("\nLinked List after deletion: ");
// print list
printList();
}
Output
Linked List:
[ 55 40 30 22 12 ]
Linked List after deletion:
[ 55 40 22 12 ]
Reverse Operation
Except the node (first node) pointed by the head node, all nodes
should point to their predecessor, making them their new successor.
The first node will point to NULL.
We'll make the head node point to the new first node by using the
temp node.
Algorithm
Example
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
struct node {
int data;
struct node *next;
};
struct node *head = NULL;
struct node *current = NULL;
//create a link
struct node *lk = (struct node*) malloc(sizeof(struct node));
lk->data = data;
// print list
printList();
reverseList(&head);
printf("\nReversed Linked List: ");
printList();
}
Output
Linked List:
[ 55 40 30 22 12 ]
Reversed Linked List:
[ 12 22 30 40 55 ]
Search Operation
1 START
2 If the list is not empty, iteratively check if the list contains the key
3 If the key element is not present in the list, unsuccessful search
4 END
Example
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
struct node {
int data;
struct node *next;
};
struct node *head = NULL;
struct node *current = NULL;
//create a link
struct node *lk = (struct node*) malloc(sizeof(struct node));
lk->data = data;
// print list
printList();
k = searchlist(30);
if (k == 1)
printf("\nElement is found");
else
printf("\nElement is not present in the list");
}
Output
Linked List:
[ 55 40 30 22 12 ]
Element is found
Traversal Operation
The traversal operation walks through all the elements of the list in
an order and displays the elements in that order.
Algorithm
1. START
2. While the list is not empty and did not reach the end of the list, print the data in each node
3. END
Example
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
struct node {
int data;
struct node *next;
};
struct node *head = NULL;
struct node *current = NULL;
//create a link
struct node *lk = (struct node*) malloc(sizeof(struct node));
lk->data = data;
// print list
printList();
}
Output
Linked List:
[ 30 22 12 ]
Complete implementation
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
struct node {
int data;
struct node *next;
};
struct node *head = NULL;
struct node *current = NULL;
//create a link
struct node *lk = (struct node*) malloc(sizeof(struct node));
lk->data = data;
struct node *linkedlist = head;
// print list
printList();
deleteatbegin();
deleteatend();
deletenode(12);
printf("\nLinked List after deletion: ");
// print list
printList();
insertatbegin(4);
insertatbegin(16);
printf("\nUpdated Linked List: ");
printList();
k = searchlist(16);
if (k == 1)
printf("\nElement is found");
else
printf("\nElement is not present in the list");
}
Output
Linked List:
[ 50 22 12 33 30 44 ]
Linked List after deletion:
[ 22 33 30 ]
Updated Linked List:
[ 16 4 22 33 30 ]
Element is found
Doubly Linked List Data Structure
Doubly Linked List is a variation of Linked list in which navigation is
possible in both ways, either forward and backward easily as
compared to Single Linked List. Following are the important terms to
understand the concept of doubly linked list.
• Doubly Linked List contains a link element called first and last.
• Each link carries a data field(s) and a link field called next.
• Each link is linked with its next link using its next link.
• Each link is linked with its previous link using its previous link.
• The last link carries a link as null to mark the end of the list.
Basic Operations
1. START
2. Create a new node with three variables: prev, data, next.
3. Store the new data in the data variable
4. If the list is empty, make the new node as head.
5. Otherwise, link the address of the existing first node to the next variable of the new node, and
assign null to the prev variable.
6. Point the head to the new node.
7. END
Example
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdbool.h>
struct node {
int data;
int key;
struct node *next;
struct node *prev;
};
//create a link
struct node *link = (struct node*) malloc(sizeof(struct node));
link->key = key;
link->data = data;
if(isEmpty()) {
//make it the last link
last = link;
} else {
This deletion operation deletes the existing first nodes in the doubly
linked list. The head is shifted to the next node and the link is
removed.
Algorithm
1. START
2. Check the status of the doubly linked list
3. If the list is empty, deletion is not possible
4. If the list is not empty, the head pointer is shifted to the next node.
5. END
Example
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdbool.h>
struct node {
int data;
int key;
struct node *next;
struct node *prev;
};
//this link always point to first Link
struct node *head = NULL;
//create a link
struct node *link = (struct node*) malloc(sizeof(struct node));
link->key = key;
link->data = data;
if(isEmpty()) {
In this insertion operation, the new input node is added at the end of
the doubly linked list; if the list is not empty. The head will be pointed
to the new node, if the list is empty.
Algorithm
1. START
2. If the list is empty, add the node to the list and point the head to it.
3. If the list is not empty, find the last node of the list.
4. Create a link between the last node in the list and the new node.
5. The new node will point to NULL as it is the new last node.
6. END
Example
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdbool.h>
struct node {
int data;
int key;
struct node *next;
struct node *prev;
};
//this link always point to first Link
struct node *head = NULL;
//create a link
struct node *link = (struct node*) malloc(sizeof(struct node));
link->key = key;
link->data = data;
if(isEmpty()) {
//create a link
struct node *link = (struct node*) malloc(sizeof(struct node));
link->key = key;
link->data = data;
if(isEmpty()) {
//make it the last link
last = link;
} else {
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdbool.h>
struct node {
int data;
int key;
struct node *next;
struct node *prev;
};
//print data
printf("(%d,%d) ",ptr->key,ptr->data);
//create a link
struct node *link = (struct node*) malloc(sizeof(struct node));
link->key = key;
link->data = data;
if(isEmpty()) {
//create a link
struct node *link = (struct node*) malloc(sizeof(struct node));
link->key = key;
link->data = data;
if(isEmpty()) {
//create a link
struct node *newLink = (struct node*) malloc(sizeof(struct node));
newLink->key = key;
newLink->data = data;
if(current == last) {
newLink->next = NULL;
last = newLink;
} else {
newLink->next = current->next;
current->next->prev = newLink;
}
newLink->prev = current;
current->next = newLink;
return true;
}
int main(){
insertFirst(1,10);
insertFirst(2,20);
insertFirst(3,30);
insertFirst(4,1);
insertFirst(5,40);
insertFirst(6,56);
printf("\nList (First to Last): ");
displayForward();
printf("\n");
printf("\nList (Last to first): ");
displayBackward();
printf("\nList , after deleting first record: ");
deleteFirst();
displayForward();
printf("\nList , after deleting last record: ");
deleteLast();
displayForward();
printf("\nList , insert after key(4) : ");
insertAfter(4,7, 13);
displayForward();
printf("\nList , after delete key(4) : ");
delete(4);
displayForward();
}
Output
List (First to Last):
[ (6,56) (5,40) (4,1) (3,30) (2,20) (1,10) ]
In singly linked list, the next pointer of the last node points to the
first node.
In doubly linked list, the next pointer of the last node points to the
first node and the previous pointer of the first node points to the last
node making the circular in both directions.
Basic Operations
Insertion Operation
Algorithm
1. START
2. Check if the list is empty
3. If the list is empty, add the node and point the head to this node
4. If the list is not empty, link the existing head as the next node to the new node.
5. Make the new node as the new head.
6. END
Example
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdbool.h>
struct node {
int data;
int key;
struct node *next;
};
struct node *head = NULL;
struct node *current = NULL;
bool isEmpty(){
return head == NULL;
}
//create a link
struct node *link = (struct node*) malloc(sizeof(struct node));
link->key = key;
link->data = data;
if (isEmpty()) {
head = link;
head->next = head;
} else {
//print list
printList();
}
Output
Circular Linked List:
[ (6,56) (5,40) (4,1) (3,30) (2,20) ]
Deletion Operation
Algorithm
1. START
2. If the list is empty, then the program is returned.
3. If the list is not empty, we traverse the list using a current pointer that is set to the head pointer
and create another pointer previous that points to the last node.
4. Suppose the list has only one node, the node is deleted by setting the head pointer to NULL.
5. If the list has more than one node and the first node is to be deleted, the head is set to the next
node and the previous is linked to the new head.
6. If the node to be deleted is the last node, link the preceding node of the last node to head node.
7. If the node is neither first nor last, remove the node by linking its preceding node to its
succeeding node.
8. END
Example
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdbool.h>
struct node {
int data;
int key;
struct node *next;
};
struct node *head = NULL;
struct node *current = NULL;
bool isEmpty(){
return head == NULL;
}
//create a link
struct node *link = (struct node*) malloc(sizeof(struct node));
link->key = key;
link->data = data;
if (isEmpty()) {
head = link;
head->next = head;
} else {
The Display List operation visits every node in the list and prints them
all in the output.
Algorithm
1. START
2. Walk through all the nodes of the list and print them
3. END
Example
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdbool.h>
struct node {
int data;
int key;
struct node *next;
};
struct node *head = NULL;
struct node *current = NULL;
bool isEmpty(){
return head == NULL;
}
//create a link
struct node *link = (struct node*) malloc(sizeof(struct node));
link->key = key;
link->data = data;
if (isEmpty()) {
head = link;
head->next = head;
} else {
Complete implementation
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdbool.h>
struct node {
int data;
int key;
struct node *next;
};
struct node *head = NULL;
struct node *current = NULL;
bool isEmpty(){
return head == NULL;
}
int length(){
int length = 0;
//if list is empty
if(head == NULL) {
return 0;
}
current = head->next;
while(current != head) {
length++;
current = current->next;
}
return length;
}
//insert link at the first location
void insertFirst(int key, int data){
//create a link
struct node *link = (struct node*) malloc(sizeof(struct node));
link->key = key;
link->data = data;
if (isEmpty()) {
head = link;
head->next = head;
} else {
//point it to old first node
link->next = head;
The stack follows the LIFO (Last in - First out) structure where the
last element inserted would be the first element deleted.
Stack Representation
A Stack ADT allows all data operations at one end only. At any given
time, we can only access the top element of a stack.
Stack uses pointers that always point to the topmost element within
the stack, hence called as the top pointer.
Insertion: push()
Algorithm
Example
#include <stdio.h>
int MAXSIZE = 8;
int stack[8];
int top = -1;
/* Main function */
int main(){
int i;
push(44);
push(10);
push(62);
push(123);
push(15);
printf("Stack Elements: \n");
Algorithm
Example
#include <stdio.h>
int MAXSIZE = 8;
int stack[8];
int top = -1;
/* Main function */
int main(){
int i;
push(44);
push(10);
push(62);
push(123);
push(15);
printf("Stack Elements: \n");
peek()
Algorithm
1. START
2. return the element at the top of the stack
3. END
Example
#include <stdio.h>
int MAXSIZE = 8;
int stack[8];
int top = -1;
/* Check if the stack is full */
int isfull(){
if(top == MAXSIZE)
return 1;
else
return 0;
}
/* Main function */
int main(){
int i;
push(44);
push(10);
push(62);
push(123);
push(15);
printf("Stack Elements: \n");
isFull()
Algorithm
1. START
2. If the size of the stack is equal to the top position of the stack, the stack is full. Return 1.
3. Otherwise, return 0.
4. END
Example
#include <stdio.h>
int MAXSIZE = 8;
int stack[8];
int top = -1;
/* Main function */
int main(){
printf("Stack full: %s\n" , isfull()?"true":"false");
return 0;
}
Output
Stack full: false
isEmpty()
Algorithm
1. START
2. If the top value is -1, the stack is empty. Return 1.
3. Otherwise, return 0.
4. END
Example
#include <stdio.h>
int MAXSIZE = 8;
int stack[8];
int top = -1;
Complete implementation
#include <stdio.h>
int MAXSIZE = 8;
int stack[8];
int top = -1;
/* Check if the stack is empty */
int isempty(){
if(top == -1)
return 1;
else
return 0;
}
/* Check if the stack is full */
int isfull(){
if(top == MAXSIZE)
return 1;
else
return 0;
}
• Infix Notation
• Prefix (Polish) Notation
• Postfix (Reverse-Polish) Notation
Infix Notation
Prefix Notation
The following table briefly tries to show the difference in all three
notations −
Parsing Expressions
Associativity
The above table shows the default behavior of operators. At any point
of time in expression evaluation, the order can be altered by using
parenthesis. For example −
#include<stdio.h>
#include<string.h>
#include<ctype.h>
//char stack
char stack[25];
int top = -1;
void push(char item) {
stack[++top] = item;
}
char pop() {
return stack[top--];
}
//returns precedence of operators
int precedence(char symbol) {
switch(symbol) {
case '+':
case '-':
return 2;
break;
case '*':
case '/':
return 3;
break;
case '^':
return 4;
break;
case '(':
case ')':
case '#':
return 1;
break;
}
}
switch(symbol) {
case '+':
case '-':
case '*':
case '/':
case '^':
case '(':
case ')':
return 1;
break;
default:
return 0;
}
}
//converts infix expression to postfix
void convert(char infix[],char postfix[]) {
int i,symbol,j = 0;
stack[++top] = '#';
for(i = 0;i<strlen(infix);i++) {
symbol = infix[i];
if(isOperator(symbol) == 0) {
postfix[j] = symbol;
j++;
} else {
if(symbol == '(') {
push(symbol);
} else {
if(symbol == ')') {
while(stack[top] != '(') {
postfix[j] = pop();
j++;
}
push(symbol);
}
}
}
}
}
while(stack[top] != '#') {
postfix[j] = pop();
j++;
}
//int stack
int stack_int[25];
int top_int = -1;
char pop_int() {
return stack_int[top_int--];
}
char ch;
int i = 0,operand1,operand2;
if(isdigit(ch)) {
push_int(ch-'0'); // Push the operand
} else {
//Operator,pop two operands
operand2 = pop_int();
operand1 = pop_int();
switch(ch) {
case '+':
push_int(operand1+operand2);
break;
case '-':
push_int(operand1-operand2);
break;
case '*':
push_int(operand1*operand2);
break;
case '/':
push_int(operand1/operand2);
break;
}
}
}
return stack_int[top_int];
}
void main() {
char infix[25] = "1*(2+3)",postfix[25];
convert(infix,postfix);
Representation of Queues
Similar to the stack ADT, a queue ADT can also be implemented using
arrays, linked lists, or pointers. As a small example in this tutorial,
we implement queues using a one-dimensional array.
Basic Operations
Queue uses two pointers − front and rear. The front pointer accesses
the data from the front end (helping in enqueueing) while the rear
pointer accesses data from the rear end (helping in
dequeuing).
Algorithm
1 − START
2 – Check if the queue is full.
3 − If the queue is full, produce overflow error and exit.
4 − If the queue is not full, increment rear pointer to point the next empty space.
5 − Add data element to the queue location, where the rear is pointing.
6 − return success.
7 – END
Example
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdbool.h>
#define MAX 6
int intArray[MAX];
int front = 0;
int rear = -1;
int itemCount = 0;
bool isFull(){
return itemCount == MAX;
}
bool isEmpty(){
return itemCount == 0;
}
int removeData(){
int data = intArray[front++];
if(front == MAX) {
front = 0;
}
itemCount--;
return data;
}
void insert(int data){
if(!isFull()) {
if(rear == MAX-1) {
rear = -1;
}
intArray[++rear] = data;
itemCount++;
}
}
int main(){
insert(3);
insert(5);
insert(9);
insert(1);
insert(12);
insert(15);
printf("Queue: ");
while(!isEmpty()) {
int n = removeData();
printf("%d ",n);
}
}
Output
Queue: 3 5 9 1 12 15
Deletion Operation: dequeue()
Algorithm
1 – START
2 − Check if the queue is empty.
3 − If the queue is empty, produce underflow error and exit.
4 − If the queue is not empty, access the data where front is pointing.
5 − Increment front pointer to point to the next available data element.
6 − Return success.
7 – END
Example
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdbool.h>
#define MAX 6
int intArray[MAX];
int front = 0;
int rear = -1;
int itemCount = 0;
bool isFull(){
return itemCount == MAX;
}
bool isEmpty(){
return itemCount == 0;
}
void insert(int data){
if(!isFull()) {
if(rear == MAX-1) {
rear = -1;
}
intArray[++rear] = data;
itemCount++;
}
}
int removeData(){
int data = intArray[front++];
if(front == MAX) {
front = 0;
}
itemCount--;
return data;
}
int main(){
int i;
/* insert 5 items */
insert(3);
insert(5);
insert(9);
insert(1);
insert(12);
insert(15);
printf("Queue: ");
for(i = 0; i < MAX; i++)
printf("%d ", intArray[i]);
Algorithm
1 – START
2 – Return the element at the front of the queue
3 – END
Example
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdbool.h>
#define MAX 6
int intArray[MAX];
int front = 0;
int rear = -1;
int itemCount = 0;
int peek(){
return intArray[front];
}
bool isFull(){
return itemCount == MAX;
}
void insert(int data){
if(!isFull()) {
if(rear == MAX-1) {
rear = -1;
}
intArray[++rear] = data;
itemCount++;
}
}
int main(){
int i;
/* insert 5 items */
insert(3);
insert(5);
insert(9);
insert(1);
insert(12);
insert(15);
printf("Queue: ");
for(i = 0; i < MAX; i++)
printf("%d ", intArray[i]);
printf("\nElement at front: %d\n",peek());
}
Output
Queue: 3 5 9 1 12 15
Element at front: 3
Algorithm
1 – START
2 – If the count of queue elements equals the queue size, return true
3 – Otherwise, return false
4 – END
Example
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdbool.h>
#define MAX 6
int intArray[MAX];
int front = 0;
int rear = -1;
int itemCount = 0;
bool isFull(){
return itemCount == MAX;
}
void insert(int data){
if(!isFull()) {
if(rear == MAX-1) {
rear = -1;
}
intArray[++rear] = data;
itemCount++;
}
}
int main(){
int i;
/* insert 5 items */
insert(3);
insert(5);
insert(9);
insert(1);
insert(12);
insert(15);
printf("Queue: ");
for(i = 0; i < MAX; i++)
printf("%d ", intArray[i]);
printf("\n");
if(isFull()) {
printf("Queue is full!\n");
}
}
Output
Queue: 3 5 9 1 12 15
Queue is full!
Algorithm
1 – START
2 – If the count of queue elements equals zero, return true
3 – Otherwise, return false
4 – END
Example
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdbool.h>
#define MAX 6
int intArray[MAX];
int front = 0;
int rear = -1;
int itemCount = 0;
bool isEmpty(){
return itemCount == 0;
}
int main(){
int i;
printf("Queue: ");
for(i = 0; i < MAX; i++)
printf("%d ", intArray[i]);
printf("\n");
if(isEmpty()) {
printf("Queue is Empty!\n");
}
}
Output
Queue: 0 0 0 0 0 0
Queue is Empty!
Implementation of Queue
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdbool.h>
#define MAX 6
int intArray[MAX];
int front = 0;
int rear = -1;
int itemCount = 0;
int peek(){
return intArray[front];
}
bool isEmpty(){
return itemCount == 0;
}
bool isFull(){
return itemCount == MAX;
}
int size(){
return itemCount;
}
void insert(int data){
if(!isFull()) {
if(rear == MAX-1) {
rear = -1;
}
intArray[++rear] = data;
itemCount++;
}
}
int removeData(){
int data = intArray[front++];
if(front == MAX) {
front = 0;
}
itemCount--;
return data;
}
int main(){
/* insert 5 items */
insert(3);
insert(5);
insert(9);
insert(1);
insert(12);
// front : 0
// rear : 4
// ------------------
// index : 0 1 2 3 4
// ------------------
// queue : 3 5 9 1 12
insert(15);
// front : 0
// rear : 5
// ---------------------
// index : 0 1 2 3 4 5
// ---------------------
// queue : 3 5 9 1 12 15
if(isFull()) {
printf("Queue is full!\n");
}
// front : 1
// rear : 5
// -------------------
// index : 1 2 3 4 5
// -------------------
// queue : 5 9 1 12 15
// insert more items
insert(16);
// front : 1
// rear : -1
// ----------------------
// index : 0 1 2 3 4 5
// ----------------------
// queue : 16 5 9 1 12 15
// As queue is full, elements will not be inserted.
insert(17);
insert(18);
// ----------------------
// index : 0 1 2 3 4 5
// ----------------------
// queue : 16 5 9 1 12 15
printf("Element at front: %d\n",peek());
printf("----------------------\n");
printf("index : 5 4 3 2 1 0\n");
printf("----------------------\n");
printf("Queue: ");
while(!isEmpty()) {
int n = removeData();
printf("%d ",n);
}
}
Output
Queue is full!
Element removed: 3
Element at front: 5
----------------------
index : 5 4 3 2 1 0
----------------------
Queue: 5 9 1 12 15 16
Graph Data Structure
A graph is an abstract data type (ADT) that consists of a set of objects
that are connected to each other via links. These objects are
called vertices and the links are called edges.
Operations of Graphs
Depth First Search is a traversal algorithm that visits all the vertices
of a graph in the decreasing order of its depth. In this algorithm, an
arbitrary node is chosen as the starting point and the graph is
traversed back and forth by marking unvisited adjacent nodes until
all the vertices are marked.
The DFS traversal uses the stack data structure to keep track of the
unvisited nodes.
Breadth First Search is a traversal algorithm that visits all the vertices
of a graph present at one level of the depth before moving to the next
level of depth. In this algorithm, an arbitrary node is chosen as the
starting point and the graph is traversed by visiting the adjacent
vertices on the same depth level and marking them until there is no
vertex left.
The DFS traversal uses the queue data structure to keep track of the
unvisited nodes.
Representation of Graphs
• Adjacency Matrix
• Adjacency List
Adjacency Matrix
The Adjacency Matrix is a V×V matrix where the values are filled with
either 0 or 1. If the link exists between Vi and Vj, it is recorded 1;
otherwise, 0.
Adjacency List
Types of graph
• Directed Graph
• Undirected Graph
Undirected Graph
Spanning Tree
If all the vertices are connected in a graph, then there exists at least
one spanning tree. In a graph, there may exist more than one
spanning tree.
Properties
As we have discussed, one graph may have more than one spanning
tree. If there are n number of vertices, the spanning tree should have
𝒏−𝟏 number of edges. In this context, if each edge of the graph is
associated with a weight and there exists more than one spanning
tree, we need to find the minimum spanning tree of the graph.
Moreover, if there exist any duplicate weighted edges, the graph may
have multiple minimum spanning tree.
In the above graph, we have shown a spanning tree though it’s not
the minimum spanning tree. The cost of this spanning tree
is (5+7+3+3+5+8+3+4)=38.
Shortest Path
#include <stdio.h>
#include<stdlib.h>
#include <stdlib.h>
#define V 5
// declaring vertices
int end;
struct vertex *next;
};
struct Edge {
// declaring edges
int end, start;
};
struct graph *create_graph (struct Edge edges[], int x){
int i;
struct graph *graph = (struct graph *) malloc (sizeof (struct graph));
for (i = 0; i < V; i++) {
graph->point[i] = NULL;
}
for (i = 0; i < x; i++) {
int start = edges[i].start;
int end = edges[i].end;
struct vertex *v = (struct vertex *) malloc (sizeof (struct vertex));
v->end = end;
v->next = graph->point[start];
graph->point[start] = v;
}
return graph;
}
int main (){
struct Edge edges[] = { {0, 1}, {0, 2}, {0, 3}, {1, 2}, {1, 4}, {2, 4}, {2, 3}, {3, 1} };
int n = sizeof (edges) / sizeof (edges[0]);
struct graph *graph = create_graph (edges, n);
int i;
for (i = 0; i < V; i++) {
struct vertex *ptr = graph->point[i];
while (ptr != NULL) {
printf ("(%d -> %d)\t", i, ptr->end);
ptr = ptr->next;
}
printf ("\n");
}
return 0;
}
Output
(1 -> 3) (1 -> 0)
(2 -> 1) (2 -> 0)
(3 -> 2) (3 -> 0)
(4 -> 2) (4 -> 1)
We choose B, mark it as
visited and put onto the
stack. Here B does not
5
have any unvisited
adjacent node. So, we
pop B from the stack.
We check the stack top for
return to the previous node
and check if it has any
6
unvisited nodes. Here, we
find D to be on the top of
the stack.
Example
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#define MAX 5
struct Vertex {
char label;
bool visited;
};
//stack variables
int stack[MAX];
int top = -1;
//graph variables
//array of vertices
struct Vertex* lstVertices[MAX];
//adjacency matrix
int adjMatrix[MAX][MAX];
//vertex count
int vertexCount = 0;
//stack functions
void push(int item) {
stack[++top] = item;
}
int pop() {
return stack[top--];
}
int peek() {
return stack[top];
}
bool isStackEmpty() {
return top == -1;
}
//graph functions
We start from
2 visiting S (starting node),
and mark it as visited.
From A we have D as
unvisited adjacent node.
7
We mark it as visited and
enqueue it.
At this stage, we are left with no unmarked (unvisited) nodes. But as
per the algorithm we keep on dequeuing in order to get all unvisited
nodes. When the queue gets emptied, the program is over.
Example
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#define MAX 5
struct Vertex {
char label;
bool visited;
};
//queue variables
int queue[MAX];
int rear = -1;
int front = 0;
int queueItemCount = 0;
//graph variables
//array of vertices
struct Vertex* lstVertices[MAX];
//adjacency matrix
int adjMatrix[MAX][MAX];
//vertex count
int vertexCount = 0;
//queue functions
void insert(int data) {
queue[++rear] = data;
queueItemCount++;
}
int removeData() {
queueItemCount--;
return queue[front++];
}
bool isQueueEmpty() {
return queueItemCount == 0;
}
//graph functions
//add vertex to the vertex list
void addVertex(char label) {
struct Vertex* vertex = (struct Vertex*) malloc(sizeof(struct Vertex));
vertex->label = label;
vertex->visited = false;
lstVertices[vertexCount++] = vertex;
}
//add edge to edge array
void addEdge(int start,int end) {
adjMatrix[start][end] = 1;
adjMatrix[end][start] = 1;
}
//display the vertex
void displayVertex(int vertexIndex) {
printf("%c ",lstVertices[vertexIndex]->label);
}
//get the adjacent unvisited vertex
int getAdjUnvisitedVertex(int vertexIndex) {
int i;
Spanning Tree
A spanning tree is a subset of Graph G, which has all the vertices
covered with minimum possible number of edges. Hence, a spanning
tree does not have cycles and it cannot be disconnected..
We now understand that one graph can have more than one spanning
tree. Following are a few properties of the spanning tree connected
to graph G −
• General Trees
• Binary Trees
• Binary Search Trees
General Trees
General trees are unordered tree data structures where the root node
has minimum 0 or maximum ‘n’ subtrees.
Binary Trees
Binary Trees are general trees in which the root node can only hold
up to maximum 2 subtrees: left subtree and right subtree. Based on
the number of children, binary trees are divided into three types.
• A full binary tree is a binary tree type where every node has
either 0 or 2 child nodes.
• A complete binary tree is a binary tree type where all the leaf
nodes must be on the same level. However, root and internal
nodes in a complete binary tree can either have 0, 1 or 2 child
nodes.
• A perfect binary tree is a binary tree type where all the leaf
nodes are on the same level and every node except leaf nodes
have 2 children.
Binary Search Trees
The data in the Binary Search Trees (BST) is always stored in such a
way that the values in the left subtree are always less than the values
in the root node and the values in the right subtree are always greater
than the values in the root node, i.e. left subtree < root node ≤ right
subtree.
Advantages of BST
• Binary Search Trees are more efficient than Binary Trees since
time complexity for performing various operations reduces.
• Since the order of keys is based on just the parent node,
searching operation becomes simpler.
• The alignment of BST also favors Range Queries, which are
executed to find values existing between two keys. This helps in
the Database Management System.
Disadvantages of BST
This skewness will make the tree a linked list rather than a BST, since
the worst case time complexity for searching operation becomes
O(n).
Consider a Binary Search Tree with ‘m’ as the height of the left
subtree and ‘n’ as the height of the right subtree. If the value of (m-
n) is equal to 0,1 or -1, the tree is said to be a Balanced Binary Search
Tree.
The trees are designed in a way that they self-balance once the height
difference exceeds 1. Binary Search Trees use rotations as self-
balancing algorithms. There are four different types of rotations: Left
Left, Right Right, Left Right, Right Left.
• AVL Trees
• Red Black Trees
• B Trees
• B+ Trees
• Splay Trees
• Priority Search Trees
Tree Traversal
Traversal is a process to visit all the nodes of a tree and may print
their values too. Because, all nodes are connected via edges (links)
we always start from the root (head) node. That is, we cannot
randomly access a node in a tree. There are three ways which we use
to traverse a tree −
• In-order Traversal
• Pre-order Traversal
• Post-order Traversal
In-order Traversal
In this traversal method, the left subtree is visited first, then the root
and later the right sub-tree. We should always remember that every
node may represent a subtree itself.
D→B→E→A→F→C→G
Algorithm
Example
Following are the implementations of this operation in various
programming languages −
#include <stdio.h>
#include <stdlib.h>
struct node {
int data;
struct node *leftChild;
struct node *rightChild;
};
struct node *root = NULL;
void insert(int data){
struct node *tempNode = (struct node*) malloc(sizeof(struct node));
struct node *current;
struct node *parent;
tempNode->data = data;
tempNode->leftChild = NULL;
tempNode->rightChild = NULL;
Pre-order Traversal
In this traversal method, the root node is visited first, then the left
subtree and finally the right subtree.
We start from A, and following pre-order traversal, we first
visit A itself and then move to its left subtree B. B is also traversed
pre-order. The process goes on until all the nodes are visited. The
output of pre-order traversal of this tree will be −
A→B→D→E→C→F→G
Algorithm
Example
Following are the implementations of this operation in various
programming languages −
#include <stdio.h>
#include <stdlib.h>
struct node {
int data;
struct node *leftChild;
struct node *rightChild;
};
struct node *root = NULL;
void insert(int data){
struct node *tempNode = (struct node*) malloc(sizeof(struct node));
struct node *current;
struct node *parent;
tempNode->data = data;
tempNode->leftChild = NULL;
tempNode->rightChild = NULL;
Post-order Traversal
In this traversal method, the root node is visited last, hence the
name. First we traverse the left subtree, then the right subtree and
finally the root node.
We start from A, and following pre-order traversal, we first visit the
left subtree B. B is also traversed post-order. The process goes on
until all the nodes are visited. The output of post-order traversal of
this tree will be −
D→E→B→F→G→C→A
Algorithm
Example
Following are the implementations of this operation in various
programming languages −
#include <stdio.h>
#include <stdlib.h>
struct node {
int data;
struct node *leftChild;
struct node *rightChild;
};
struct node *root = NULL;
void insert(int data){
struct node *tempNode = (struct node*) malloc(sizeof(struct node));
struct node *current;
struct node *parent;
tempNode->data = data;
tempNode->leftChild = NULL;
tempNode->rightChild = NULL;
Implementation
Traversal is a process to visit all the nodes of a tree and may print
their values too. Because, all nodes are connected via edges (links)
we always start from the root (head) node. That is, we cannot
randomly access a node in a tree. There are three ways which we use
to traverse a tree −
• In-order Traversal
• Pre-order Traversal
• Post-order Traversal
We shall now see the implementation of tree traversal in C
programming language here using the following binary tree −
Example
C C++JavaPython
#include <stdio.h>
#include <stdlib.h>
struct node {
int data;
struct node *leftChild;
struct node *rightChild;
};
struct node *root = NULL;
void insert(int data){
struct node *tempNode = (struct node*) malloc(sizeof(struct node));
struct node *current;
struct node *parent;
tempNode->data = data;
tempNode->leftChild = NULL;
tempNode->rightChild = NULL;
• The left sub-tree of a node has a key less than or equal to its
parent node's key.
• The right sub-tree of a node has a key greater than or equal to
its parent node's key.
Thus, BST divides all its sub-trees into two segments; the left sub-
tree and the right sub-tree and can be defined as −
Representation
We observe that the root node key (27) has all less-valued keys on
the left sub-tree and the higher valued keys on the right sub-tree.
Basic Operations
Defining a Node
Define a node that stores some data, and references to its left and
right child nodes.
struct node {
int data;
struct node *leftChild;
struct node *rightChild;
};
Search Operation
1. START
2. Check whether the tree is empty or not
3. If the tree is empty, search is not possible
4. Otherwise, first search the root of the tree.
5. If the key does not match with the value in the root, search its subtrees.
6. If the value of the key is less than the root value, search the left subtree
7. If the value of the key is greater than the root value, search the right subtree.
8. If the key is not found in the tree, return unsuccessful search.
9. END
Example
#include <stdio.h>
#include <stdlib.h>
struct node {
int data;
struct node *leftChild, *rightChild;
};
struct node *root = NULL;
struct node *newNode(int item){
struct node *temp = (struct node *)malloc(sizeof(struct node));
temp->data = item;
temp->leftChild = temp->rightChild = NULL;
return temp;
}
void insert(int data){
struct node *tempNode = (struct node*) malloc(sizeof(struct node));
struct node *current;
struct node *parent;
tempNode->data = data;
tempNode->leftChild = NULL;
tempNode->rightChild = NULL;
Insert Operation
Algorithm
1 – START
2 – If the tree is empty, insert the first element as the root node of the tree. The following
elements are added as the leaf nodes.
3 – If an element is less than the root value, it is added into the left subtree as a leaf node.
4 – If an element is greater than the root value, it is added into the right subtree as a leaf node.
5 – The final leaf nodes of the tree point to NULL values as their child nodes.
6 – END
Example
#include <stdio.h>
#include <stdlib.h>
struct node {
int data;
struct node *leftChild, *rightChild;
};
struct node *root = NULL;
struct node *newNode(int item){
struct node *temp = (struct node *)malloc(sizeof(struct node));
temp->data = item;
temp->leftChild = temp->rightChild = NULL;
return temp;
}
void insert(int data){
struct node *tempNode = (struct node*) malloc(sizeof(struct node));
struct node *current;
struct node *parent;
tempNode->data = data;
tempNode->leftChild = NULL;
tempNode->rightChild = NULL;
The inorder traversal operation in a Binary Search Tree visits all its
nodes in the following order −
Algorithm
1. START
2. Traverse the left subtree, recursively
3. Then, traverse the root node
4. Traverse the right subtree, recursively.
5. END
Example
#include <stdio.h>
#include <stdlib.h>
struct node {
int key;
struct node *left, *right;
};
struct node *newNode(int item){
struct node *temp = (struct node *)malloc(sizeof(struct node));
temp->key = item;
temp->left = temp->right = NULL;
return temp;
}
// Inorder Traversal
void inorder(struct node *root){
if (root != NULL) {
inorder(root->left);
printf("%d -> ", root->key);
inorder(root->right);
}
}
// Insertion operation
struct node *insert(struct node *node, int key){
if (node == NULL) return newNode(key);
if (key < node->key)
node->left = insert(node->left, key);
else
node->right = insert(node->right, key);
return node;
}
int main(){
struct node *root = NULL;
root = insert(root, 55);
root = insert(root, 20);
root = insert(root, 90);
root = insert(root, 50);
root = insert(root, 35);
root = insert(root, 15);
root = insert(root, 65);
printf("Inorder traversal: ");
inorder(root);
}
Output
Inorder traversal: 15 -> 20 -> 35 -> 50 -> 55 -> 65 -> 90 ->
Preorder Traversal
The preorder traversal operation in a Binary Search Tree visits all its
nodes. However, the root node in it is first printed, followed by its left
subtree and then its right subtree.
Algorithm
1. START
2. Traverse the root node first.
3. Then traverse the left subtree, recursively
4. Later, traverse the right subtree, recursively.
5. END
Example
// Preorder Traversal
void preorder(struct node *root){
if (root != NULL) {
printf("%d -> ", root->key);
preorder(root->left);
preorder(root->right);
}
}
// Insertion operation
struct node *insert(struct node *node, int key){
if (node == NULL) return newNode(key);
if (key < node->key)
node->left = insert(node->left, key);
else
node->right = insert(node->right, key);
return node;
}
int main(){
struct node *root = NULL;
root = insert(root, 55);
root = insert(root, 20);
root = insert(root, 90);
root = insert(root, 50);
root = insert(root, 35);
root = insert(root, 15);
root = insert(root, 65);
printf("Preorder traversal: ");
preorder(root);
}
Output
Preorder traversal: 55 -> 20 -> 15 -> 50 -> 35 -> 90 -> 65 ->
Postorder Traversal
Like the other traversals, postorder traversal also visits all the nodes
in a Binary Search Tree and displays them. However, the left subtree
is printed first, followed by the right subtree and lastly, the root node.
Algorithm
1. START
2. Traverse the left subtree, recursively
3. Traverse the right subtree, recursively.
4. Then, traverse the root node
5. END
Example
#include <stdio.h>
#include <stdlib.h>
struct node {
int key;
struct node *left, *right;
};
struct node *newNode(int item){
struct node *temp = (struct node *)malloc(sizeof(struct node));
temp->key = item;
temp->left = temp->right = NULL;
return temp;
}
// Postorder Traversal
void postorder(struct node *root){
if (root != NULL) {
printf("%d -> ", root->key);
postorder(root->left);
postorder(root->right);
}
}
// Insertion operation
struct node *insert(struct node *node, int key){
if (node == NULL) return newNode(key);
if (key < node->key)
node->left = insert(node->left, key);
else
node->right = insert(node->right, key);
return node;
}
int main(){
struct node *root = NULL;
root = insert(root, 55);
root = insert(root, 20);
root = insert(root, 90);
root = insert(root, 50);
root = insert(root, 35);
root = insert(root, 15);
root = insert(root, 65);
printf("Postorder traversal: ");
postorder(root);
}
Output
Postorder traversal: 55 -> 20 -> 15 -> 50 -> 35 -> 90 > 65 ->
Example
#include <stdio.h>
#include <stdlib.h>
struct node {
int data;
struct node *leftChild, *rightChild;
};
struct node *root = NULL;
struct node *newNode(int item){
struct node *temp = (struct node *)malloc(sizeof(struct node));
temp->data = item;
temp->leftChild = temp->rightChild = NULL;
return temp;
}
void insert(int data){
struct node *tempNode = (struct node*) malloc(sizeof(struct node));
struct node *current;
struct node *parent;
tempNode->data = data;
tempNode->leftChild = NULL;
tempNode->rightChild = NULL;
//if tree is empty
if(root == NULL) {
root = tempNode;
} else {
current = root;
parent = NULL;
while(1) {
parent = current;
//not found
if(current == NULL) {
return NULL;
}
}
}
return current;
}
// Inorder Traversal
void inorder(struct node *root){
if (root != NULL) {
inorder(root->leftChild);
printf("%d -> ", root->data);
inorder(root->rightChild);
}
}
// Preorder Traversal
void preorder(struct node *root){
if (root != NULL) {
printf("%d -> ", root->data);
preorder(root->leftChild);
preorder(root->rightChild);
}
}
// Postorder Traversal
void postorder(struct node *root){
if (root != NULL) {
printf("%d -> ", root->data);
postorder(root->leftChild);
postorder(root->rightChild);
}
}
int main(){
insert(55);
insert(20);
insert(90);
insert(50);
insert(35);
insert(15);
insert(65);
printf("Insertion done\n");
printf("\nPreorder Traversal: ");
preorder(root);
printf("\nInorder Traversal: ");
inorder(root);
printf("\nPostorder Traversal: ");
postorder(root);
struct node* k;
k = search(35);
if(k != NULL)
printf("\nElement %d found", k->data);
else
printf("\nElement not found");
return 0;
}
Output
Insertion done
Preorder Traversal: 55 -> 20 -> 15 -> 50 -> 35 -> 90 -> 65 ->
Inorder Traversal: 15 -> 20 -> 35 -> 50 -> 55 -> 65 -> 90 ->
Postorder Traversal: 55 -> 20 -> 15 -> 50 -> 35 -> 90 -> 65 ->
Visiting elements: 55 20 50
Element 35 found
AVL Trees
The first type of self-balancing binary search tree to be invented is
the AVL tree. The name AVL tree is coined after its inventor's names
− Adelson-Velsky and Landis.
In AVL trees, the difference between the heights of left and right
subtrees, known as the Balance Factor, must be at most one. Once the
difference exceeds one, the tree automatically executes the balancing
algorithm until the difference becomes one again.
LL Rotations
The node where the unbalance occurs becomes the left child and the
newly added node becomes the right child with the middle node as
the parent node.
RR Rotations
The node where the unbalance occurs becomes the right child and
the newly added node becomes the left child with the middle node as
the parent node.
LR Rotations
• Consider an example with “A” as the root node, “B” as the left
child of “A” and “C” as the right child of “B”.
• Since the unbalance occurs at A, a left rotation is applied on the
child nodes of A, i.e. B and C.
• After the rotation, the C node becomes the left child of A and B
becomes the left child of C.
• The unbalance still persists, therefore a right rotation is applied
at the root node A and the left child C.
• After the final right rotation, C becomes the root node, A
becomes the right child and B is the left child.
Fig : LR Rotation
RL Rotations
• Consider an example with “A” as the root node, “B” as the right
child of “A” and “C” as the left child of “B”.
• Since the unbalance occurs at A, a right rotation is applied on
the child nodes of A, i.e. B and C.
• After the rotation, the C node becomes the right child of A and
B becomes the right child of C.
• The unbalance still persists, therefore a left rotation is applied
at the root node A and the right child C.
• After the final left rotation, C becomes the root node, A becomes
the left child and B is the right child.
Fig : RL Rotation
The basic operations performed on the AVL Tree structures include all
the operations performed on a binary search tree, since the AVL Tree
at its core is actually just a binary search tree holding all its
properties. Therefore, basic operations performed on an AVL Tree are
− Insertion and Deletion.
Insertion
The data is inserted into the AVL Tree by following the Binary Search
Tree property of insertion, i.e. the left subtree must contain elements
less than the root value and right subtree must contain all the greater
elements. However, in AVL Trees, after the insertion of each element,
the balance factor of the tree is checked; if it does not exceed 1, the
tree is left as it is. But if the balance factor exceeds 1, a balancing
algorithm is applied to readjust the tree such that balance factor
becomes less than or equal to 1 again.
Algorithm
Step 3 − If the tree is empty, the new node created will become the
root node of the AVL Tree.
Step 4 − If the tree is not empty, we perform the Binary Search Tree
insertion operation and check the balancing factor of the node in the
tree.
START
if node == null then:
return new node
if key < node.key then:
node.left = insert (node.left, key)
else if (key > node.key) then:
node.right = insert (node.right, key)
else
return node
node.height = 1 + max (height (node.left), height (node.right))
balance = getBalance (node)
if balance > 1 and key < node.left.key then:
rightRotate
if balance < -1 and key > node.right.key then:
leftRotate
if balance > 1 and key > node.left.key then:
node.left = leftRotate (node.left)
rightRotate
if balance < -1 and key < node.right.key then:
node.right = rightRotate (node.right)
leftRotate (node)
return node
END
Insertion Example
Starting with the first element 1, we create a node and measure the
balance, i.e., 0.
Since both the binary search property and the balance factor are
satisfied, we insert another element into the tree.
The balance factor for the two nodes are calculated and is found to
be -1 (Height of left subtree is 0 and height of the right subtree is 1).
Since it does not exceed 1, we add another element to the tree.
Now, after adding the third element, the balance factor exceeds 1
and becomes 2. Therefore, rotations are applied. In this case, the RR
rotation is applied since the imbalance occurs at two right nodes.
The tree is rearranged as −
Similarly, the next elements are inserted and rearranged using these
rotations. After rearrangement, we achieve the tree as −
Example
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
struct Node *leftChild;
struct Node *rightChild;
int height;
};
int max(int a, int b);
int height(struct Node *N){
if (N == NULL)
return 0;
return N->height;
}
int max(int a, int b){
return (a > b) ? a : b;
}
struct Node *newNode(int data){
struct Node *node = (struct Node *) malloc(sizeof(struct Node));
node->data = data;
node->leftChild = NULL;
node->rightChild = NULL;
node->height = 1;
return (node);
}
struct Node *rightRotate(struct Node *y){
struct Node *x = y->leftChild;
struct Node *T2 = x->rightChild;
x->rightChild = y;
y->leftChild = T2;
y->height = max(height(y->leftChild), height(y->rightChild)) + 1;
x->height = max(height(x->leftChild), height(x->rightChild)) + 1;
return x;
}
struct Node *leftRotate(struct Node *x){
struct Node *y = x->rightChild;
struct Node *T2 = y->leftChild;
y->leftChild = x;
x->rightChild = T2;
x->height = max(height(x->leftChild), height(x->rightChild)) + 1;
y->height = max(height(y->leftChild), height(y->rightChild)) + 1;
return y;
}
int getBalance(struct Node *N){
if (N == NULL)
return 0;
return height(N->leftChild) - height(N->rightChild);
}
struct Node *insertNode(struct Node *node, int data){
if (node == NULL)
return (newNode(data));
if (data < node->data)
node->leftChild = insertNode(node->leftChild, data);
else if (data > node->data)
node->rightChild = insertNode(node->rightChild, data);
else
return node;
node->height = 1 + max(height(node->leftChild),
height(node->rightChild));
int balance = getBalance(node);
if (balance > 1 && data < node->leftChild->data)
return rightRotate(node);
if (balance < -1 && data > node->rightChild->data)
return leftRotate(node);
if (balance > 1 && data > node->leftChild->data) {
node->leftChild = leftRotate(node->leftChild);
return rightRotate(node);
}
if (balance < -1 && data < node->rightChild->data) {
node->rightChild = rightRotate(node->rightChild);
return leftRotate(node);
}
return node;
}
struct Node *minValueNode(struct Node *node){
struct Node *current = node;
while (current->leftChild != NULL)
current = current->leftChild;
return current;
}
void printTree(struct Node *root){
if (root == NULL)
return;
if (root != NULL) {
printTree(root->leftChild);
printf("%d ", root->data);
printTree(root->rightChild);
}
}
int main(){
struct Node *root = NULL;
root = insertNode(root, 22);
root = insertNode(root, 14);
root = insertNode(root, 72);
root = insertNode(root, 44);
root = insertNode(root, 25);
root = insertNode(root, 63);
root = insertNode(root, 98);
printf("AVL Tree: ");
printTree(root);
return 0;
}
Output
AVL Tree: 14 22 25 44 63 72 98
Deletion
Deletion Example
Using the same tree given above, let us perform deletion in three
scenarios −
However, element 6 is not a leaf node and has one child node
attached to it. In this case, we replace node 6 with its child node:
node 5.
The balance of the tree becomes 1, and since it does not exceed 1
the tree is left as it is. If we delete the element 5 further, we would
have to apply the left rotations; either LL or LR since the imbalance
occurs at both 1-2-4 and 3-2-4.
The balance factor is disturbed after deleting the element 5, therefore
we apply LL rotation (we can also apply the LR rotation here).
The balance of the tree still remains 1, therefore we leave the tree as
it is without performing any rotations.
Example
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
struct Node *leftChild;
struct Node *rightChild;
int height;
};
int max(int a, int b);
int height(struct Node *N){
if (N == NULL)
return 0;
return N->height;
}
int max(int a, int b){
return (a > b) ? a : b;
}
struct Node *newNode(int data){
struct Node *node = (struct Node *) malloc(sizeof(struct Node));
node->data = data;
node->leftChild = NULL;
node->rightChild = NULL;
node->height = 1;
return (node);
}
struct Node *rightRotate(struct Node *y){
struct Node *x = y->leftChild;
struct Node *T2 = x->rightChild;
x->rightChild = y;
y->leftChild = T2;
y->height = max(height(y->leftChild), height(y->rightChild)) + 1;
x->height = max(height(x->leftChild), height(x->rightChild)) + 1;
return x;
}
struct Node *leftRotate(struct Node *x){
struct Node *y = x->rightChild;
struct Node *T2 = y->leftChild;
y->leftChild = x;
x->rightChild = T2;
x->height = max(height(x->leftChild), height(x->rightChild)) + 1;
y->height = max(height(y->leftChild), height(y->rightChild)) + 1;
return y;
}
int getBalance(struct Node *N){
if (N == NULL)
return 0;
return height(N->leftChild) - height(N->rightChild);
}
struct Node *insertNode(struct Node *node, int data){
if (node == NULL)
return (newNode(data));
if (data < node->data)
node->leftChild = insertNode(node->leftChild, data);
else if (data > node->data)
node->rightChild = insertNode(node->rightChild, data);
else
return node;
node->height = 1 + max(height(node->leftChild),
height(node->rightChild));
int balance = getBalance(node);
if (balance > 1 && data < node->leftChild->data)
return rightRotate(node);
if (balance < -1 && data > node->rightChild->data)
return leftRotate(node);
if (balance > 1 && data > node->leftChild->data) {
node->leftChild = leftRotate(node->leftChild);
return rightRotate(node);
}
if (balance < -1 && data < node->rightChild->data) {
node->rightChild = rightRotate(node->rightChild);
return leftRotate(node);
}
return node;
}
struct Node *minValueNode(struct Node *node){
struct Node *current = node;
while (current->leftChild != NULL)
current = current->leftChild;
return current;
}
struct Node *deleteNode(struct Node *root, int data){
if (root == NULL)
return root;
if (data < root->data)
root->leftChild = deleteNode(root->leftChild, data);
else if (data > root->data)
root->rightChild = deleteNode(root->rightChild, data);
else {
if ((root->leftChild == NULL) || (root->rightChild == NULL)) {
struct Node *temp = root->leftChild ? root->leftChild : root->rightChild;
if (temp == NULL) {
temp = root;
root = NULL;
} else
*root = *temp;
free(temp);
} else {
struct Node *temp = minValueNode(root->rightChild);
root->data = temp->data;
root->rightChild = deleteNode(root->rightChild, temp->data);
}
}
if (root == NULL)
return root;
root->height = 1 + max(height(root->leftChild),
height(root->rightChild));
int balance = getBalance(root);
if (balance > 1 && getBalance(root->leftChild) >= 0)
return rightRotate(root);
if (balance > 1 && getBalance(root->leftChild) < 0) {
root->leftChild = leftRotate(root->leftChild);
return rightRotate(root);
}
if (balance < -1 && getBalance(root->rightChild) <= 0)
return leftRotate(root);
if (balance < -1 && getBalance(root->rightChild) > 0) {
root->rightChild = rightRotate(root->rightChild);
return leftRotate(root);
}
return root;
}
Example
Following are the implementations of this operation in various
programming languages −
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
struct Node *leftChild;
struct Node *rightChild;
int height;
};
int max(int a, int b);
int height(struct Node *N){
if (N == NULL)
return 0;
return N->height;
}
int max(int a, int b){
return (a > b) ? a : b;
}
struct Node *newNode(int data){
struct Node *node = (struct Node *) malloc(sizeof(struct Node));
node->data = data;
node->leftChild = NULL;
node->rightChild = NULL;
node->height = 1;
return (node);
}
struct Node *rightRotate(struct Node *y){
struct Node *x = y->leftChild;
struct Node *T2 = x->rightChild;
x->rightChild = y;
y->leftChild = T2;
y->height = max(height(y->leftChild), height(y->rightChild)) + 1;
x->height = max(height(x->leftChild), height(x->rightChild)) + 1;
return x;
}
struct Node *leftRotate(struct Node *x){
struct Node *y = x->rightChild;
struct Node *T2 = y->leftChild;
y->leftChild = x;
x->rightChild = T2;
x->height = max(height(x->leftChild), height(x->rightChild)) + 1;
y->height = max(height(y->leftChild), height(y->rightChild)) + 1;
return y;
}
int getBalance(struct Node *N){
if (N == NULL)
return 0;
return height(N->leftChild) - height(N->rightChild);
}
struct Node *insertNode(struct Node *node, int data){
if (node == NULL)
return (newNode(data));
if (data < node->data)
node->leftChild = insertNode(node->leftChild, data);
else if (data > node->data)
node->rightChild = insertNode(node->rightChild, data);
else
return node;
node->height = 1 + max(height(node->leftChild),
height(node->rightChild));
int balance = getBalance(node);
if (balance > 1 && data < node->leftChild->data)
return rightRotate(node);
if (balance < -1 && data > node->rightChild->data)
return leftRotate(node);
if (balance > 1 && data > node->leftChild->data) {
node->leftChild = leftRotate(node->leftChild);
return rightRotate(node);
}
if (balance < -1 && data < node->rightChild->data) {
node->rightChild = rightRotate(node->rightChild);
return leftRotate(node);
}
return node;
}
struct Node *minValueNode(struct Node *node){
struct Node *current = node;
while (current->leftChild != NULL)
current = current->leftChild;
return current;
}
struct Node *deleteNode(struct Node *root, int data){
if (root == NULL)
return root;
if (data < root->data)
root->leftChild = deleteNode(root->leftChild, data);
else if (data > root->data)
root->rightChild = deleteNode(root->rightChild, data);
else {
if ((root->leftChild == NULL) || (root->rightChild == NULL)) {
struct Node *temp = root->leftChild ? root->leftChild : root->rightChild;
if (temp == NULL) {
temp = root;
root = NULL;
} else
*root = *temp;
free(temp);
} else {
struct Node *temp = minValueNode(root->rightChild);
root->data = temp->data;
root->rightChild = deleteNode(root->rightChild, temp->data);
}
}
if (root == NULL)
return root;
root->height = 1 + max(height(root->leftChild),
height(root->rightChild));
int balance = getBalance(root);
if (balance > 1 && getBalance(root->leftChild) >= 0)
return rightRotate(root);
if (balance > 1 && getBalance(root->leftChild) < 0) {
root->leftChild = leftRotate(root->leftChild);
return rightRotate(root);
}
if (balance < -1 && getBalance(root->rightChild) <= 0)
return leftRotate(root);
if (balance < -1 && getBalance(root->rightChild) > 0) {
root->rightChild = rightRotate(root->rightChild);
return leftRotate(root);
}
return root;
}
Even though AVL trees are more balanced than RB trees, with the
balancing algorithm in AVL trees being stricter than that of RB trees,
multiple and faster insertion and deletion operations are made more
efficient through RB trees.
Fig: RB trees
Basic Operations of Red-Black Trees
• Insertion
• Deletion
• Search
Case 1 − Check whether the tree is empty; make the current node as
the root and color the node black if it is empty.
Case 2 − But if the tree is not empty, we create a new node and color
it red. Here we face two different cases −
Insertion Example
The tree is checked to be empty so the first node added is a root and
is colored black.
Now, the tree is not empty so we create a new node and add the next
integer with color red,
The nodes do not violate the binary search tree and RB tree
properties, hence we move ahead to add another node.
The tree is not empty; we create a new red node with the next integer
to it. But the parent of the new node is not a black colored node,
The tree right now violates both the binary search tree and RB tree
properties; since parent’s sibling is NULL, we apply a suitable rotation
and recolor the nodes.
We next insert the element 5, which makes the tree violate the RB
Tree balance property once again.
And since the sibling is NULL, we apply suitable rotation and recolor.
Now, we insert element 6, but the RB Tree property is violated and
one of the insertion cases need to be applied −
Deletion
Case 3 − If the double black’s sibling node is also a black node and its
child nodes are also black in color, follow the steps below −
• Swap the colors of the parent node and the parent’s sibling
node.
• Rotate parent node in the double black’s direction
• Reapply other cases that are suitable.
Case 5 − If the double black’s sibling is a black node but the sibling’s
child node that is closest to the double black is red, follows the steps
below −
• Swap the colors of double black’s sibling and the sibling’s child
in question
• Rotate the sibling node is the opposite direction of double black
(i.e. if the double black is a right child apply left rotations and
vice versa)
• Apply case 6.
Case 6 − If the double black’s sibling is a black node but the sibling’s
child node that is farther to the double black is red, follows the steps
below −
Deletion Example
Search
Complete implementation
Output
B Trees
B trees are extended binary search trees that are specialized in m-
way searching, since the order of B trees is ‘m’. Order of a tree is
defined as the maximum number of children a node can
accommodate. Therefore, the height of a b tree is relatively smaller
than the height of AVL tree and RB tree.
They are general form of a Binary Search Tree as it holds more than
one key and two children.
Insertion
Step 2 − The data is inserted into the tree using the binary search
insertion and once the keys reach the maximum number, the node is
split into half and the median key becomes the internal node while
the left and right keys become its children.
Step 3 − All the leaf nodes must be on the same level.
The keys, 5, 3, 21, 9, 13 are all added into the node according to the
binary search property but if we add the key 22, it will violate the
maximum key property. Hence, the node is split in half, the median
key is shifted to the parent node and the insertion is then continued.
Another hiccup occurs during the insertion of 11, so the node is split
and median is shifted to the parent.
While inserting 16, even if the node is split in two parts, the parent
node also overflows as it reached the maximum keys. Hence, the
parent node is split first and the median key becomes the root. Then,
the leaf node is split in half the median of leaf node is shifted to its
parent.
Example
// Deletion operation
void deletion(int key){
int index = searchkey(key);
if (index < n && keys[index] == key) {
if (leaf)
deletion_at_leaf(index);
else
deletion_at_nonleaf(index);
} else {
if (leaf) {
cout << "key " << key << " does not exist in the tree\n";
return;
}
bool flag = ((index == n) ? true : false);
if (C[index]->n < t)
fill(index);
if (flag && index > n)
C[index - 1]->deletion(key);
else
C[index]->deletion(key);
}
return;
}
Example
C C++JavaPython
int main() {
int i, n, t;
insert(10);
insert(20);
insert(30);
insert(40);
insert(50);
printf("B tree:\n");
traverse(r);
return 0;
}
Output
B tree:
10 20 30 40 50
B+ Trees
The B+ trees are extensions of B trees designed to make the
insertion, deletion and searching operations more efficient.
Since the size of main memory is limited, B+ trees act as the data
storage for the records that couldn’t be stored in the main memory.
For this, the internal nodes are stored in the main memory and the
leaf nodes are stored in the secondary memory storage.
Properties of B+ trees
They are almost similar to the B tree operations as the base idea to
store data in both data structures is same. However, the difference
occurs as the data is stored only in the leaf nodes of a B+ trees, unlike
B trees.
Insertion
Step 3 − The node is split into half where the left child consists of
minimum number of keys and the remaining keys are stored in the
right child.
Step 4 − But if the internal node also exceeds the maximum key
property, the node is split in half where the left child consists of the
minimum keys and remaining keys are stored in the right child.
However, the smallest number in the right child is made the parent.
Step 5 − If both the leaf node and internal node have the maximum
keys, both of them are split in the similar manner and the smallest
key in the right child is added to the parent node.
Deletion
Case 1 − If the key is present in a leaf node which has more than
minimal number of keys, without its copy present in the internal
nodes, simple delete it.
Case 2 − If the key is present in a leaf node with exactly minimal
number of keys and a copy of it is not present in the internal nodes,
borrow a key from its sibling node and delete the desired key.
Case 3 − If the key present in the leaf node has its copy in the internal
nodes, there are multiple scenarios possible −
• More than minimal keys present in both leaf node and internal
node: simply delete the key and add the inorder successor to
the internal node only.
void insert(int a) {
int i, t;
x = r;
if (x == NULL) {
r = init();
x = r;
} else {
if (x->l == 1 && x->n == 6) {
t = split_child(x, -1);
x = r;
for (i = 0; i < x->n; i++) {
if (a > x->d[i] && a < x->d[i + 1]) {
i++;
break;
} else if (a < x->d[0]) {
break;
} else {
continue;
}
}
x = x->child_ptr[i];
} else {
while (x->l == 0) {
for (i = 0; i < x->n; i++) {
if (a > x->d[i] && a < x->d[i + 1]) {
i++;
break;
} else if (a < x->d[0]) {
break;
} else {
continue;
}
}
if (x->child_ptr[i]->n == 6) {
t = split_child(x, i);
x->d[x->n] = t;
x->n++;
continue;
} else {
x = x->child_ptr[i];
}
}
}
}
x->d[x->n] = a;
sort(x->d, x->n);
x->n++;
}
int main() {
int i, n, t;
insert(10);
insert(20);
insert(30);
insert(40);
insert(50);
printf("B+ tree:\n");
traverse(r);
return 0;
}
Output
B+ tree:
10 20 30 40 50
Splay Trees
Splay trees are the altered versions of the Binary Search Trees, since
it contains all the operations of BSTs, like insertion, deletion and
searching, followed by another extended operation called splaying.
• Zig rotation
• Zag rotation
• Zig-Zig rotation
• Zag-Zag rotation
• Zig-Zag rotation
• Zag-Zig rotation
Zig rotation
The zig rotations are performed when the operational node is either
the root node or the left child node of the root node. The node is
rotated towards its right.
The zag rotations are also performed when the operational node is
either the root node or the right child nod of the root node. The node
is rotated towards its left.
The operational node becomes the root node after the shift −
Zig-Zig rotation
The zig-zig rotations are performed when the operational node has
both parent and a grandparent. The node is rotated two places
towards its right.
The first rotation will shift the tree to one position right −
The second right rotation will once again shift the node for one
position. The final tree after the shift will look like this −
Zag-Zag rotation
The zag-zag rotations are also performed when the operational node
has both parent and a grandparent. The node is rotated two places
towards its left.
Zig-Zag rotation
The zig-zag rotations are performed when the operational node has
both a parent and a grandparent. But the difference is the
grandparent, parent and child are in LRL format. The node is rotated
first towards its right followed by left.
After the first rotation, the tree is −
The zag-zig rotations are also performed when the operational node
has both parent and grandparent. But the difference is the
grandparent, parent and child are in RLR format. The node is rotated
first towards its left followed by right.
First rotation is performed, the tree is obtained as −
After second rotation, the final tree is given as below. However, the
operational node is not the root node yet so one more rotation needs
to be performed to make the said node as the root.
Basic Operations of Splay Trees
A splay contains the same basic operations that a Binary Search Tree
provides with: Insertion, Deletion, and Search. However, after every
operation there is an additional operation that differs them from
Binary Search tree operations: Splaying. We have learned about
Splaying already so let us understand the procedures of the other
operations.
Insertion
• Check whether the tree is empty; if yes, add the new node and
exit
• If the tree is not empty, add the new node to the existing tree
using the binary search insertion.
Example
Deletion
Example
#include <stdio.h>
#include <stdlib.h>
struct node {
int data;
struct node *leftChild, *rightChild;
};
struct node* newNode(int data){
struct node* Node = (struct node*)malloc(sizeof(struct node));
Node->data = data;
Node->leftChild = Node->rightChild = NULL;
return (Node);
}
struct node* rightRotate(struct node *x){
struct node *y = x->leftChild;
x->leftChild = y->rightChild;
y->rightChild = x;
return y;
}
struct node* leftRotate(struct node *x){
struct node *y = x->rightChild;
x->rightChild = y->leftChild;
y->leftChild = x;
return y;
}
struct node* splay(struct node *root, int data){
if (root == NULL || root->data == data)
return root;
if (root->data > data) {
if (root->leftChild == NULL) return root;
if (root->leftChild->data > data) {
root->leftChild->leftChild = splay(root->leftChild->leftChild, data);
root = rightRotate(root);
} else if (root->leftChild->data < data) {
root->leftChild->rightChild = splay(root->leftChild->rightChild, data);
if (root->leftChild->rightChild != NULL)
root->leftChild = leftRotate(root->leftChild);
}
return (root->leftChild == NULL)? root: rightRotate(root);
} else {
if (root->rightChild == NULL) return root;
if (root->rightChild->data > data) {
root->rightChild->leftChild = splay(root->rightChild->leftChild, data);
if (root->rightChild->leftChild != NULL)
root->rightChild = rightRotate(root->rightChild);
} else if (root->rightChild->data < data) {
root->rightChild->rightChild = splay(root->rightChild->rightChild, data);
root = leftRotate(root);
}
return (root->rightChild == NULL)? root: leftRotate(root);
}
}
struct node* insert(struct node *root, int k){
if (root == NULL) return newNode(k);
root = splay(root, k);
if (root->data == k) return root;
struct node *newnode = newNode(k);
if (root->data > k) {
newnode->rightChild = root;
newnode->leftChild = root->leftChild;
root->leftChild = NULL;
} else {
newnode->leftChild = root;
newnode->rightChild = root->rightChild;
root->rightChild = NULL;
}
return newnode;
}
struct node* deletenode(struct node* root, int data){
struct node* temp;
if (root == NULL)
return NULL;
root = splay(root, data);
if (data != root->data)
return root;
if (!root->leftChild) {
temp = root;
root = root->rightChild;
} else {
temp = root;
root = splay(root->leftChild, data);
root->rightChild = temp->rightChild;
}
free(temp);
return root;
}
void printTree(struct node *root){
if (root == NULL)
return;
if (root != NULL) {
printTree(root->leftChild);
printf("%d ", root->data);
printTree(root->rightChild);
}
}
int main(){
struct node* root = newNode(34);
root->leftChild = newNode(15);
root->rightChild = newNode(40);
printf("The Splay tree is \n");
printTree(root);
root = deletenode(root, 40);
printf("\nThe Splay tree after deletion is \n");
printTree(root);
return 0;
}
Output
The Splay tree is
15 34 40
The Splay tree after deletion is
15 34
Search
Example
#include <stdio.h>
#include <stdlib.h>
struct node {
int data;
struct node *leftChild, *rightChild;
};
struct node* newNode(int data){
struct node* Node = (struct node*)malloc(sizeof(struct node));
Node->data = data;
Node->leftChild = Node->rightChild = NULL;
return (Node);
}
struct node* rightRotate(struct node *x){
struct node *y = x->leftChild;
x->leftChild = y->rightChild;
y->rightChild = x;
return y;
}
struct node* leftRotate(struct node *x){
struct node *y = x->rightChild;
x->rightChild = y->leftChild;
y->leftChild = x;
return y;
}
struct node* splay(struct node *root, int data){
if (root == NULL || root->data == data)
return root;
if (root->data > data) {
if (root->leftChild == NULL) return root;
if (root->leftChild->data > data) {
root->leftChild->leftChild = splay(root->leftChild->leftChild, data);
root = rightRotate(root);
} else if (root->leftChild->data < data) {
root->leftChild->rightChild = splay(root->leftChild->rightChild, data);
if (root->leftChild->rightChild != NULL)
root->leftChild = leftRotate(root->leftChild);
}
return (root->leftChild == NULL)? root: rightRotate(root);
} else {
if (root->rightChild == NULL) return root;
if (root->rightChild->data > data) {
root->rightChild->leftChild = splay(root->rightChild->leftChild, data);
if (root->rightChild->leftChild != NULL)
root->rightChild = rightRotate(root->rightChild);
} else if (root->rightChild->data < data) {
root->rightChild->rightChild = splay(root->rightChild->rightChild, data);
root = leftRotate(root);
}
return (root->rightChild == NULL)? root: leftRotate(root);
}
}
struct node* insert(struct node *root, int k){
if (root == NULL) return newNode(k);
root = splay(root, k);
if (root->data == k) return root;
struct node *newnode = newNode(k);
if (root->data > k) {
newnode->rightChild = root;
newnode->leftChild = root->leftChild;
root->leftChild = NULL;
} else {
newnode->leftChild = root;
newnode->rightChild = root->rightChild;
root->rightChild = NULL;
}
return newnode;
}
struct node* deletenode(struct node* root, int data){
struct node* temp;
if (root == NULL)
return NULL;
root = splay(root, data);
if (data != root->data)
return root;
if (!root->leftChild) {
temp = root;
root = root->rightChild;
} else {
temp = root;
root = splay(root->leftChild, data);
root->rightChild = temp->rightChild;
}
free(temp);
return root;
}
void printTree(struct node *root){
if (root == NULL)
return;
if (root != NULL) {
printTree(root->leftChild);
printf("%d ", root->data);
printTree(root->rightChild);
}
}
int main(){
struct node* root = newNode(34);
root->leftChild = newNode(15);
root->rightChild = newNode(40);
root->leftChild->leftChild = newNode(12);
root->leftChild->leftChild->rightChild = newNode(14);
root->rightChild->rightChild = newNode(59);
printf("The Splay tree is \n");
printTree(root);
root = deletenode(root, 40);
printf("\nThe Splay tree after deletion is \n");
printTree(root);
return 0;
}
Output
The Splay tree is
12 14 15 34 40 59
The Splay tree after deletion is
12 14 15 34 59
• Standard Tries
• Compressed Tries
• Suffix Tries
The trie data structures also perform the same operations that tree
data structures perform. They are −
• Insertion
• Deletion
• Search
Insertion
Example
Case 1 − The key is unique − in this case, the entire key path is
deleted from the node. (Unique key suggests that there is no other
path that branches out from one path).
Case 2 − The key is not unique − the leaf nodes are updated. For
example, if the key to be deleted is see but it is a prefix of another
key seethe; we delete the see and change the Boolean values of t, h
and e as false.
Case 3 − The key to be deleted already has a prefix − the values until
the prefix are deleted and the prefix remains in the tree. For example,
if the key to be deleted is heart but there is another key present he;
so we delete a, r, and t until only he remains.
Example
if (parent != NULL) {
parent->children[index] = NULL; // Remove the child node
}
return true;
}
int main() {
struct TrieNode* root = createNode();
//Inserting the elements
insert(root, "lamborghini");
insert(root, "mercedes-benz");
insert(root, "land rover");
insert(root, "maruti suzuki");
//Before Deletion
printf("Before Deletion\n");
printf("%d\n", search(root, "lamborghini")); // Output: 1 (true)
printf("%d\n", search(root, "mercedes-benz")); // Output: 1 (true)
printf("%d\n", search(root, "land rover")); // Output: 1 (true)
printf("%d\n", search(root, "maruti suzuki")); // Output: 1 (true)
//Deleting the elements
deleteWord(root, "lamborghini");
deleteWord(root, "land rover");
//After Deletion
printf("After Deletion\n");
printf("%d\n", search(root, "lamborghini")); // Output: 0 (false)
printf("%d\n", search(root, "mercedes-benz")); // Output: 1 (true)
printf("%d\n", search(root, "land rover")); // Output: 0 (false)
printf("%d\n", search(root, "maruti suzuki")); // Output: 1 (true)
return 0;
}
Output
Before Deletion
1
1
1
1
After Deletion
0
1
0
1
Search
Example
if (curr->children[index] == NULL) {
return false;
}
curr = curr->children[index];
}
return (curr != NULL && curr->isEndOfWord);
}
bool startsWith(struct TrieNode* root, char* prefix) {
struct TrieNode* curr = root;
for (int i = 0; prefix[i] != '\0'; i++) {
int index = prefix[i] - 'a';
if (curr->children[index] == NULL) {
return false;
}
curr = curr->children[index];
}
return true;
}
int main() {
struct TrieNode* root = createNode();
//inserting the elements
insert(root, "Lamborghini");
insert(root, "Mercedes-Benz");
insert(root, "Land Rover");
insert(root, "Maruti Suzuki");
//Searching elements
printf("Searching Cars\n");
//Printing searched elements
printf("%d\n", search(root, "Lamborghini")); // Output: 1 (true)
printf("%d\n", search(root, "Mercedes-Benz")); // Output: 1 (true)
printf("%d\n", search(root, "Honda")); // Output: 0 (false)
printf("%d\n", search(root, "Land Rover")); // Output: 1 (true)
printf("%d\n", search(root, "BMW")); // Output: 0 (false)
//Searching the elements the name starts with?
printf("Cars name starts with\n");
//Printing the elements
printf("%d\n", startsWith(root, "Lambo")); // Output: 1 (true)
printf("%d\n", startsWith(root, "Hon")); // Output: 0 (false)
printf("%d\n", startsWith(root, "Hy")); // Output: 0 (false)
printf("%d\n", startsWith(root, "Mar")); // Output: 1 (true)
printf("%d\n", startsWith(root, "Land")); // Output: 1 (true)
return 0;
}
Output
Searching Cars
1
1
0
1
0
Cars name starts with
1
0
0
1
1
Heap Data Structure
Heap is a special case of balanced binary tree data structure where
the root-node key is compared with its children and arranged
accordingly. If α has child node β then −
key(α) ≥ key(β)
For Input → 35 33 42 10 14 19 27 44 26 31
Min-Heap − Where the value of the root node is less than or equal to
either of its children.
Max-Heap − Where the value of the root node is greater than or equal
to either of its children.
Both trees are constructed using the same input and order of arrival.
Example
printf("%d ",value);
}
Properties
Implementation
Analysis of Recursion
One may argue why to use recursion, as the same task can be done
with iteration. The first reason is, recursion makes a program more
readable and because of latest enhanced CPU systems, recursion is
more efficient than iterations.
Time Complexity
Space Complexity
Example
Rules
The mission is to move all the disks to some another tower without
violating the sequence of arrangement. A few rules to be followed for
Tower of Hanoi are −
• Only one disk can be moved among the towers at any given
time.
• Only the "top" disk can be removed.
• No large disk can sit over a small disk.
Algorithm
If we have 2 disks −
START
Procedure Hanoi(disk, source, dest, aux)
IF disk == 1, THEN
move disk from source to dest
ELSE
Hanoi(disk - 1, source, aux, dest) // Step 1
move disk from source to dest // Step 2
Hanoi(disk - 1, aux, dest, source) // Step 3
END IF
END Procedure
STOP
Example
#include <stdio.h>
#include <stdbool.h>
#define MAX 10
int list[MAX] = {1,8,4,6,0,3,5,2,7,9};
void display(){
int i;
printf("[");
// navigate through all items
for(i = 0; i < MAX; i++) {
printf("%d ",list[i]);
}
printf("]\n");
}
void bubbleSort() {
int temp;
int i,j;
bool swapped = false;
// loop through all numbers
for(i = 0; i < MAX-1; i++) {
swapped = false;
// loop through numbers falling ahead
for(j = 0; j < MAX-1-i; j++) {
printf("Items compared: [ %d, %d ] ", list[j],list[j+1]);
// check if next number is lesser than current no
// swap the numbers.
// (Bubble up the highest number)
if(list[j] > list[j+1]) {
temp = list[j];
list[j] = list[j+1];
list[j+1] = temp;
swapped = true;
printf(" => swapped [%d, %d]\n",list[j],list[j+1]);
} else {
printf(" => not swapped\n");
}
}
// if no number was swapped that means
// array is sorted now, break the loop.
if(!swapped) {
break;
}
printf("Iteration %d#: ",(i+1));
display();
}
}
int main() {
printf("Input Array: ");
display();
printf("\n");
bubbleSort();
printf("\nOutput Array: ");
display();
}
Output
Input Array: [1 8 4 6 0 3 5 2 7 9 ]
Output Array: [0 1 2 3 4 5 6 7 8 9 ]
Fn = Fn-1 + Fn-2
or, this −
F8 = 1 1 2 3 5 8 13 21
Procedure Fibonacci(n)
declare f0, f1, fib, loop
set f0 to 0
set f1 to 1
for loop ← 1 to n
fib ← f0 + f1
f0 ← f1
f1 ← fib
<b>display fib</b>
end for
end procedure
START
Procedure Fibonacci(n)
declare f0, f1, fib, loop
set f0 to 0
set f1 to 1
display f0, f1
for loop ← 1 to n
fib ← f0 + f1
f0 ← f1
f1 ← fib
display fib
end for
END
Example
C C++JavaPython
#include <stdio.h>
int factorial(int n) {
//base case
if(n == 0) {
return 1;
} else {
return n * factorial(n-1);
}
}
int fibbonacci(int n) {
if(n == 0){
return 0;
} else if(n == 1) {
return 1;
} else {
return (fibbonacci(n-1) + fibbonacci(n-2));
}
}
int main() {
int n = 5;
int i;