You are on page 1of 14

5. Construct a C++ program to evaluate the postfix expression using stack.

#include<iostream.h>
using namespace std;
#define SIZE 50
int s[SIZE];
int top= -1;
void push(int elem)
{
s[++top]=elem;
}
int pop()
{
return(s[top--]);
}
int main()
{
char pofx[50],ch;
int i=0,op1,op2;
cout<<"\n\nRead the Postfix Expression:";
cin>>pofx;
while((ch=pofx[i++])!='\0')
{
if(isdigit(ch))
push(ch-'0');
else
{
op2=pop();
op1=pop();
switch(ch)
{
case '+':
push(op1+op2);break;
case '-':
push(op1-op2);break;
case '*':
push(op1*op2);break;
case '/':
push(op1/op2);break;
}
}
}
cout<<"\n Given Postfix Expn:"<<pofx;
cout<<"\n Result after Evaluation:"<<s[top];
return 0;
}
OUTPUT
Read the Postfix Expression: 32+51-*
Given Postfix Expn: 32+51-*
Result after Evaluation: 20

6. Develop a C++ program for traversing a binary tree in preorder, inorder and postorder using
recursive function.

#include<iostream>
using namespace std;
struct btree
{
struct btree *left;
struct btree *right;
int no;
};
void postorder(struct btree *trav);
void inorder(struct btree *trav);
void preorder(struct btree *trav);
struct btree * create(struct btree *trav);
int main()
{
struct btree *root=NULL;
char c;

while(1)
{
root=create(root);
cout<<"Do you want to continue : ";
cin>>c;
if(c=='n' ||c=='N')
break;
}
cout<<endl<<"Inoder is : ";inorder(root);
cout<<endl<<"Preorder is : ";preorder(root);
cout<<endl<<"Postorder is : ";postorder(root);
return 0;
}
struct btree * create(struct btree *trav)
{
if(trav==NULL)
{
trav=new btree;
trav->right=NULL;
trav->left=NULL;
cout<<"Enter the no : ";
cin>>trav->no;
return(trav);
}
char choice;
cout<<"Enter the left or right child : ";
cin>>choice;
if(choice == 'r' || choice == 'R')
{
trav->right=create(trav->right);
}
if(choice=='l' || choice=='L')
{
trav->left=create(trav->left);
}
return(trav);
}
void inorder(struct btree *trav)
{
if(trav==NULL)
return ;
inorder(trav->left);
cout<<" "<<trav->no;
inorder(trav->right);
}
void preorder(struct btree *trav)
{
if(trav==NULL)
return;
cout<<" "<<trav->no;
preorder(trav->left);
preorder(trav->right);
}
void postorder(struct btree *trav)
{
if(trav==NULL)
return;
postorder(trav->left);
postorder(trav->right);
cout<<" "<<trav->no;
}
OUTPUT
Enter the no : 5
Do you want to continue : y
Enter the left or right child : L L R
Enter the no : 4
Do you want to continue : Y
Enter the left or right child : R
Enter the no : 6
Do you want to continue : N

Inoder is : 4 5 6
Preorder is : 5 4 6
Postorder is : 4 6 5

7. Create a Binary Search Tree to implement the insert, search and delete operations using recursive
function.

# include<iostream>
using namespace std;
// Node Declaration
struct node
{
int info;
struct node *left;
struct node *right;
}*root;
// Class Declaration
class BST
{
public:
void find(int, node **, node **);
void insert(node *,node *) ;
void del(int);
void case_a(node *,node *);
void case_b(node *,node *);
void case_c(node *,node *);
void preorder(node *);
void inorder(node *);
void postorder(node *);
void display(node *, int);
BST()
{
root = NULL;
}
};
// Main Contains Menu
int main()
{
int choice, num;
BST bst;
node *temp;
while (1)
{
cout<<"-----------------"<<endl;
cout<<"Operations on BST"<<endl;
cout<<"-----------------"<<endl;
cout<<"1.Insert Element "<<endl;
cout<<"2.Delete Element "<<endl;
cout<<"3.Inorder Traversal"<<endl;
cout<<"4.Preorder Traversal"<<endl;
cout<<"5.Postorder Traversal"<<endl;
cout<<"6.Display"<<endl;
cout<<"7.Quit"<<endl;
cout<<"Enter your choice : ";
cin>>choice;
switch(choice)
{
case 1:
temp = new node;
cout<<"Enter the number to be inserted : ";
cin>>temp->info;
bst.insert(root, temp);
break;
case 2:
if (root == NULL)
{
cout<<"Tree is empty, nothing to delete"<<endl;
continue;
}
cout<<"Enter the number to be deleted : ";
cin>>num;
bst.del(num);
break;
case 3:
cout<<"Inorder Traversal of BST:"<<endl;
bst.inorder(root);
cout<<endl;
break;
case 4:
cout<<"Preorder Traversal of BST:"<<endl;
bst.preorder(root);
cout<<endl;
break;
case 5:
cout<<"Postorder Traversal of BST:"<<endl;
bst.postorder(root);
cout<<endl;
break;
case 6:
cout<<"Display BST:"<<endl;
bst.display(root,1);
cout<<endl;
break;
case 7:
exit(1);
default:
cout<<"Wrong choice"<<endl;
}
}
return 0;
}

// Find Element in the Tree


void BST::find(int item, node **par, node **loc)
{
node *ptr, *ptrsave;
if (root == NULL)
{
*loc = NULL;
*par = NULL;
return;
}
if (item == root->info)
{
*loc = root;
*par = NULL;
return;
}
if (item < root->info)
ptr = root->left;
else
ptr = root->right;
ptrsave = root;
while (ptr != NULL)
{
if (item == ptr->info)
{
*loc = ptr;
*par = ptrsave;
return;
}
ptrsave = ptr;
if (item < ptr->info)
ptr = ptr->left;
else
ptr = ptr->right;
}
*loc = NULL;
*par = ptrsave;
}
// Inserting Element into the Tree
void BST::insert(node *tree, node *newnode)
{
if (root == NULL)
{
root = new node;
root->info = newnode->info;
root->left = NULL;
root->right = 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)
{
if (tree->left != NULL)
{
insert(tree->left, newnode);
}
else
{
tree->left = newnode;
(tree->left)->left = NULL;
(tree->left)->right = NULL;
cout<<"Node Added To Left"<<endl;
return;
}
}
else
{
if (tree->right != NULL)
{
insert(tree->right, newnode);
}
else
{
tree->right = newnode;
(tree->right)->left = NULL;
(tree->right)->right = NULL;
cout<<"Node Added To Right"<<endl;
return;
}
}
}
// Delete Element from the tree
void BST::del(int item)
{
node *parent, *location;
if (root == NULL)
{
cout<<"Tree empty"<<endl;
return;
}
find(item, &parent, &location);
if (location == NULL)
{
cout<<"Item not present in tree"<<endl; return;
}
if (location->left == NULL && location->right == NULL)
case_a(parent, location);
if (location->left != NULL && location->right == NULL)
case_b(parent, location);
if (location->left == NULL && location->right != NULL)
case_b(parent, location);
if (location->left != NULL && location->right != NULL)
case_c(parent, location);
free(location);
}
// * Case A
void BST::case_a(node *par, node *loc )
{
if (par == NULL)
{
root = NULL;
}
else
{
if (loc == par->left)
par->left = NULL;
else
par->right = NULL;
}
}
// * Case B
void BST::case_b(node *par, node *loc)
{
node *child;
if (loc->left != NULL)
child = loc->left;
else
child = loc->right;
if (par == NULL)
{
root = child;
}
else
{
if (loc == par->left)
par->left = child;
else
par->right = child;
}
}
// * Case C
void BST::case_c(node *par, node *loc)
{
node *ptr, *ptrsave, *suc, *parsuc;
ptrsave = loc;
ptr = loc->right;
while (ptr->left != NULL)
{
ptrsave = ptr;
ptr = ptr->left;
}
suc = ptr;
parsuc = ptrsave;
if (suc->left == NULL && suc->right == NULL)
case_a(parsuc, suc);
else
case_b(parsuc, suc);
if (par == NULL)
{
root = suc;
}
else
{
if (loc == par->left)
par->left = suc;
else
par->right = suc;
}
suc->left = loc->left;
suc->right = loc->right;
}
// Pre Order Traversal
void BST::preorder(node *ptr)
{
if (root == NULL)
{
cout<<"Tree is empty"<<endl; return;
}
if (ptr != NULL)
{
cout<<ptr->info<<" ";
preorder(ptr->left);
preorder(ptr->right);
}
}
// In Order Traversal
void BST::inorder(node *ptr)
{
if (root == NULL)
{
cout<<"Tree is empty"<<endl; return;
}
if (ptr != NULL)
{
inorder(ptr->left);
cout<<ptr->info<<" "; inorder(ptr->right);
}
}
// Postorder Traversal
void BST::postorder(node *ptr)
{
if (root == NULL)
{
cout<<"Tree is empty"<<endl; return;
}
if (ptr != NULL)
{
postorder(ptr->left);
postorder(ptr->right);
cout<<ptr->info<<" ";
}
}
// Display Tree Structure
void BST::display(node *ptr, int level)
{
int i;
if (ptr != NULL)
{
display(ptr->right, level+1); cout<<endl;
if (ptr == root)
cout<<"Root->: ";
else
{
for (i = 0;i < level;i++)
cout<<" ";
}
cout<<ptr->info; display(ptr->left, level+1);
}
}
Operations on BST

1.Insert Element, 2.Delete Element, 3.Inorder Traversal


4.Preorder Traversal, 5.Postorder Traversal, 6.Display, 7.Quit
Enter your choice : 1
Enter the number to be inserted : 8
Root Node is Added

Enter your choice : 1


Enter the number to be inserted : 9
Node Added To Right

Enter your choice : 1


Enter the number to be inserted : 5
Node Added To Left
Enter your choice : 1
Enter the number to be inserted : 11
Node Added To Right

Enter your choice : 1


Enter the number to be inserted : 3
Node Added To Left

Enter your choice : 1


Enter the number to be inserted : 7
Node Added To Right

Enter your choice : 1


Enter the number to be inserted : 10
Node Added To Left

Enter your choice : 6


Display BST:
11
10
9
Root->: 8
7
5
3

Enter your choice : 2


Enter the number to be deleted : 10
Enter your choice : 6
Display BST:
11
9
Root->: 8
7
5
3

Enter your choice : 3


Inorder Traversal of BST:
3 5 7 8 9 11
Enter your choice : 4
Preorder Traversal of BST:
8 5 3 7 9 11

Enter your choice : 5


Postorder Traversal of BST:
3 7 5 11 9 8

8. Develop a C++ program to find shortest wire length between two components using Dijkstra’s
algorithm for a given circuit.
#include <climits>
#include <iostream>
using namespace std;
#define N 6
int minDist(int dist[], bool sptSet[])
{
int min = INT_MAX, min_index;
for (int v = 0; v < N; v++)
if (sptSet[v] == false && dist[v] <= min)
min = dist[v], min_index = v;
return min_index;
}
void displaySolution(int dist[], int n)
{
cout << "Component\tDistance from other component\n";
for (int i = 0; i < n; i++)
printf("%d\t\t%d\n", i, dist[i]);
}
void optimizeLength(int g[N][N], int src)
{
int dist[N];
bool sptSet[N];
for (int i = 0; i < N; i++)
dist[i] = INT_MAX, sptSet[i] = false;
dist[src] = 0;
//Find shortest path for all components.
for (int cnt = 0; cnt < N - 1; cnt++) {
//Pick the minimum distance component from the set of
//components not yet processed.
int u = minDist(dist, sptSet);
//Mark the picked component as processed.
sptSet[u] = true;
//Update dist value of the adjacent components of the picked component.
for (int v = 0; v < N; v++)
if (!sptSet[v] && g[u][v] && dist[u] != INT_MAX && dist[u] + g[u][v] < dist[v])
//Update dist[v] only if is not in sptSet, there is an edge from
//u to v, and total weight of path from src to v through u is
//smaller than current value of dist[v].
dist[v] = dist[u] + g[u][v];
}
displaySolution(dist, N);
}
int main()
{
int g[N][N] = { { 0, 0, 6, 7, 0, 4}, { 4, 0, 8, 0, 1, 2 },
{0, 9, 0, 2,0, 4 },{ 0, 0, 7, 0, 9, 5 }, { 0, 1, 0, 0, 6,7 }, { 6, 7, 0, 0, 2,3} };
cout << "Enter the starting component: ";
int s;
cin >> s;
optimizeLength(g, s);
return 0;
}
OUTPUT
Enter the starting component: 4
Component Distance from other component
0 5
1 1
2 9
3 11
4 0
5 3

9. Implement and test a C++ program to sort the given list of numbers using bubble sort.

#include<iostream>
using namespace std;
int main ()
{
   int i, j,temp,pass=0;
   int a[10] = {10,2,0,14,43,25,18,1,5,45};
   cout <<"Input list ...\n";
   for(i = 0; i<10; i++) {
      cout <<a[i]<<"\t";
   }
cout<<endl;
for(i = 0; i<10; i++) {
   for(j = i+1; j<10; j++)
   {
      if(a[j] < a[i]) {
         temp = a[i];
         a[i] = a[j];
         a[j] = temp;
      }
   }
pass++;
}
cout <<"Sorted Element List ...\n";
for(i = 0; i<10; i++) {
   cout <<a[i]<<"\t";
}
cout<<"\nNumber of passes taken to sort the list:"<<pass<<endl;
return 0;
}

OUTPUT
Input list …
10      2       0       14      43      25      18      1       5       45
Sorted Element List …
0       1       2       5       10      14      18      25      43      45
Number of passes taken to sort the list:10

10. Develop and test a C++ Program to check whether the given number is in the list using binary
search.

#include<iostream>
Using namespace std;
int main()
{
int a[20],i,num,low,high,mid,t,flag=0;
clrscr();
cout<<"\nEnter the size of array:";
cin>>num;
cout<<"\nEnter the elements in ascending order:";
for(i=1;i<=num;i++)
cin>>a[i];
cout<<"\nEnter the elements to be searched:";
cin>>t;
low=1;
high=num;
while(low<=high)
{
mid=(low+high)/2;
if(t<a[mid])
high=mid-1;
else if(t>a[mid])
low=mid+1;
else
{
cout<<"Element " <<t<<" is in position "<<mid;
flag=1;
break;
}
}
if(flag==0)
cout<<"\nElement not found";
return 0;
}
OUTPUT
Enter the size of array: 6
Enter the elements in ascending order:
10 20 30 40 50 60
Enter the elements to be searched: 50
Element 50 is at position 5

You might also like