Advanced Datastructures Lab Manual
Advanced Datastructures Lab Manual
RECORD NOTEBOOK
SEMESTER II
2024-2025
ACADEMIC YEAR : 2023-2024
NAME : ……………………………………
REG.NO : ……………………………….……
LORD JEGANNATH COLLEGE OF ENGINEERING AND TECHNOLOGY
PSN Nagar, Ramanathichanputhur, Kumarapuram Thoppur Post-629 402
Kanyakumari District, Tamil Nadu
(Approved by AICTE and Affiliated to Anna University, Chennai)
(Accredited with ‘A’ Grade by NAAC)
2024 – 2025
This is to certify that this is a bonafide record of the Practical work done by
AIM
PROCEDURE
Step 1: Open Ubuntu OS.
Step 2: Open terminal
Step 3: Check gcc version using the command gcc - -version
Step 4: Install/Update gcc use the command sudo apt install build-essential
Step 5: Use the Command for writing the Program touch filename.cpp
(Open the file using notepad & Save the Program (File Name can be
anything but it should end with .cpp extension)
Step 6: To Compile the Program use the command g++ filename.cpp
Step 7: To Run the Program ./a.out
Step 8: Output will be displayed in the Terminal.
1
PROGRAM
#include <bits/stdc++.h>
using namespace std;
2
int main()
{
//building the tree
TreeNode* t = new TreeNode(7);
t->left = new TreeNode(1);
t->left->left = new TreeNode(0);
t->left->right = new TreeNode(3);
t->left->right->left = new TreeNode(2);
t->left->right->right = new TreeNode(5); t-
>left->right->right->left = new TreeNode(4); t-
>left->right->right->right = new TreeNode(6);
t->right = new TreeNode(9); t->right->left =
new TreeNode(8);
t->right->right = new TreeNode(10);
return 0;
}
OUTPUT
3
//In-Order Tree Traversal Using Recursion
#include<iostream>
using namespace std;
struct node {
int data;
struct node *left;
struct node *right;
};
struct node *createNode(int val) {
struct node *temp = (struct node *)malloc(sizeof(struct node));
temp->data = val;
temp->left = temp->right = NULL;
return temp;
}
void inorder(struct node *root) {
if (root != NULL) {
inorder(root->left);
cout<<root->data<<" ";
inorder(root->right);
}
}
struct node* insertNode(struct node* node, int val)
{ if (node == NULL) return createNode(val);
if (val < node->data)
node->left = insertNode(node->left, val);
else if (val > node->data)
node->right = insertNode(node->right, val);
return node;
}
int main() {
struct node *root = NULL;
root = insertNode(root, 4);
insertNode(root, 5);
insertNode(root, 2);
insertNode(root, 9);
insertNode(root, 1);
4
insertNode(root, 3);
cout<<"In-Order traversal of the Binary Search Tree is: ";
inorder(root);
return 0;
}
OUTPUT
#include <iostream>
#include <string>
5
// Specializing template of value structure for string
// String initialization is different from interger etc.
template <>
6
Node<T>* node5 = new Node<T>; node5->m_Value.m_Value = 55;
Node<T>* node6 = new Node<T>; node6->m_Value.m_Value = 66;
Node<T>* node7 = new Node<T>; node7->m_Value.m_Value = 77;
Node<T>* node8 = new Node<T>; node8->m_Value.m_Value = 88;
Node<T>* node9 = new Node<T>; node9->m_Value.m_Value = 99;
node1->m_Left = node2;
node1->m_Right = node3;
node2->m_Left = node4;
node2->m_Right = node5;
node3->m_Left = node6;
node3->m_Right = node7;
node4->m_Left = node8;
node8->m_Left = node9;
return node1;
}
postorder<T>(root->m_Left);
postorder<T>(root->m_Right);
cout << root->m_Value.m_Value << " ";
}
int main()
{
Node<int>* root = createTree<int>();
cout << "postorder: ";
7
postorder<int>(root);
cout << endl << endl;
return 0;
}
OUTPUT
#include <iostream>
using namespace std;
int main() {
int n1=0,n2=1,n3,i,number;
cout<<"Enter the number of elements: ";
cin>>number;
cout<<n1<<" "<<n2<<" "; //printing 0 and 1
for(i=2;i<number;++i) //loop starts from 2 because 0 and 1 are already printed
{
n3=n1+n2;
cout<<n3<<" ";
n1=n2;
8
n2=n3;
}
return 0;
}
OUTPUT
RESULT
9
Program No:
Date:
AIM
PROCEDURE
Step 1: Open Ubuntu OS.
Step 2: Open terminal
Step 3: Check gcc version using the command gcc - -version
Step 4: Install/Update gcc use the command sudo apt install build-essential
Step 5: Use the Command for writing the Program touch filename.cpp
(Open the file using notepad & Save the Program (File Name can be
anything but it should end with .cpp extension)
Step 6: To Compile the Program use the command g++ filename.cpp
Step 7: To Run the Program ./a.out
Step 8: Output will be displayed in the Terminal.
10
//IMPLEMENTATION OF ITERATIVE FUNCTION FOR TREE TRAVERSAL
//In-Order Tree traversal using iterative functions
#include<bits/stdc++.h>
using namespace std;
/* A binary tree Node has data, pointer to left child and a pointer to right child */
struct Node
{
int data;
struct Node* left;
struct Node* right;
Node (int data)
{
this->data = data;
left = right = NULL;
}
};
stack<Node *> s;
Node *curr = root;
11
while (curr != NULL)
{
/* place pointer to a tree node on the stack before traversingthe node's left
subtree */
s.push(curr);
curr = curr->left;
}
/* we have visited the node and itsleft subtree. Now, it's right subtree's turn */
curr = curr->right;
} /* end of while */
}
/* Driver program to test above functions*/
int main()
{
struct Node *root = new Node(1);
root->left = new Node(2);
root->right = new Node(3);
root->left->left = new Node(4);
root->left->right = new Node(5);
12
cout<< "The inoder tree Traversal is:";
inOrder(root);
return 0;
}
OUTPUT
#include <iostream>
#include <stack>
using namespace std;
{
this->data = data;
this->left = this->right = nullptr;
13
}
};
// push the right child of the popped node into the stack
if (curr->right) {
stack.push(curr->right);
}
14
// push the left child of the popped node into the stack
if (curr->left) {
stack.push(curr->left);
}
// the right child must be pushed first so that the left child
// is processed first (LIFO order)
}
}
int main()
{
Node* root = new Node(1);
root->left = new Node(2);
root->right = new Node(3);
root->left->left = new Node(4);
root->right->left = new Node(5);
root->right->right = new Node(6);
root->right->left->left = new Node(7);
root->right->left->right = new Node(8);
cout<<"The pre-order tree traversal is:\n";
preorderIterative(root);
return 0;
}
15
OUTPUT
#include <bits/stdc++.h>
using namespace std;
// A tree node
struct Node {
int data;
struct Node *left, *right;
};
16
// An iterative function to do postorder traversal of a given binary tree
vector<int> postOrderIterative(struct Node* root)
{
vector<int> postOrderList;
return postOrderList;
stack<Node*> S;
S.push(root);
|| prev->right == current) {
if (current->left)
S.push(current->left);
else if (current->right)
S.push(current->right);
else {
S.pop();
postOrderList.push_back(current->data);
}
17
/* go up the tree from left node, if the child is right push it onto stack
otherwise process parent and pop stack */
}
else if (current->left == prev) {
if (current->right)
S.push(current->right);
else {
S.pop();
postOrderList.push_back(current->data);
}
/* go up the tree from right node and after coming back from right node process
parent and pop stack */
}
else if (current->right == prev) {
S.pop();
postOrderList.push_back(current->data);
}
prev = current;
}
return postOrderList;
}
18
root->left = newNode(2);
root->right = newNode(3);
root->left->left = newNode(4);
root->left->right = newNode(5);
root->right->left = newNode(6);
root->right->right = newNode(7);
vector<int> postOrderList =
postOrderIterative(root); for (auto it : postOrderList)
cout << it << " ";
printf("]");
return 0;
}
OUTPUT
19
//IMPLEMENTATION OF ITERATIVE FUNCTION FOR FIBONACCI
#include <iostream>
using namespace std;
void fib(int num) {
int x = 0, y = 1, z = 0;
for (int i = 0; i < num; i++) {
cout << x << " ";
z = x + y;
x = y;
y = z;
}
}
int main() {
int num;
cout << "Enter the number : ";
cin >> num;
cout << "\nThe fibonacci series : " ;
fib(num);
return 0;
}
20
OUTPUT
RESULT
Thus the C++ program was implemented done successfully using Linux
Platform
21
Program No:
Date:
AIM
To implement merge sort and quick sort using C++ Program
PROCEDURE
Step 1: Open Ubuntu OS.
Step 2: Open terminal
Step 3: Check gcc version using the command gcc - -version
Step 4: Install/Update gcc use the command sudo apt install build-essential
Step 5: Use the Command for writing the Program touch filename.cpp
(Open the file using notepad & Save the Program (File Name can be
anything but it should end with .cpp extension)
Step 6: To Compile the Program use the command g++ filename.cpp
Step 7: To Run the Program ./a.out
Step 8: Output will be displayed in the Terminal.
PROGRAM
//Merge Sort
#include <bits/stdc++.h>
using namespace std;
22
public:
int data;
Node* next;
};
/* function prototypes */
Node* SortedMerge(Node* a, Node* b);
void FrontBackSplit(Node* source,
Node** frontRef, Node** backRef);
23
/* answer = merge the two sorted lists together
*/ *headRef = SortedMerge(a, b);
}
/* Base cases */
if (a == NULL)
return (b);
else if (b == NULL)
return (a);
24
void FrontBackSplit(Node* source, Node** frontRef, Node** backRef)
{
Node* fast;
Node* slow;
slow = source;
fast = source->next;
25
/* Function to insert a node at the beginning of the linked list
*/ void push(Node** head_ref, int new_data) {
/* allocate node */
Node* new_node = new Node();
26
push(&a, 3);
push(&a, 2);
return 0;
}
OUTPUT
27
//Quick Sort
#include <iostream>
using namespace std;
28
}
if (i < pivotIndex && j > pivotIndex) {
swap(arr[i++], arr[j--]);
}
}
return pivotIndex;
}
void quickSort(int arr[], int start, int end)
{
// base case
if (start >= end)
return;
29
quickSort(arr, 0, n - 1);
for (int i = 0; i < n; i++) {
cout << arr[i] << " ";
}
return 0;
}
OUTPUT
RESULT
Thus the C++ program was implemented done successfully using Linux
Platform.
30
Program No:
Date:
AIM
To implement Binary Search Tree using C++ Program
PROCEDURE
Step 1: Open Ubuntu OS.
Step 2: Open terminal
Step 3: Check gcc version using the command gcc - -version
Step 4: Install/Update gcc use the command sudo apt install build-essential
Step 5: Use the Command for writing the Program touch filename.cpp
(Open the file using notepad & Save the Program (File Name can be
anything but it should end with .cpp extension)
Step 6: To Compile the Program use the command g++ filename.cpp
Step 7: To Run the Program ./a.out
Step 8: Output will be displayed in the Terminal.
PROGRAM
31
for (int i = 0; i < arraysize; ++i)
cout << a[i] << ",";
}
int main()
{
int a[] = { 1, 5, 8, 9, 6, 7, 3, 4, 2, 0 };
int asize = sizeof(a) / sizeof(a[0]);
cout << "\nThe array is : \n";
show(a, asize);
32
return 0;
}
OUTPUT
RESULT
33
Program No:
Date:
AIM
To implement Red-Black Tree using C++ Program
PROCEDURE
Step 1: Open Ubuntu OS.
Step 2: Open terminal
Step 3: Check gcc version using the command gcc - -version
Step 4: Install/Update gcc use the command sudo apt install build-essential
Step 5: Use the Command for writing the Program touch filename.cpp
(Open the file using notepad & Save the Program (File Name can be
anything but it should end with .cpp extension)
Step 6: To Compile the Program use the command g++ filename.cpp
Step 7: To Run the Program ./a.out
Step 8: Output will be displayed in the Terminal.
PROGRAM
PROGRAM
#include<iostream>
34
struct node
{
int key;
node *parent;
char color;
node *left;
node *right;
};
class RBtree
{
node *root;
node *q;
public :
RBtree()
{
q=NULL;
root=NULL;
}
void insert();
void insertfix(node *);
void leftrotate(node *);
void rightrotate(node *);
void del();
node* successor(node *);
void delfix(node *);
void disp();
void display( node *);
void search();
};
void RBtree::insert()
{
int z,i=0;
cout<<"\nEnter key of the node to be inserted: ";
cin>>z;
node *p,*q;
node *t=new node;
t->key=z;
35
t->left=NULL;
t->right=NULL;
t->color='r';
p=root;
q=NULL;
if(root==NULL)
{
root=t;
t->parent=NULL;
}
else
{
while(p!=NULL)
{
q=p;
if(p->key<t->key)
p=p->right;
else
p=p->left;
}
t->parent=q;
if(q->key<t->key)
q->right=t;
else
q->left=t;
}
insertfix(t);
}
void RBtree::insertfix(node *t)
{
node *u;
if(root==t)
{
t->color='b';
return;
}
while(t->parent!=NULL&&t->parent->color=='r')
36
{
node *g=t->parent->parent;
if(g->left==t->parent)
{
if(g->right!=NULL)
{
u=g->right;
if(u->color=='r')
{
t->parent->color='b';
u->color='b';
g->color='r';
t=g;
}
}
else
{
if(t->parent->right==t)
{
t=t->parent;
leftrotate(t);
}
t->parent->color='b';
g->color='r';
rightrotate(g);
}
}
else
{
if(g->left!=NULL)
{
u=g->left;
if(u->color=='r')
{
t->parent->color='b';
u->color='b';
g->color='r';
37
t=g;
}
}
else
{
if(t->parent->left==t)
{
t=t->parent;
rightrotate(t);
}
t->parent->color='b';
g->color='r';
leftrotate(g);
}
}
root->color='b';
}
}
void RBtree::del()
{
if(root==NULL)
{
cout<<"\nEmpty Tree." ;
return ;
}
int x;
cout<<"\nEnter the key of the node to be deleted: ";
cin>>x;
node *p;
p=root;
node *y=NULL;
node *q=NULL;
int found=0;
while(p!=NULL&&found==0)
{
if(p->key==x)
38
found=1;
if(found==0)
{
if(p->key<x)
p=p->right;
else
p=p->left;
}
}
if(found==0)
{
cout<<"\nElement Not Found.";
return ;
}
else
{
cout<<"\nDeleted Element: "<<p->key;
cout<<"\nColour: ";
if(p->color=='b')
cout<<"Black\n";
else
cout<<"Red\n";
if(p->parent!=NULL)
cout<<"\nParent: "<<p->parent->key;
else
cout<<"\nThere is no parent of the node. ";
if(p->right!=NULL)
cout<<"\nRight Child: "<<p->right->key;
else
cout<<"\nThere is no right child of the node. ";
if(p->left!=NULL)
cout<<"\nLeft Child: "<<p->left->key;
else
cout<<"\nThere is no left child of the node. ";
cout<<"\nNode Deleted."; if(p->left==NULL||p-
>right==NULL)
39
y=p;
else
y=successor(p);
if(y->left!=NULL)
q=y->left;
else
{
if(y->right!=NULL)
q=y->right;
else
q=NULL;
}
if(q!=NULL)
q->parent=y->parent;
if(y->parent==NULL)
root=q;
else
{
if(y==y->parent->left)
y->parent->left=q;
else
y->parent->right=q;
}
if(y!=p)
{
p->color=y->color;
p->key=y->key;
}
if(y->color=='b')
delfix(q);
}
}
40
{
if(p->parent->left==p)
{
s=p->parent->right;
if(s->color=='r')
{
s->color='b';
p->parent->color='r';
leftrotate(p->parent);
s=p->parent->right;
}
if(s->right->color=='b'&&s->left->color=='b')
{
s->color='r';
p=p->parent;
}
else
{
if(s->right->color=='b')
{
s->left->color=='b';
s->color='r';
rightrotate(s);
s=p->parent->right;
}
s->color=p->parent->color;
p->parent->color='b';
s->right->color='b';
leftrotate(p->parent);
p=root;
}
}
else
{
s=p->parent->left;
if(s->color=='r')
{
41
s->color='b';
p->parent->color='r';
rightrotate(p->parent);
s=p->parent->left;
}
if(s->left->color=='b'&&s->right->color=='b')
{
s->color='r';
p=p->parent;
}
else
{
if(s->left->color=='b')
{
s->right->color='b';
s->color='r';
leftrotate(s);
s=p->parent->left;
}
s->color=p->parent->color;
p->parent->color='b';
s->left->color='b';
rightrotate(p->parent);
p=root;
}
}
p->color='b';
root->color='b';
}
}
42
node *y=p->right;
if(y->left!=NULL)
{
p->right=y->left;
y->left->parent=p;
}
else
p->right=NULL;
if(p->parent!=NULL)
y->parent=p->parent;
if(p->parent==NULL)
root=y;
else
{
if(p==p->parent->left)
p->parent->left=y;
else
p->parent->right=y;
}
y->left=p;
p->parent=y;
}
}
void RBtree::rightrotate(node *p)
{
if(p->left==NULL)
return ;
else
{
node *y=p->left;
if(y->right!=NULL)
{
p->left=y->right;
y->right->parent=p;
}
else
p->left=NULL;
43
if(p->parent!=NULL)
y->parent=p->parent;
if(p->parent==NULL)
root=y;
else
{
if(p==p->parent->left)
p->parent->left=y;
else
p->parent->right=y;
}
y->right=p;
p->parent=y;
}
}
void RBtree::disp()
{
display(root);
44
}
void RBtree::display(node *p)
{
if(root==NULL)
{
cout<<"\nEmpty Tree.";
return ;
}
if(p!=NULL)
{
cout<<"\n\t NODE: ";
cout<<"\n Key: "<<p->key;
cout<<"\n Colour: ";
if(p->color=='b')
cout<<"Black";
else
cout<<"Red";
if(p->parent!=NULL)
cout<<"\n Parent: "<<p->parent->key;
else
cout<<"\n There is no parent of the node. ";
if(p->right!=NULL)
cout<<"\n Right Child: "<<p->right->key;
else
cout<<"\n There is no right child of the node. ";
if(p->left!=NULL)
cout<<"\n Left Child: "<<p->left->key;
else
cout<<"\n There is no left child of the node. ";
cout<<endl;
if(p->left)
{
cout<<"\n\nLeft:\n";
display(p->left);
}
/*else
cout<<"\nNo Left Child.\n";*/
45
if(p->right)
{
cout<<"\n\nRight:\n";
display(p->right);
}
/*else
cout<<"\nNo Right Child.\n"*/
}
}
void RBtree::search()
{
if(root==NULL)
{
cout<<"\nEmpty Tree\n" ;
return ;
}
int x;
cout<<"\n Enter key of the node to be searched:
"; cin>>x;
node *p=root;
int found=0;
while(p!=NULL&& found==0)
{
if(p->key==x)
found=1;
if(found==0)
{
if(p->key<x)
p=p->right;
else
p=p->left;
}
}
if(found==0)
cout<<"\nElement Not Found.";
else
{
46
cout<<"\n\t FOUND NODE: ";
cout<<"\n Key: "<<p->key;
cout<<"\n Colour: ";
if(p->color=='b')
cout<<"Black";
else
cout<<"Red";
if(p->parent!=NULL)
cout<<"\n Parent: "<<p->parent->key;
else
cout<<"\n There is no parent of the node. ";
if(p->right!=NULL)
cout<<"\n Right Child: "<<p->right->key;
else
cout<<"\n There is no right child of the node. ";
if(p->left!=NULL)
cout<<"\n Left Child: "<<p->left->key;
else
cout<<"\n There is no left child of the node. ";
cout<<endl;
}
}
int main()
{
int ch,y=0;
RBtree obj;
do
{
cout<<"\n\t RED BLACK TREE " ; cout<<"\n 1.
Insert in the tree "; cout<<"\n 2. Delete a node
from the tree"; cout<<"\n 3. Search for an
element in the tree"; cout<<"\n 4. Display the
tree "; cout<<"\n 5. Exit " ;
47
switch(ch)
{
case 1 : obj.insert();
cout<<"\nNode Inserted.\n";
break;
case 2 : obj.del();
break;
case 3 : obj.search();
break;
case 4 : obj.disp();
break;
case 5 : y=1;
break;
default : cout<<"\nEnter a Valid Choice.";
}
cout<<endl;
}while(y!=1);
return 1;
}
OUTPUT
48
49
RESULT
Thus the C++ program was implemented done successfully using Linux
Platform.
50
Program No:
Date:
HEAP IMPLEMENTATION
AIM
To implement Heap implementation using C++ Program
PROCEDURE
Step 1: Open Ubuntu OS.
Step 2: Open terminal
Step 3: Check gcc version using the command gcc - -version
Step 4: Install/Update gcc use the command sudo apt install build-essential
Step 5: Use the Command for writing the Program touch filename.cpp
(Open the file using notepad & Save the Program (File Name can be
anything but it should end with .cpp extension)
Step 6: To Compile the Program use the command g++ filename.cpp
Step 7: To Run the Program ./a.out
Step 8: Output will be displayed in the Terminal.
PROGRAM
52
}
bool isEmpty()
{
return currentSize == 0;
}
bool isFull()
{
return currentSize == size;
}
void makeEmpty( )
{
currentSize = 0;
}
int parent(int i)
{
return (i - 1) / d;
}
int kthChild(int i, int k)
{
return d * i + k;
}
void insert(int x)
{
if (isFull())
{
cout<<"Array Out of Bounds"<<endl;
53
return;
}
int hole = currentSize;
currentSize++;
array[hole] = x;
percolateUp(hole);
}
int findMin()
{
if (isEmpty())
{
cout<<"Array Underflow"<<endl;
return 0;
}
return array[0];
}
int Delete(int hole)
{
if (isEmpty())
{
cout<<"Array Underflow"<<endl;
return 0;
}
int keyItem = array[hole];
array[hole] = array[currentSize - 1];
currentSize--;
54
percolateDown( hole );
return keyItem;
}
void buildHeap()
{
for (int i = currentSize - 1 ; i >= 0; i--)
percolateDown(i);
}
55
int k = 2;
int candidateChild = kthChild(hole, k);
while ((k <= d) && (candidateChild < currentSize))
{
if (array[candidateChild] < array[bestChildYet])
bestChildYet = candidateChild;
k++;
candidateChild = kthChild(hole, k);
}
return bestChildYet;
}
void percolateUp(int hole)
{
int tmp = array[hole];
for (; hole > 0 && tmp < array[parent(hole)]; hole = parent(hole))
array[hole] = array[ parent(hole) ];
array[hole] = tmp;
}
void printHeap()
{
cout<<"\nHeap = ";
for (int i = 0; i < currentSize; i++)
cout<<array[i]<<" ";
cout<<endl;
}
};
56
/*
* Main
*/
int main()
{
DaryHeap th(size,
num); char ch;
do
{
57
cin>>val;
th.insert(val);
break;
case 2:
cout<<"Enter delete position: ";
cin>>val;
th.Delete(val - 1);
break;
case 3:
if (th.isFull())
cout<<"The Heap is Full"<<endl;
else
cout<<"The Heap is not Full"<<endl;
break;
case 4 :
if (th.isEmpty())
cout<<"The Heap is Empty"<<endl;
else
cout<<"The Heap is not Empty"<<endl;
break;
case 5 :
th.makeEmpty();
cout<<"Heap Cleared\n";
break;
default :
cout<<"Wrong Entry \n ";
58
break;
}
th.printHeap();
59
60
RESULT
Thus the C++ program was implemented done successfully using Linux
Platform.
61
Program No:
Date:
AIM
To implement Fibonacci Heap implementation using C++ Program
PROCEDURE
Step 1: Open Ubuntu OS.
Step 2: Open terminal
Step 3: Check gcc version using the command gcc - -version
Step 4: Install/Update gcc use the command sudo apt install build-essential
Step 5: Use the Command for writing the Program touch filename.cpp
(Open the file using notepad & Save the Program (File Name can be
anything but it should end with .cpp extension)
Step 6: To Compile the Program use the command g++ filename.cpp
Step 7: To Run the Program ./a.out
Step 8: Output will be displayed in the Terminal.
PROGRAM
// FIBONACCI HEAP IMPLEMENTATION
#include <iostream>
#include <malloc.h>
using namespace std;
62
struct node {
node* parent;
node* child;
node* left;
node* right;
int key;
};
63
mini->left = new_node;
if (new_node->key < mini->key)
mini = new_node;
}
else {
mini = new_node;
}
}
else {
cout << "The root nodes of Heap are: " << endl;
do {
cout << ptr->key;
ptr = ptr->right;
if (ptr != mini) {
cout << "-->";
}
64
}
}
// Driver code
int main()
{
no_of_nodes = 7;
insertion(4);
insertion(3);
insertion(7);
insertion(5);
insertion(2);
insertion(1);
insertion(10);
display(mini);
find_min(mini);
return 0;
}
65
OUTPUT
RESULT
Thus the C++ program was implemented done successfully using Linux
Platform.
66
Program No:
Date:
GRAPH TRAVERSAL
AIM
To implement Graph Traversal implemantation using C++ Program
PROCEDURE
Step 1: Open Ubuntu OS.
Step 2: Open terminal
Step 3: Check gcc version using the command gcc - -version
Step 4: Install/Update gcc use the command sudo apt install build-essential
Step 5: Use the Command for writing the Program touch filename.cpp
(Open the file using notepad & Save the Program (File Name can be
anything but it should end with .cpp extension)
Step 6: To Compile the Program use the command g++ filename.cpp
Step 7: To Run the Program ./a.out
Step 8: Output will be displayed in the Terminal.
PROGRAM
// GRAPH TRAVERSAL
// Breadth first Traversal
#include <bits/stdc++.h>
using namespace std;
67
// adjacency list representation
class Graph {
int V; // No. of vertices
public:
Graph(int V); // Constructor
Graph::Graph(int V)
{
this->V = V;
adj.resize(V);
}
68
adj[v].push_back(w); // Add w to v’s list.
}
void Graph::BFS(int s)
{
while (!queue.empty()) {
69
if (!visited[adjecent]) {
visited[adjecent] = true;
queue.push_back(adjecent);
}
}
}
}
g.addEdge(0, 1);
g.addEdge(0, 2);
g.addEdge(1, 2);
g.addEdge(2, 0);
g.addEdge(2, 3);
g.addEdge(3, 3);
cout << "Following is Breadth First Traversal "
<< "(starting from vertex 2) \n";
g.BFS(2);
return 0;
}
70
OUTPUT
#include <iostream>
#include <list>
using namespace std;
class DFSGraph
{
int V; // No. of vertices
list<int> *adjList; // adjacency list
// class Constructor
DFSGraph(int V)
{
this->V = V;
adjList = new list<int>[V];
}
71
adjList[v].push_back(w); // Add w to v’s list.
}
// DFS traversal
void DFSGraph::DFS()
{
72
// explore the vertices one by one by recursively calling DFS_util
for (int i = 0; i < V; i++)
if (visited[i] == false)
DFS_util(i, visited);
int main()
{
// Create a graph
DFSGraph gdfs(5);
gdfs.addEdge(0, 1);
gdfs.addEdge(0, 2);
gdfs.addEdge(0, 3);
gdfs.addEdge(1, 2);
gdfs.addEdge(2, 4);
gdfs.addEdge(3, 3);
gdfs.addEdge(4, 4);
return 0;
}
73
OUTPUT
RESULT
Thus the C++ program was implemented done successfully using Linux
Platform.
74
Program No:
Date:
AIM
To implement Spanning tree using C++ Program
PROCEDURE
Step 1: Open Ubuntu OS.
Step 2: Open terminal
Step 3: Check gcc version using the command gcc - -version
Step 4: Install/Update gcc use the command sudo apt install build-essential
Step 5: Use the Command for writing the Program touch filename.cpp
(Open the file using notepad & Save the Program (File Name can be
anything but it should end with .cpp extension)
Step 6: To Compile the Program use the command g++ filename.cpp
Step 7: To Run the Program ./a.out
Step 8: Output will be displayed in the Terminal.
PROGRAM
75
#define V 7
int main () {
int G[V][V] = {
{0,28,0,0,0,10,0},
{28,0,16,0,0,0,14},
{0,16,0,12,0,0,0},
{0,0,12,22,0,18},
{0,0,0,22,0,25,24},
{10,0,0,0,25,0,0},
{0,14,0,18,24,0,0}
};
int edge;
int visit[V];
for(int i=0;i<V;i++){
visit[i]=false;
}
edge = 0;
visit[0] = true;
int x;
int y;
cout << "Edge" << " : " << "Weight";
cout << endl;
while (edge < V - 1) {
int min = INT_MAX;
x = 0;
76
y = 0;
for (int i = 0; i < V; i++) {
if (visit[i]) {
for (int j = 0; j < V; j++) {
if (!visit[j] && G[i][j]) {
if (min > G[i][j]) {
min = G[i][j];
x = i;
y = j;
}
}
}
}
}
cout << x << " ---> " << y << " : " << G[x][y];
cout << endl;
visit[y] = true;
edge++;
}
return 0;
}
77
OUTPUT
RESULT
Thus the C++ program was implemented done successfully using Linux
Platform.
78
Program No:
Date:
AIM
To implement Shortest Path Algorithm using C++ Program
PROCEDURE
Step 1: Open Ubuntu OS.
Step 2: Open terminal
Step 3: Check gcc version using the command gcc - -version
Step 4: Install/Update gcc use the command sudo apt install build-essential
Step 5: Use the Command for writing the Program touch filename.cpp
(Open the file using notepad & Save the Program (File Name can be
anything but it should end with .cpp extension)
Step 6: To Compile the Program use the command g++ filename.cpp
Step 7: To Run the Program ./a.out
Step 8: Output will be displayed in the Terminal.
PROGRAM
80
Dset[u]=true;
for(int v=0;v<vertex;v++)
81
OUTPUT
struct Edge {
int src, dest, weight;
};
struct Graph {
int V, E;
struct Edge* edge;
};
82
{
struct Graph* graph = new Graph;
graph->V = V;
graph->E = E;
graph->edge = new Edge[E];
return graph;
}
void printArr(int dist[], int n)
{
printf("Vertex Distance from Source\n");
for (int i = 0; i < n; ++i)
printf("%d \t\t %d\n", i, dist[i]);
}
void BellmanFord(struct Graph* graph, int src)
{
int V = graph->V;
int E = graph->E;
int dist[V];
83
int v = graph->edge[j].dest;
int weight = graph->edge[j].weight;
if (dist[u] != INT_MAX
84
graph->edge[0].dest = 1;
graph->edge[0].weight = -1;
graph->edge[1].src = 0;
graph->edge[1].dest = 2;
graph->edge[1].weight = 4;
graph->edge[2].src = 1;
graph->edge[2].dest = 2;
graph->edge[2].weight = 3;
graph->edge[3].src = 1;
graph->edge[3].dest = 3;
graph->edge[3].weight = 2;
graph->edge[4].src = 1;
graph->edge[4].dest = 4;
graph->edge[4].weight = 2;
graph->edge[5].src = 3;
graph->edge[5].dest = 2;
graph->edge[5].weight = 5;
graph->edge[6].src = 3;
graph->edge[6].dest = 1;
graph->edge[6].weight = 1;
graph->edge[7].src = 4;
graph->edge[7].dest = 3;
graph->edge[7].weight = -3;
BellmanFord(graph, 0);
return 0;
}
85
OUTPUT
RESULT
Thus the C++ program was implemented done successfully using Linux
Platform.
86
Program No:
Date:
AIM
To implement Matrix Chain Multiplication using C++ Program
PROCEDURE
Step 1: Open Ubuntu OS.
Step 2: Open terminal
Step 3: Check gcc version using the command gcc - -version
Step 4: Install/Update gcc use the command sudo apt install build-essential
Step 5: Use the Command for writing the Program touch filename.cpp
(Open the file using notepad & Save the Program (File Name can be
anything but it should end with .cpp extension)
Step 6: To Compile the Program use the command g++ filename.cpp
Step 7: To Run the Program ./a.out
Step 8: Output will be displayed in the Terminal.
PROGRAM
88
int main()
{
int arr[] = { 1, 2, 3, 4, 3 };
int n = sizeof(arr) / sizeof(arr[0]);
getchar();
return 0;
}
OUTPUT
RESULT
Thus the C++ program was implemented done successfully using Linux
Platform.
89
Program No:
Date:
AIM
To implement Activity Selection and Huffman Code using C++ Program
PROCEDURE
Step 1: Open Ubuntu OS.
Step 2: Open terminal
Step 3: Check gcc version using the command gcc - -version
Step 4: Install/Update gcc use the command sudo apt install build-essential
Step 5: Use the Command for writing the Program touch filename.cpp
(Open the file using notepad & Save the Program (File Name can be
anything but it should end with .cpp extension)
Step 6: To Compile the Program use the command g++ filename.cpp
Step 7: To Run the Program ./a.out
Step 8: Output will be displayed in the Terminal.
PROGRAM
// Activity Selection
include <bits/stdc++.h>
using namespace std;
// Driver code
int main()
{
int s[] = { 1, 3, 0, 5, 8, 5 };
91
int f[] = { 2, 4, 6, 7, 9, 9 };
int n = sizeof(s) / sizeof(s[0]);
// Function call
printMaxActivities(s, f,
n); return 0;
}
OUTPUT
//Huffman Coding
#include <bits/stdc++.h>
using namespace std;
struct MinHeapNode
{
92
char d;
unsigned frequency;
MinHeapNode *lChild, *rChild;
{
lChild = rChild = NULL;
this->d = d;
this->frequency = frequency;
}
};
//function to compare
struct compare
{
bool operator()(MinHeapNode *l, MinHeapNode *r)
{
return (l->frequency > r->frequency);
}
};
93
if (root->d != '$')
cout << root->d << ": " << str << "\n";
while (minHeap.size() != 1)
{
lChild = minHeap.top();
minHeap.pop();
rChild = minHeap.top();
minHeap.pop();
94
top->lChild = lChild;
top->rChild = rChild;
minHeap.push(top);
}
printCodes(minHeap.top(), "");
}
int main()
{
return 0;
}
95
OUTPUT
RESULT
Thus the C++ program was implemented done successfully using Linux
Platform.
96