Hulas Ds Record

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 24

PROGRAM 14:

Write a C++ program for implementation of tree traversal

Aim:

Algorithm:

Code:
#include<iostream>

using namespace std;

class Tree

public:

int data;

Tree *left = NULL, *right = NULL;


Tree (int x)

data = x;

left = NULL;

right = NULL;

};

void preorder_traversal (Tree * root)

if (root ==

NULL) return;

cout << root->data << " ";

preorder_traversal (root->left);

preorder_traversal (root->right);

void inorder_traversal (Tree * root)

if (root ==

NULL) return;

inorder_traversal (root->left);

cout << root->data << " ";

inorder_traversal (root->right);
}

void postorder_traversal (Tree * root)

if (root ==

NULL) return;

postorder_traversal (root->left);

postorder_traversal (root->right);

cout << root->data << " ";

int main ()

Tree *root = new Tree (17);

root->left = new Tree (10);

root->right = new Tree (11);

root->left->left = new Tree (7);

root->right->left = new Tree (27);

root->right->right = new Tree

(9); cout << "Preorder => ";

preorder_traversal (root);

cout << endl;

cout << "Inorder => ";

inorder_traversal (root);
cout << endl;

cout << "Postorder => ";

postorder_traversal (root);

cout << endl;

return 0;

OUTPUT:

RESULT:
PROGRAM 15:

Write a C++ program for implementation of priority queue

Aim:

Algorithm:

Code:
#include <iostream>

#include <cstdio>

#include <cstring>

#include <cstdlib>

using namespace std;

/*

* Node Declaration
*/

struct node

int priority;

int info;

struct node *link;

};

/*

* Class Priority Queue

*/

class Priority_Queue

private:

node *front;

public:

Priority_Queue()

front = NULL;

/*

* Insert into Priority Queue

*/
void insert(int item, int priority)

node *tmp, *q;

tmp = new node;

tmp->info =

item;

tmp->priority = priority;

if (front == NULL || priority < front->priority)

tmp->link = front;

front = tmp;

else

q = front;

while (q->link != NULL && q->link->priority <=

priority) q=q->link;

tmp->link = q-

>link; q->link =

tmp;

/*
* Delete from Priority Queue
*/

void del()

node *tmp;

if(front == NULL)

cout<<"Queue Underflow\n";

else

tmp = front;

cout<<"Deleted item is: "<<tmp->info<<endl;

front = front->link;

free(tmp);

/*

* Print Priority Queue

*/

void display()

node *ptr;

ptr = front;

if (front == NULL)
cout<<"Queue is empty\n";

else

{ cout<<"Queue is :\n";

cout<<"Priority Item\n";

while(ptr != NULL)

cout<<ptr->priority<<" "<<ptr->info<<endl;

ptr = ptr->link;

};

/*

* Main

*/

int main()

int choice, item, priority;

Priority_Queue pq;

do

cout<<"1.Insert\n";
cout<<"2.Delete\n";

cout<<"3.Display\n";

cout<<"4.Quit\n";

cout<<"Enter your choice : ";

cin>>choice;

switch(choice)

case 1:

cout<<"Input the item value to be added in the queue : ";

cin>>item;

cout<<"Enter its priority : ";

cin>>priority;

pq.insert(item, priority);

break;

case 2:

pq.del();

break;

case 3:

pq.display();

break;

case 4:

break;
default :

cout<<"Wrong choice\n";

while(choice != 4);

return 0;

OUTPUT:

RESULT:
PROGRAM 16:

Write a C++ program for implementation of AVL tree

Aim:

Algorithm:

Code:
#include<iostream>

using namespace std;

class AVLtree

public:

int key;

AVLtree *left;

AVLtree *right;
int height;

};

int max(int a, int b)

return (a > b)? a : b;

int getHeight(AVLtree *tree)

if (tree == NULL)

return 0;

return tree->height;

AVLtree* newNode(int key)

AVLtree* tree = new

AVLtree(); tree->key = key;

tree->left = NULL;

tree->right =

NULL; tree->height

= 1; return(tree);

AVLtree *leftRotate(AVLtree *t1)


{

AVLtree *curr = t1->right;

AVLtree *t2 = curr->left;

curr->left = t1;

t1->right = t2;

t1->height = max(getHeight(t1->left),

getHeight(t1->right)) + 1;

curr->height = max(getHeight(curr->left),

getHeight(curr->right)) + 1;

return curr;

AVLtree *rightRotate(AVLtree *t1)

AVLtree *curr = t1->left;

AVLtree *t2 = curr->right;

curr->right =

t1; t1->left = t2;

t1->height = max(getHeight(t1->left),

getHeight(t1->right)) + 1;
curr->height = max(getHeight(curr->left),

getHeight(curr->right)) + 1;

return curr;

int getBalance(AVLtree *tree)

if (tree == NULL)

return 0;

return getHeight(tree->left) - getHeight(tree->right);

AVLtree* insertNode(AVLtree* tree, int key)

if (tree == NULL)

return(newNode(key));

if (key < tree->key)

tree->left = insertNode(tree->left, key);

else if (key > tree->key)

tree->right = insertNode(tree->right, key);

else

return tree;
tree->height = 1 + max(getHeight(tree->left),

getHeight(tree->right));

int balance = getBalance(tree);

if (balance > 1 && key < tree->left->key)

return rightRotate(tree);

if (balance < -1 && key > tree->right->key)

return leftRotate(tree);

if (balance > 1 && key > tree->left->key)

tree->left = leftRotate(tree->left);

return rightRotate(tree);

if (balance < -1 && key < tree->right->key)

tree->right = rightRotate(tree->right);

return leftRotate(tree);

return tree;
}

void preOrder(AVLtree *root)

if(root != NULL)

cout << root->key << "

"; preOrder(root->left);

preOrder(root->right);

int main()

AVLtree *tree = NULL;

tree = insertNode(tree,

10); tree =

insertNode(tree, 20); tree

= insertNode(tree, 30);

tree = insertNode(tree,

40); tree =

insertNode(tree, 50); tree

= insertNode(tree, 25);

cout << "Preorder traversal of the AVL tree is: \t";

preOrder(tree);
return 0;}
OUTPUT:

RESULT:
PROGRAM 17:

Write a C++ program for implementation of shortest path algorithm

Aim:

Algorithm:

Code:
#include <iostream>

using namespace std;

#include <limits.h>

#define V 9

int minDistance(int dist[], bool sptSet[])

int min = INT_MAX, min_index;


for (int v = 0; v < V; v++)

if (sptSet[v] == false && dist[v] <= min)

min = dist[v], min_index = v;

return min_index;

void printSolution(int dist[])

cout << "Vertex \t Distance from Source" << endl;

for (int i = 0; i < V; i++)

cout << i << " \t\t\t\t" << dist[i] << endl;

void dijkstra(int graph[V][V], int src)

int dist[V];

bool sptSet[V];

for (int i = 0; i < V; i++)

dist[i] = INT_MAX, sptSet[i] = false;

dist[src] = 0;

for (int count = 0; count < V - 1; count++) {

int u = minDistance(dist, sptSet);

sptSet[u] = true;

for (int v = 0; v < V; v++)

if (!sptSet[v] && graph[u][v]


&& dist[u] != INT_MAX

&& dist[u] + graph[u][v] < dist[v])

dist[v] = dist[u] + graph[u][v];

printSolution(dist);

int main()

int graph[V][V] = { { 0, 4, 0, 0, 0, 0, 0, 8, 0 },

{ 4, 0, 8, 0, 0, 0, 0, 11, 0 },

{ 0, 8, 0, 7, 0, 4, 0, 0, 2 },

{ 0, 0, 7, 0, 9, 14, 0, 0, 0 },

{ 0, 0, 0, 9, 0, 10, 0, 0, 0 },

{ 0, 0, 4, 14, 10, 0, 2, 0, 0 },

{ 0, 0, 0, 0, 0, 2, 0, 1, 6 },

{ 8, 11, 0, 0, 0, 0, 1, 0, 7 },

{ 0, 0, 2, 0, 0, 0, 6, 7, 0 } };

dijkstra(graph, 0);

return 0;

}
OUTPUT:

RESULT:

You might also like