CP4161 Adsa Lab Record
CP4161 Adsa Lab Record
CP4161 Adsa Lab Record
PAGE
EX.NO DATE EXPERIMENT NAME MARKS SIGN
NO
6
Implementation of heap
8 Graph traversals
1
EX.NO:1 IMPLEMENTATION OF RECURSIVE FUNCTION FOR TREE TRAVERSAL AND
DATE: FABONACCI
AIM:
Write a program to implementation of recursive function for tree traversal and Fibonacci in C++
language.
PROGRAM:
INORDER TRAVERSALS:
#include <iostream>
using namespace std;
return 0;
}
PREORDER TRAVERSALS:
#include <iostream>
using namespace std;
POSTORDER TRAVERSALS:
#include <iostream>
using namespace std;
return 0;
}
RECURSIVE OF FIBONACCI:
4
#include <iostream>
using namespace std;
int fib(int x) {
if((x==1)||(x==0)) {
return(x);
}else {
return(fib(x-1)+fib(x-2));
}
}
int main() {
int x , i=0;
cout << "Enter the number of terms of series : ";
cin >> x;
cout << "\nFibonnaci Series : ";
while(i < x) {
cout << " " << fib(i);
i++;
}
return 0;
}
OUTPUT:
42513
12453
45231
RESULTS:
Thus, the program is implementation and verified in recursive function for tree traversal and
Fibonacci in C++ language.
5
EX.NO:2 IMPLEMENTATION OF ITERATION FUNCTION FOR TREE TRAVERSAL
AND
DATE: FABONACCI
AIM:
Write a program to implementation of iteration function for tree traversal and Fibonacci in C++
language.
PROGRAM:
INORDER TRAVERSALS:
#include <iostream>
#include <stack>
using namespace std;
Node(int data)
{
this->data = data;
this->left = this->right = nullptr;
}
};
// start from the root node (set current node to the root node)
Node* curr = root;
// if the current node is null and the stack is also empty, we are done
while (!stack.empty() || curr != nullptr)
{
// if the current node exists, push it into the stack (defer it)
// and move to its left child
if (curr != nullptr)
{
stack.push(curr);
curr = curr->left;
}
6
else {
// otherwise, if the current node is null, pop an element from the stack,
// print it, and finally set the current node to its right child
curr = stack.top();
stack.pop();
cout << curr->data << " ";
curr = curr->right;
}
}
}
int main()
{
inorderIterative(root);
return 0;
}
PREORDER TRAVERSALS:
#include <iostream>
#include <stack>
using namespace std;
Node(int data)
{
this->data = data;
this->left = this->right = nullptr;
}
};
// push the right child of the popped node into the stack
if (curr->right) {
stack.push(curr->right);
}
// 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()
{
preorderIterative(root);
return 0;
}
POSTORDER TRAVERSALS:
#include <iostream>
#include <stack>
using namespace std;
8
// Data structure to store a binary tree node
struct Node
{
int data;
Node *left, *right;
Node(int data)
{
this->data = data;
this->left = this->right = nullptr;
}
};
out.push(curr->data);
// push the left and right child of the popped node into the stack
if (curr->left) {
s.push(curr->left);
}
if (curr->right) {
s.push(curr->right);
}
}
9
}
}
int main()
{
postorderIterative(root);
return 0;
}
ITERATION OF 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;
}
return 0;
}
OUTPUT:
10
Inorder traversal of binary tree is
42175836
12435786
42785631
Enter the number : 10
The fibonacci series : 0 1 1 2 3 5 8 13 21 34
RESULTS:
Thus, the program is implementation and verified in iteration function for tree traversal and
Fibonacci in C++ language.
11
EX.NO:3 IMPLEMENTATION OF MERGE SORT AND QUICK SORT
DATE:
AIM:
Write a program to implementation of Merge Sort and Quick Sort in C++ language.
PROGRAM:
#include <iostream>
int i, j, k, temp[high-low+1];
i = low;
k = 0;
j = mid + 1;
temp[k] = a[i];
k++;
i++;
12
else
temp[k] = a[j];
k++;
j++;
temp[k] = a[i];
k++;
i++;
temp[k] = a[j];
k++;
j++;
a[i] = temp[i-low];
13
}
int mid;
mid=(low+high)/2;
int main()
int n, i;
cin>>n;
int arr[n];
14
cin>>arr[i];
MergeSort(arr, 0, n-1);
cout<<"->"<<arr[i];
return 0;
#include <iostream>
void quick_sort(int[],int,int);
int partition(int[],int,int);
int main()
{
int a[50],n,i;
cout<<"How many elements?";
cin>>n;
cout<<"\nEnter array elements:";
for(i=0;i<n;i++)
cin>>a[i];
quick_sort(a,0,n-1);
cout<<"\nArray after sorting:";
for(i=0;i<n;i++)
cout<<a[i]<<" ";
return 0;
}
void quick_sort(int a[],int l,int u)
{
int j;
if(l<u)
{
j=partition(a,l,u);
quick_sort(a,l,j-1);
15
quick_sort(a,j+1,u);
}
}
int partition(int a[],int l,int u)
{
int v,i,j,temp;
v=a[l];
i=l;
j=u+1;
do
{
do
i++;
while(a[i]<v&&i<=u);
do
j--;
while(v<a[j]);
if(i<j)
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}while(i<j);
a[l]=a[j];
a[j]=v;
return(j);
}
OUTPUT:
Case 1:
Enter element 1: 23
Enter element 3: 45
Enter element 4: 65
Enter element 5: 32
16
Enter element 6: 9
Enter element 8: 1
Enter element 9: 17
Enter element 10: 3
RESULTS:
Thus, the program is implementation and verified in Merge Sort and Quick Sort in C++ language.
17
EX.NO:4 IMPLEMENTATION OF A BINARY SEARCH TREE
DATE:
AIM:
PROGRAM:
# include <iostream>
# include <cstdlib>
using namespace std;
struct nod//node declaration
{
int info;
struct nod *l;
struct nod *r;
}*r;
class BST
{
public://functions declaration
void search(nod *, int);
void find(int, nod **, nod **);
void insert(nod *, nod *);
void del(int);
void casea(nod *,nod *);
void caseb(nod *,nod *);
void casec(nod *,nod *);
void preorder(nod *);
void inorder(nod *);
void postorder(nod *);
void show(nod *, int);
BST()
{
r = NULL;
}
};
void BST::find(int i, nod **par, nod **loc)//find the position of the item
18
{
nod *ptr, *ptrsave;
if (r == NULL)
{
*loc = NULL;
*par = NULL;
return;
}
if (i == r→info)
{
*loc = r;
*par = NULL;
return;
}
if (i < r→info)
ptr = r→l;
else
ptr = r→r;
ptrsave = r;
while (ptr != NULL)
{
if (i == ptr→info)
{
*loc = ptr;
*par = ptrsave;
return;
}
ptrsave = ptr;
if (i < ptr→info)
ptr = ptr→l;
else
ptr = ptr→r;
}
*loc = NULL;
*par = ptrsave;
}
void BST::search(nod *root, int data) //searching
19
{
int depth = 0;
nod *temp = new nod;
temp = root;
while(temp != NULL)
{
depth++;
if(temp→info == data)
{
cout<<"\nData found at depth: "<<depth<<endl;
return;
}
else if(temp→info > data)
temp = temp→l;
else
temp = temp→r;
}
cout<<"\n Data not found"<<endl;
return;
}
void BST::insert(nod *tree, nod *newnode)
{
if (r == NULL)
{
r = new nod;
r→info = newnode→info;
r→l= NULL;
r→r= NULL;
cout<<"Root Node is Added"<<endl;
return;
}
if (tree→info == newnode→info)
{
cout<<"Element already in the tree"<<endl;
return;
}
if (tree→info > newnode→info)
20
{
if (tree→l != NULL)
{
insert(tree→l, newnode);
}
else
{
tree→l= newnode;
(tree→l)→l = NULL;
(tree→l)→r= NULL;
cout<<"Node Added To Left"<<endl;
return;
}
}
else
{
if (tree→r != NULL)
{
insert(tree→r, newnode);
}
else
{
tree→r = newnode;
(tree→r)→l= NULL;
(tree→r)→r = NULL;
cout<<"Node Added To Right"<<endl;
return;
}
}
}
void BST::del(int i)
{
nod *par, *loc;
if (r == NULL)
{
cout<<"Tree empty"<<endl;
return;
21
}
find(i, &par, &loc);
if (loc == NULL)
{
cout<<"Item not present in tree"<<endl;
return;
}
if (loc→l == NULL && loc→r == NULL)
{
casea(par, loc);
cout<<"item deleted"<<endl;
}
if (loc→l!= NULL && loc→r == NULL)
{
caseb(par, loc);
cout<<"item deleted"<<endl;
}
if (loc→l== NULL && loc→r != NULL)
{
caseb(par, loc);
cout<<"item deleted"<<endl;
}
if (loc→l != NULL && loc→r != NULL)
{
casec(par, loc);
cout<<"item deleted"<<endl;
}
free(loc);
}
void BST::casea(nod *par, nod *loc )
{
if (par == NULL)
{
r= NULL;
}
else
{
22
if (loc == par→l)
par→l = NULL;
else
par→r = NULL;
}
}
void BST::caseb(nod *par, nod *loc)
{
nod *child;
if (loc→l!= NULL)
child = loc→l;
else
child = loc→r;
if (par == NULL)
{
r = child;
}
else
{
if (loc == par→l)
par→l = child;
else
par→r = child;
}
}
void BST::casec(nod *par, nod *loc)
{
nod *ptr, *ptrsave, *suc, *parsuc;
ptrsave = loc;
ptr = loc→r;
while (ptr→l!= NULL)
{
ptrsave = ptr;
ptr = ptr→l;
}
suc = ptr;
parsuc = ptrsave;
23
if (suc→l == NULL && suc→r == NULL)
casea(parsuc, suc);
else
caseb(parsuc, suc);
if (par == NULL)
{
r = suc;
}
else
{
if (loc == par→l)
par→l = suc;
else
par→r= suc;
}
suc→l = loc→l;
suc→r= loc→r;
}
void BST::preorder(nod *ptr)
{
if (r == NULL)
{
cout<<"Tree is empty"<<endl;
return;
}
if (ptr != NULL)
{
cout<<ptr→info<<" ";
preorder(ptr→l);
preorder(ptr→r);
}
}
void BST::inorder(nod *ptr)//inorder traversal
{
if (r == NULL)
{
cout<<"Tree is empty"<<endl;
24
return;
}
if (ptr != NULL)
{
inorder(ptr→l);
cout<<ptr→info<<" ";
inorder(ptr→r);
}
}
void BST::postorder(nod *ptr)//postorder traversal
{
if (r == NULL)
{
cout<<"Tree is empty"<<endl;
return;
}
if (ptr != NULL)
{
postorder(ptr→l);
postorder(ptr→r);
cout<<ptr→info<<" ";
}
}
void BST::show(nod *ptr, int level)//print the tree
{
int i;
if (ptr != NULL)
{
show(ptr→r, level+1);
cout<<endl;
if (ptr == r)
cout<<"Root→: ";
else
{
for (i = 0;i < level;i++)
cout<<" ";
}
25
cout<<ptr→info;
show(ptr→l, level+1);
}
}
int main()
{
int c, n,item;
BST bst;
nod *t;
while (1)
{
cout<<"1.Insert Element "<<endl;
cout<<"2.Delete Element "<<endl;
cout<<"3.Search Element"<<endl;
cout<<"4.Inorder Traversal"<<endl;
cout<<"5.Preorder Traversal"<<endl;
cout<<"6.Postorder Traversal"<<endl;
cout<<"7.Display the tree"<<endl;
cout<<"8.Quit"<<endl;
cout<<"Enter your choice : ";
cin>>c;
switch(c)
{
case 1:
t = new nod;
cout<<"Enter the number to be inserted : ";
cin>>t→info;
bst.insert(r, t);
break;
case 2:
if (r == NULL)
{
cout<<"Tree is empty, nothing to delete"<<endl;
continue;
}
cout<<"Enter the number to be deleted : ";
cin>>n;
26
bst.del(n);
break;
case 3:
cout<<"Search:"<<endl;
cin>>item;
bst.search(r,item);
break;
case 4:
cout<<"Inorder Traversal of BST:"<<endl;
bst.inorder(r);
cout<<endl;
break;
case 5:
cout<<"Preorder Traversal of BST:"<<endl;
bst.preorder(r);
cout<<endl;
break;
case 6:
cout<<"Postorder Traversal of BST:"<<endl;
bst.postorder(r);
cout<<endl;
break;
case 7:
cout<<"Display BST:"<<endl;
bst.show(r,1);
cout<<endl;
break;
case 8:
exit(1);
default:
cout<<"Wrong choice"<<endl;
}
}
}
27
OUTPUT:
1.Insert Element
2.Delete Element
3.Search Element
4.Inorder Traversal
5.Preorder Traversal
6.Postorder Traversal
7.Display the tree
8.Quit
Enter your choice : 1
Enter the number to be inserted : 6
Root Node is Added
1.Insert Element
2.Delete Element
3.Search Element
4.Inorder Traversal
5.Preorder Traversal
6.Postorder Traversal
7.Display the tree
8.Quit
Enter your choice : 1
Enter the number to be inserted : 7
Node Added To Right
1.Insert Element
2.Delete Element
3.Search Element
4.Inorder Traversal
5.Preorder Traversal
6.Postorder Traversal
7.Display the tree
8.Quit
Enter your choice : 1
Enter the number to be inserted : 5
Node Added To Left
1.Insert Element
2.Delete Element
3.Search Element
28
4.Inorder Traversal
5.Preorder Traversal
6.Postorder Traversal
7.Display the tree
8.Quit
Enter your choice : 1
Enter the number to be inserted : 4
Node Added To Left
1.Insert Element
2.Delete Element
3.Search Element
4.Inorder Traversal
5.Preorder Traversal
6.Postorder Traversal
7.Display the tree
8.Quit
Enter your choice : 3
Search:
7
Data found at depth: 2
1.Insert Element
2.Delete Element
3.Search Element
4.Inorder Traversal
5.Preorder Traversal
6.Postorder Traversal
7.Display the tree
8.Quit
Enter your choice : 3
Search:
1
Data not found
1.Insert Element
2.Delete Element
3.Search Element
4.Inorder Traversal
5.Preorder Traversal
29
6.Postorder Traversal
7.Display the tree
8.Quit
Enter your choice : 4
Inorder Traversal of BST:
4567
1.Insert Element
2.Delete Element
3.Search Element
4.Inorder Traversal
5.Preorder Traversal
6.Postorder Traversal
7.Display the tree
8.Quit
Enter your choice : 5
Preorder Traversal of BST:
6547
1.Insert Element
2.Delete Element
3.Search Element
4.Inorder Traversal
5.Preorder Traversal
6.Postorder Traversal
7.Display the tree
8.Quit
Enter your choice : 6
Postorder Traversal of BST:
4576
1.Insert Element
2.Delete Element
3.Search Element
4.Inorder Traversal
5.Preorder Traversal
6.Postorder Traversal
7.Display the tree
8.Quit
Enter your choice : 7
30
Display BST:
7
Root→: 6
5
4
1.Insert Element
2.Delete Element
3.Search Element
4.Inorder Traversal
5.Preorder Traversal
6.Postorder Traversal
7.Display the tree
8.Quit
Enter your choice : 2
Enter the number to be deleted : 1
Item not present in tree
1.Insert Element
2.Delete Element
3.Search Element
4.Inorder Traversal
5.Preorder Traversal
6.Postorder Traversal
7.Display the tree
8.Quit
Enter your choice : 5
Preorder Traversal of BST:
6547
1.Insert Element
2.Delete Element
3.Search Element
4.Inorder Traversal
5.Preorder Traversal
6.Postorder Traversal
7.Display the tree
8.Quit
Enter your choice : 2
Enter the number to be deleted : 5
31
item deleted
1.Insert Element
2.Delete Element
3.Search Element
4.Inorder Traversal
5.Preorder Traversal
6.Postorder Traversal
7.Display the tree
8.Quit
Enter your choice : 7
Display BST:
7
Root→: 6
4
1.Insert Element
2.Delete Element
3.Search Element
4.Inorder Traversal
5.Preorder Traversal
6.Postorder Traversal
7.Display the tree
8.Quit
Enter your choice :
RESULTS:
Thus, the program is implementation and verified in a Binary Search Tree in C++ language.
32
EX.NO:5 IMPLEMENTATION OF RED-BLACK TREE
DATE:
AIM:
PROGRAM:
#include <iostream>
struct Node {
};
class RBTree {
private:
NodePtr root;
33
NodePtr TNULL;
node->data = 0;
node->parent = parent;
node->left = nullptr;
node->right = nullptr;
node->color = 0;
if (node != TNULL) {
cout<<node->data<<" ";
preOrderHelper(node->left);
preOrderHelper(node->right);
if (node != TNULL) {
inOrderHelper(node->left);
cout<<node->data<<" ";
inOrderHelper(node->right);
postOrderHelper(node->left);
postOrderHelper(node->right);
cout<<node->data<<" ";
return node;
void fixDelete(NodePtr x) {
NodePtr s;
if (x == x->parent->left) {
s = x->parent->right;
if (s->color == 1) {
// case 3.1
s->color = 0;
x->parent->color = 1;
leftRotate(x->parent);
35
s = x->parent->right;
// case 3.2
s->color = 1;
x = x->parent;
} else {
if (s->right->color == 0) {
// case 3.3
s->left->color = 0;
s->color = 1;
rightRotate(s);
s = x->parent->right;
// case 3.4
s->color = x->parent->color;
x->parent->color = 0;
s->right->color = 0;
leftRotate(x->parent);
x = root;
} else {
s = x->parent->left;
if (s->color == 1) {
// case 3.1
s->color = 0;
x->parent->color = 1;
36
rightRotate(x->parent);
s = x->parent->left;
// case 3.2
s->color = 1;
x = x->parent;
} else {
if (s->left->color == 0) {
// case 3.3
s->right->color = 0;
s->color = 1;
leftRotate(s);
s = x->parent->left;
// case 3.4
s->color = x->parent->color;
x->parent->color = 0;
s->left->color = 0;
rightRotate(x->parent);
x = root;
x->color = 0;
37
void rbTransplant(NodePtr u, NodePtr v){
if (u->parent == nullptr) {
root = v;
} else if (u == u->parent->left){
u->parent->left = v;
} else {
u->parent->right = v;
v->parent = u->parent;
NodePtr z = TNULL;
NodePtr x, y;
if (node->data == key) {
z = node;
node = node->right;
} else {
node = node->left;
if (z == TNULL) {
38
cout<<"Couldn't find key in the tree"<<endl;
return;
y = z;
if (z->left == TNULL) {
x = z->right;
rbTransplant(z, z->right);
x = z->left;
rbTransplant(z, z->left);
} else {
y = minimum(z->right);
y_original_color = y->color;
x = y->right;
if (y->parent == z) {
x->parent = y;
} else {
rbTransplant(y, y->right);
y->right = z->right;
y->right->parent = y;
rbTransplant(z, y);
y->left = z->left;
y->left->parent = y;
y->color = z->color;
}
39
delete z;
if (y_original_color == 0){
fixDelete(x);
NodePtr u;
while (k->parent->color == 1) {
if (k->parent == k->parent->parent->right) {
u = k->parent->parent->left; // uncle
if (u->color == 1) {
// case 3.1
u->color = 0;
k->parent->color = 0;
k->parent->parent->color = 1;
k = k->parent->parent;
} else {
if (k == k->parent->left) {
// case 3.2.2
k = k->parent;
rightRotate(k);
// case 3.2.1
k->parent->color = 0;
k->parent->parent->color = 1;
leftRotate(k->parent->parent);
}
40
} else {
u = k->parent->parent->right; // uncle
if (u->color == 1) {
u->color = 0;
k->parent->color = 0;
k->parent->parent->color = 1;
k = k->parent->parent;
} else {
if (k == k->parent->right) {
k = k->parent;
leftRotate(k);
k->parent->color = 0;
k->parent->parent->color = 1;
rightRotate(k->parent->parent);
if (k == root) {
break;
root->color = 0;
if (root != TNULL) {
cout<<indent;
if (last) {
cout<<"R----";
} else {
cout<<"L----";
cout<<root->data<<"("<<sColor<<")"<<endl;
// cout<<root->left->data<<endl;
public:
RBTree() {
TNULL->color = 0;
TNULL->left = nullptr;
TNULL->right = nullptr;
root = TNULL;
// Pre-Order traversal
42
// Node->Left Subtree->Right Subtree
void preorder() {
preOrderHelper(this->root);
// In-Order traversal
void inorder() {
inOrderHelper(this->root);
// Post-Order traversal
void postorder() {
postOrderHelper(this->root);
NodePtr searchTree(int k) {
node = node->left;
return node;
43
}
node = node->right;
return node;
NodePtr successor(NodePtr x) {
// right subtree
if (x->right != TNULL) {
return minimum(x->right);
NodePtr y = x->parent;
x = y;
y = y->parent;
return y;
44
// find the predecessor of a given node
NodePtr predecessor(NodePtr x) {
// left subtree
if (x->left != TNULL) {
return maximum(x->left);
NodePtr y = x->parent;
x = y;
y = y->parent;
return y;
void leftRotate(NodePtr x) {
NodePtr y = x->right;
x->right = y->left;
if (y->left != TNULL) {
y->left->parent = x;
y->parent = x->parent;
if (x->parent == nullptr) {
this->root = y;
} else if (x == x->parent->left) {
45
x->parent->left = y;
} else {
x->parent->right = y;
y->left = x;
x->parent = y;
void rightRotate(NodePtr x) {
NodePtr y = x->left;
x->left = y->right;
if (y->right != TNULL) {
y->right->parent = x;
y->parent = x->parent;
if (x->parent == nullptr) {
this->root = y;
} else if (x == x->parent->right) {
x->parent->right = y;
} else {
x->parent->left = y;
y->right = x;
x->parent = y;
node->parent = nullptr;
node->data = key;
node->left = TNULL;
node->right = TNULL;
NodePtr y = nullptr;
NodePtr x = this->root;
while (x != TNULL) {
y = x;
x = x->left;
} else {
x = x->right;
// y is parent of x
node->parent = y;
if (y == nullptr) {
root = node;
y->left = node;
} else {
y->right = node;
47
}
if (node->parent == nullptr){
node->color = 0;
return;
if (node->parent->parent == nullptr) {
return;
fixInsert(node);
NodePtr getRoot(){
return this->root;
deleteNodeHelper(this->root, data);
void prettyPrint() {
if (root) {
48
printHelper(this->root, "", true);
};
int main() {
RBTree bst;
bst.insert(8);
bst.insert(18);
bst.insert(5);
bst.insert(15);
bst.insert(17);
bst.insert(25);
bst.insert(40);
bst.insert(80);
bst.deleteNode(25);
bst.prettyPrint();
return 0;
OUTPUT:
R----17(BLACK)
L----8(RED)
L----5(BLACK)
R----15(BLACK)
R----40(RED)
49
L----18(BLACK)
R----80(BLACK)
RESULTS:
Thus, the program is implementation and verified in Red-Black Tree in C++ language.
50
EX.NO:6 IMPLEMENTATION OF HEAP
DATE:
AIM:
PROGRAM:
#include <iostream>
#include <cstdlib>
#include <vector>
#include <iterator>
/*
* Class Declaration
*/
class Heap
private:
public:
Heap()
{}
void DeleteMin();
int ExtractMin();
51
void DisplayHeap();
int Size();
};
/*
*/
int Heap::Size()
return heap.size();
/*
*/
heap.push_back(element);
heapifyup(heap.size() -1);
/*
*/
void Heap::DeleteMin()
if (heap.size() == 0)
cout<<"Heap is Empty"<<endl;
return;
}
52
heap[0] = heap.at(heap.size() - 1);
heap.pop_back();
heapifydown(0);
cout<<"Element Deleted"<<endl;
/*
*/
int Heap::ExtractMin()
if (heap.size() == 0)
return -1;
else
return heap.front();
/*
* Display Heap
*/
void Heap::DisplayHeap()
cout<<*pos<<" ";
53
pos++;
cout<<endl;
/*
*/
int l = 2 * parent + 1;
return l;
else
return -1;
/*
*/
int r = 2 * parent + 2;
return r;
else
return -1;
54
/*
* Return Parent
*/
if(child == 0)
return -1;
else
return p;
/*
*/
heap[in] = heap[parent(in)];
heap[parent(in)] = temp;
heapifyup(parent(in));
/*
*/
55
void Heap::heapifydown(int in)
child = child1;
if (child > 0)
heap[in] = heap[child];
heap[child] = temp;
heapifydown(child);
/*
*/
int main()
Heap h;
while (1)
cout<<"------------------"<<endl;
cout<<"Operations on Heap"<<endl;
cout<<"------------------"<<endl;
56
cout<<"1.Insert Element"<<endl;
cout<<"4.Print Heap"<<endl;
cout<<"5.Exit"<<endl;
cin>>choice;
switch(choice)
case 1:
cin>>element;
h.Insert(element);
break;
case 2:
h.DeleteMin();
break;
case 3:
if (h.ExtractMin() == -1)
cout<<"Heap is Empty"<<endl;
else
break;
case 4:
break;
case 5:
exit(1);
default:
return 0;
OUTPUT:
------------------
Operations on Heap
------------------
1.Insert Element
4.Print Heap
5.Exit
------------------
Operations on Heap
------------------
1.Insert Element
4.Print Heap
5.Exit
------------------
Operations on Heap
------------------
1.Insert Element
4.Print Heap
5.Exit
------------------
Operations on Heap
------------------
1.Insert Element
4.Print Heap
5.Exit
------------------
Operations on Heap
------------------
1.Insert Element
59
2.Delete Minimum Element
4.Print Heap
5.Exit
------------------
Operations on Heap
------------------
1.Insert Element
4.Print Heap
5.Exit
------------------
Operations on Heap
------------------
1.Insert Element
4.Print Heap
5.Exit
------------------
Operations on Heap
------------------
60
1.Insert Element
4.Print Heap
5.Exit
------------------
Operations on Heap
------------------
1.Insert Element
4.Print Heap
5.Exit
------------------
Operations on Heap
------------------
1.Insert Element
4.Print Heap
5.Exit
------------------
Operations on Heap
61
------------------
1.Insert Element
4.Print Heap
5.Exit
------------------
Operations on Heap
------------------
1.Insert Element
4.Print Heap
5.Exit
------------------
Operations on Heap
------------------
1.Insert Element
4.Print Heap
5.Exit
------------------
62
Operations on Heap
------------------
1.Insert Element
4.Print Heap
5.Exit
------------------
Operations on Heap
------------------
1.Insert Element
4.Print Heap
5.Exit
------------------
Operations on Heap
------------------
1.Insert Element
4.Print Heap
5.Exit
Element Deleted
63
------------------
Operations on Heap
------------------
1.Insert Element
4.Print Heap
5.Exit
------------------
Operations on Heap
------------------
1.Insert Element
4.Print Heap
5.Exit
------------------
Operations on Heap
------------------
1.Insert Element
4.Print Heap
5.Exit
------------------
Operations on Heap
------------------
1.Insert Element
4.Print Heap
5.Exit
------------------
Operations on Heap
------------------
1.Insert Element
4.Print Heap
5.Exit
------------------
Operations on Heap
------------------
1.Insert Element
4.Print Heap
5.Exit
65
Enter your choice: 5
------------------
RESULTS:
66
EX.NO:7 IMPLEMENTATION OF FIBONACCI HEAP
DATE:
AIM:
PROGRAM:
#include <iostream>
#include <cmath>
#include <cstdlib>
/*
* Node Declaration
*/
struct node
int n;
int degree;
node* parent;
node* child;
node* left;
node* right;
char mark;
char C;
};
/*
* Class Declaration
*/
class FibonacciHeap
{
67
private:
int nH;
node *H;
public:
node* InitializeHeap();
node *Create_node(int);
FibonacciHeap()
H = InitializeHeap();
};
/*
* Initialize Heap
*/
node* FibonacciHeap::InitializeHeap()
node* np;
np = NULL;
68
return np;
/*
* Create Node
*/
x->n = value;
return x;
/*
* Insert Node
*/
x->degree = 0;
x->parent = NULL;
x->child = NULL;
x->left = x;
x->right = x;
x->mark = 'F';
x->C = 'N';
if (H != NULL)
(H->left)->right = x;
x->right = H;
x->left = H->left;
H->left = x;
69
if (x->n < H->n)
H = x;
else
H = x;
nH = nH + 1;
return H;
/*
*/
(y->left)->right = y->right;
(y->right)->left = y->left;
if (z->right == z)
H1 = z;
y->left = y;
y->right = y;
y->parent = z;
if (z->child == NULL)
z->child = y;
y->right = z->child;
y->left = (z->child)->left;
((z->child)->left)->right = y;
(z->child)->left = y;
z->degree++;
/*
*/
node* np;
node* H = InitializeHeap();
H = H1;
(H->left)->right = H2;
(H2->left)->right = H;
np = H->left;
H->left = H2->left;
H2->left = np;
return H;
/*
*/
int FibonacciHeap::Display(node* H)
node* p = H;
if (p == NULL)
return 0;
}
71
cout<<"The root nodes of Heap are: "<<endl;
do
cout<<p->n;
p = p->right;
if (p != H)
cout<<"-->";
cout<<endl;
/*
*/
node* p;
node* ptr;
node* z = H1;
p = z;
ptr = z;
if (z == NULL)
return z;
node* x;
node* np;
x = NULL;
if (z->child != NULL)
72
x = z->child;
if (x != NULL)
ptr = x;
do
np = x->right;
(H1->left)->right = x;
x->right = H1;
x->left = H1->left;
H1->left = x;
H1 = x;
x->parent = NULL;
x = np;
(z->left)->right = z->right;
(z->right)->left = z->left;
H1 = z->right;
H = NULL;
else
H1 = z->right;
Consolidate(H1);
nH = nH - 1;
73
return p;
/*
*/
int d, i;
int D = f;
node* A[D];
A[i] = NULL;
node* x = H1;
node* y;
node* np;
node* pt = x;
do
pt = pt->right;
d = x->degree;
y = A[d];
np = x;
x = y;
y = np;
74
}
if (y == H1)
H1 = x;
Fibonnaci_link(H1, y, x);
if (x->right == x)
H1 = x;
A[d] = NULL;
d = d + 1;
A[d] = x;
x = x->right;
while (x != H1);
H = NULL;
if (A[j] != NULL)
A[j]->left = A[j];
A[j]->right =A[j];
if (H != NULL)
(H->left)->right = A[j];
A[j]->right = H;
A[j]->left = H->left;
H->left = A[j];
H = A[j];
}
75
else
H = A[j];
if(H == NULL)
H = A[j];
H = A[j];
/*
*/
node* y;
if (H1 == NULL)
return 0;
if (ptr == NULL)
return 1;
}
76
if (ptr->n < k)
return 0;
ptr->n = k;
y = ptr->parent;
Cascase_cut(H1, y);
H = ptr;
return 0;
/*
*/
if (x == x->right)
y->child = NULL;
(x->left)->right = x->right;
(x->right)->left = x->left;
if (x == y->child)
y->child = x->right;
y->degree = y->degree - 1;
x->right = x;
77
x->left = x;
(H1->left)->right = x;
x->right = H1;
x->left = H1->left;
H1->left = x;
x->parent = NULL;
x->mark = 'F';
/*
*/
node* z = y->parent;
if (z != NULL)
if (y->mark == 'F')
y->mark = 'T';
else
Cut(H1, y, z);
Cascase_cut(H1, z);
78
/*
*/
node* x = H;
x->C = 'Y';
node* p = NULL;
if (x->n == k)
p = x;
x->C = 'N';
return p;
if (p == NULL)
if (x->child != NULL )
p = Find(x->child, k);
if ((x->right)->C != 'Y' )
p = Find(x->right, k);
x->C = 'N';
return p;
/*
*/
{
79
node* np = NULL;
int t;
t = Decrease_key(H1, k, -5000);
if (!t)
np = Extract_Min(H);
if (np != NULL)
cout<<"Key Deleted"<<endl;
else
return 0;
/*
*/
int main()
int n, m, l;
FibonacciHeap fh;
node* p;
node* H;
H = fh.InitializeHeap();
while (1)
cout<<"----------------------------"<<endl;
cout<<"----------------------------"<<endl;
cout<<"5)Display Heap"<<endl;
cout<<"6)Exit"<<endl;
cin>>l;
switch(l)
case 1:
cin>>m;
p = fh.Create_node(m);
H = fh.Insert(H, p);
break;
case 2:
p = fh.Extract_Min(H);
if (p != NULL)
else
cout<<"Heap is empty"<<endl;
break;
case 3:
cin>>m;
cin>>l;
fh.Decrease_key(H, m, l);
break;
case 4:
fh.Delete_key(H, m);
break;
case 5:
fh.Display(H);
break;
case 6:
exit(1);
default:
cout<<"Wrong Choice"<<endl;
return 0;
OUTPUT:
----------------------------
----------------------------
4)Delete a node
5)Display Heap
6)Exit
----------------------------
----------------------------
4)Delete a node
5)Display Heap
6)Exit
----------------------------
----------------------------
4)Delete a node
5)Display Heap
6)Exit
----------------------------
4)Delete a node
5)Display Heap
6)Exit
----------------------------
----------------------------
4)Delete a node
5)Display Heap
6)Exit
----------------------------
----------------------------
4)Delete a node
84
5)Display Heap
6)Exit
5-->6-->7-->8-->9
----------------------------
----------------------------
4)Delete a node
5)Display Heap
6)Exit
----------------------------
----------------------------
4)Delete a node
5)Display Heap
6)Exit
----------------------------
----------------------------
4)Delete a node
5)Display Heap
6)Exit
----------------------------
----------------------------
4)Delete a node
5)Display Heap
6)Exit
----------------------------
----------------------------
4)Delete a node
5)Display Heap
6)Exit
------------------
RESULTS:
Thus, the program is implementation and verified in Fibonacci Heap in C++ language.
87
EX.NO:8 GRAPH TRAVERSALS
DATE:
AIM:
PROGRAM:
#include <iostream>
#include <list>
#include <memory>
class Graph
{
int _V;
bool _directed;
std::unique_ptr< std::list<int> > adj;
public:
Graph(int V, bool directed);
void AddEdge(int v, int w);
void BreadthFirstSearch(int s);
};
if (!_directed)
{
adjacency[w].push_back(v);
}
}
void Graph::BreadthFirstSearch(int s)
{
bool *visited = new bool[_V];
for(int i = 0; i < _V; i++)
visited[i] = false;
88
// Create a queue for BFS
std::list<int> queue;
visited[s] = true;
queue.push_back(s);
while(!queue.empty())
{
// Dequeue a vertex from queue and print it
s = queue.front();
std::cout << s << " ";
queue.pop_front();
int main()
{
Graph g(7, true);
g.AddEdge(0, 1);
g.AddEdge(0, 2);
g.AddEdge(0, 3);
g.AddEdge(1, 0);
g.AddEdge(1, 5);
g.AddEdge(2, 5);
g.AddEdge(3, 0);
g.AddEdge(3, 4);
g.AddEdge(4, 6);
g.AddEdge(5, 1);
g.AddEdge(6, 5);
return 0;
}
89
#include <iostream>
#include <list>
#include <memory>
class Graph
{
private:
int _V;
bool _directed;
std::unique_ptr< std::list<int> > adj;
void DFSUtil(int v, bool visited[]);
public:
Graph(int V, bool directed);
void AddEdge(int v, int w);
void DepthFirstSearch(int s);
};
if (!_directed)
{
adjacency[w].push_back(v);
}
}
90
std::unique_ptr<bool[]> visited(new bool[_V]);
int main()
{
// Create a graph given in the above diagram
Graph g(7, true);
g.AddEdge(0, 1);
g.AddEdge(0, 2);
g.AddEdge(0, 3);
g.AddEdge(1, 0);
g.AddEdge(1, 5);
g.AddEdge(2, 5);
g.AddEdge(3, 0);
g.AddEdge(3, 4);
g.AddEdge(4, 6);
g.AddEdge(5, 1);
g.AddEdge(6, 5);
return 0;
}
OUTPUT:
91
Depth First Search:
RESULTS:
Thus, the program is implementation and verified in Graph Traversals in C++ language.
92
EX.NO:9 IMPLEMENTATION OF SPANNING TREE
DATE:
AIM:
PROGRAM:
#include <bits/stdc++.h>
class Edge{
public:
int src;
int dest;
int wt;
};
return e1.wt<e2.wt;
if(parent[v]==v){
return v;
return getParent(parent[v],parent);
int main()
93
{
int n,E;
cin>>n>>E;
Edge edges[E];
for(int i=0;i<E;i++){
cin>>edges[i].src>>edges[i].dest>>edges[i].wt;
//Kruskal algo
sort(edges,edges+E,compare);
Edge output[n-1];
int parent[n];
for(int i=0;i<n;i++){
parent[i]=i;
int count=0;
int i=0;
while(count<n-1){
Edge currentEdge=edges[i];
int p1=getParent(currentEdge.src,parent);
int p2=getParent(currentEdge.dest,parent);
if(p1!=p2){
output[count]=currentEdge;
94
count++;
parent[p1]=p2;
i++;
for(int i=0;i<n-1;i++){
if(output[i].src<output[i].dest){
return 0;
OUTPUT:
Input:
44
013
035
95
121
238
Output:
121
013
035
RESULTS:
Thus, the program is implementation and verified in Spanning Tree in C++ language.
96
EX.NO:10 SHORTEST PATH ALGORITHMS (DIJKSTRA’S ALGORITHM, BELLMAN FORD
DATE: ALGORITHM
AIM:
Write a program to implementation of shortest path algorithms (dijkstra’s algorithm, bellman ford
algorithm in C++ language.
PROGRAM:
DIJKSTRA’S ALGORITHM:
#include<iostream>
#include<climits>
using namespace std;
for(int k=0;k<6;k++)
{
if(Tset[k]==false && distance[k]<=minimum)
{
minimum=distance[k];
ind=k;
}
}
return ind;
}
int main()
{
int graph[6][6]={
{0, 1, 2, 0, 0, 0},
{1, 0, 0, 5, 1, 0},
{2, 0, 0, 2, 3, 0},
{0, 5, 2, 0, 2, 2},
{0, 1, 3, 2, 0, 1},
{0, 0, 0, 2, 1, 0}};
DijkstraAlgo(graph,0);
return 0;
}
#include<iostream>
#define MAX 10
using namespace std;
98
/* distance of source vertex from source vertex is o */
dis[src_graph]=0;
int main()
{
int nv,ne,src_graph;
edge e[MAX];
99
/* if you enter no of vertices: 5 then vertices will be 1,2,3,4,5. so while giving input enter source and
destination vertex accordingly */
printf("Enter the source vertex of the graph: ");
cin>>src_graph;
for(int i=0;i<ne;i++)
{
cout<<"\nFor edge "<<i+1<<"=>";
cout<<"\nEnter source vertex :";
cin>>e[i].src;
cout<<"Enter destination vertex :";
cin>>e[i].dest;
cout<<"Enter weight :";
cin>>e[i].wt;
}
bellman_ford(nv,e,src_graph,ne);
return 0;
}
OUTPUT:
DIJKSTRA’S ALGORITHM:
A 0
B 1
C 2
D 4
E 2
F 3
100
For edge 2=>
Enter source vertex :1
Enter destination vertex :3
Enter weight :3
RESULTS:
Thus, the program is implementation and verified in shortest path algorithms (dijkstra’s algorithm,
bellman ford algorithm in C++ language.
101
EX.NO:11 IMPLEMENTATION OF MATRIX CHAIN MULTIPLICATION
DATE:
AIM:
PROGRAM:
#include<iostream>
#include<limits.h>
int main()
{
int n,i;
cout<<"Enter number of matrices\n";
102
cin>>n;
n++;
int arr[n];
for(i=0;i<n;i++)
{
cout<<"Enter d"<<i<<" :: ";
cin>>arr[i];
}
return 0;
}
OUTPUT:
RESULTS:
Thus, the program is implementation and verified in Matrix chain multiplication in C++ language.
103
EX.NO:12 IMPLEMENTATION OF ACTIVITY SELECTION AND HUFFMAN CODING
DATE:
AIM:
Write a program to implementation of Activity selection and Huffman coding in C++ language.
PROGRAM:
ACTIVITY SELECTION:
#include <bits/stdc++.h>
struct activity
};
cout << "(" << vec[i].start << ", " << vec[i].finish << "), ";
if (vec[j].start >=vec[i].finish)
i = j;
int main()
activity vec[] = {{4, 8}, {2, 5}, {4, 6}, {1, 4},
int n = sizeof(vec)/sizeof(vec[0]);
print(vec, n);
return 0;
HUFFMAN CODING:
105
#include <bits/stdc++.h>
struct MinHeapNode {
char data;
unsigned freq;
this->data = data;
this->freq = freq;
};
struct compare {
};
106
void printCodes(struct MinHeapNode* root, string str)
if (!root)
return;
if (root->data != '$')
cout << root->data << ": " << str << "\n";
while (minHeap.size() != 1) {
left = minHeap.top();
minHeap.pop();
right = minHeap.top();
minHeap.pop();
107
top->left = left;
top->right = right;
minHeap.push(top);
printCodes(minHeap.top(), "");
int main()
return 0;
OUTPUT:
ACTIVITY SELECTION:
HUFFMAN CODING:
b:000
e:001
c:01
d:10
108
f:111
a:110
RESULTS:
Thus, the program is implementation and verified in Activity selection and Huffman coding in C++
language.
109