You are on page 1of 12

//Binary Search Tree Program #include <iostream> #include <cstdlib> using namespace std; class BinarySearchTree { private: struct

tree_node { tree_node* left; tree_node* right; int data; }; tree_node* root; public: BinarySearchTree() { root = NULL; } bool void void void void void void void void }; // Smaller elements go left // larger elements go right void BinarySearchTree::insert(int d) { tree_node* t = new tree_node; tree_node* parent; t->data = d; t->left = NULL; t->right = NULL; parent = NULL; // is this a new tree? if(isEmpty()) root = t; else { //Note: ALL insertions are as leaf nodes tree_node* curr; curr = root; // Find the Node's parent while(curr) { parent = curr; if(t->data > curr->data) curr = curr->right; isEmpty() const { return root==NULL; } print_inorder(); inorder(tree_node*); print_preorder(); preorder(tree_node*); print_postorder(); postorder(tree_node*); insert(int); remove(int);

else curr = curr->left; } if(t->data < parent->data) parent->left = t; else parent->right = t; } } void BinarySearchTree::remove(int d) { //Locate the element bool found = false; if(isEmpty()) { cout<<" This Tree is empty! "<<endl; return; } tree_node* curr; tree_node* parent; curr = root; while(curr != NULL) { if(curr->data == d) { found = true; break; } else { parent = curr; if(d>curr->data) curr = curr->right; else curr = curr->left; } } if(!found) { cout<<" Data not found! "<<endl; return; } // 3 cases // 1. We're removing a // 2. We're removing a // 3. we're removing a : leaf node node with a single child node with 2 children

// Node with single child if((curr->left == NULL && curr->right != NULL)|| (curr->left != NULL && curr->right == NULL)) { if(curr->left == NULL && curr->right != NULL) { if(parent->left == curr)

{ parent->left = curr->right; delete curr; } else { parent->right = curr->right; delete curr; } } else // left child present, no right child { if(parent->left == curr) { parent->left = curr->left; delete curr; } else { parent->right = curr->left; delete curr; } } return; } //We're looking at a leaf node if( curr->left == NULL && curr->right == NULL) { if(parent->left == curr) parent->left = NULL; else parent->right = NULL; delete curr; return; } //Node with 2 children // replace node with smallest value in right subtree if (curr->left != NULL && curr->right != NULL) { tree_node* chkr; chkr = curr->right; if((chkr->left == NULL) && (chkr->right == NULL)) { curr = chkr; delete chkr; curr->right = NULL; } else // right child has children { //if the node's right child has a left child // Move all the way down left to locate smallest element if((curr->right)->left != NULL) { tree_node* lcurr; tree_node* lcurrp;

lcurrp = curr->right; lcurr = (curr->right)->left; while(lcurr->left != NULL) { lcurrp = lcurr; lcurr = lcurr->left; } curr->data = lcurr->data; delete lcurr; lcurrp->left = NULL; } else { tree_node* tmp; tmp = curr->right; curr->data = tmp->data; curr->right = tmp->right; delete tmp; } } return; } } void BinarySearchTree::print_inorder() { inorder(root); } void BinarySearchTree::inorder(tree_node* p) { if(p != NULL) { if(p->left) inorder(p->left); cout<<" "<<p->data<<" "; if(p->right) inorder(p->right); } else return; } void BinarySearchTree::print_preorder() { preorder(root); } void BinarySearchTree::preorder(tree_node* p) { if(p != NULL) { cout<<" "<<p->data<<" "; if(p->left) preorder(p->left); if(p->right) preorder(p->right); } else return; }

void BinarySearchTree::print_postorder() { postorder(root); } void BinarySearchTree::postorder(tree_node* p) { if(p != NULL) { if(p->left) postorder(p->left); if(p->right) postorder(p->right); cout<<" "<<p->data<<" "; } else return; } int main() { BinarySearchTree b; int ch,tmp,tmp1; while(1) { cout<<endl<<endl; cout<<" Binary Search Tree Operations "<<endl; cout<<" ----------------------------- "<<endl; cout<<" 1. Insertion/Creation "<<endl; cout<<" 2. In-Order Traversal "<<endl; cout<<" 3. Pre-Order Traversal "<<endl; cout<<" 4. Post-Order Traversal "<<endl; cout<<" 5. Removal "<<endl; cout<<" 6. Exit "<<endl; cout<<" Enter your choice : "; cin>>ch; switch(ch) { case 1 : cout<<" Enter Number to be inserted : "; cin>>tmp; b.insert(tmp); break; case 2 : cout<<endl; cout<<" In-Order Traversal "<<endl; cout<<" -------------------"<<endl; b.print_inorder(); break; case 3 : cout<<endl; cout<<" Pre-Order Traversal "<<endl; cout<<" -------------------"<<endl; b.print_preorder(); break; case 4 : cout<<endl; cout<<" Post-Order Traversal "<<endl; cout<<" --------------------"<<endl; b.print_postorder(); break; case 5 : cout<<" Enter data to be deleted : "; cin>>tmp1;

b.remove(tmp1); break; case 6 : return 0; } } } /***************************************************************** * File : prim.cpp * Purpose : Prims algorithm implementation to find the edges and cost of the * minimum spanning tree. * Author : Rakesh R and Nanda Kishor K N * Mail Id : knnkishor@yahoo.com, nandakishorkn@rediffmail.com * Website : www.c4swimmers.net ( WEB MASTER ) * Group : c4swimmers@yahoogroups.com ( GROUP OWNER ) * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License as * published by the Free Software Foundation; either version 2 of * the License,or (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. * *****************************************************************/ #include<iostream.h> #define MAX 10 class prims

{
private : int cost[MAX][MAX], tree[MAX][MAX]; int n; public : void readmatrix(); int spanningtree(int); void display(int);

};
void prims :: readmatrix()

{
int i, j; cout << "\nEnter the number of vertices in the Graph : "; cin >> n; cout << "\nEnter the Cost matrix of the Graph\n\n"; for (i = 1; i <= n; i++)

for (j = 1; j <= n; j++) cin >> cost[i][j];

}
int prims :: spanningtree(int src)

{
int visited[MAX], d[MAX], parent[MAX]; int i, j, k, min, u, v, stcost; for (i = 1; i <= n; i++)

{
d[i] = cost[src][i]; visited[i] = 0; parent[i] = src;

}
visited[src] = 1; stcost = 0; k = 1; for (i = 1; i < n; i++)

{
min = 999; for (j = 1; j <= n; j++)

{
if (!visited[j] && d[j] < min)

{
min = d[j]; u = j;

} }
visited[u] = 1; stcost = stcost + d[u]; tree[k][1] = parent[u]; tree[k++][2] = u; for (v = 1; v <= n; v++) if (!visited[v] && (cost[u][v] < d[v]))

{
d[v] = cost[u][v]; parent[v] = u;

} }
return (stcost);

}
void prims :: display(int cost)

{
int i; cout << "\nThe Edges of the Mininum Spanning Tree are\n\n"; for (i = 1; i < n; i++) cout << tree[i][1] << " " << tree[i][2] << endl;

cout << "\nThe Total cost of the Minimum Spanning Tree is : " << cost;

}
int main()

{
int source, treecost; prims pri; pri.readmatrix(); cout << "\nEnter the Source : "; cin >> source; treecost = pri.spanningtree(source); pri.display(treecost); return 0;

Prim Algo Implementation


#include<iostream> #include<conio.h> #include<stdlib.h> using namespace std; int cost[10][10],i,j,k,n,stk[10],top,v,visit[10],visited[10],u; main() { int m,c; cout <<"enterno of vertices"; cin >> n; cout <<"ente no of edges"; cin >> m; cout <<"\nEDGES Cost\n"; for(k=1;k<=m;k++) { cin >>i>>j>>c; cost[i][j]=c; } for(i=1;i<=n;i++) for(j=1;j<=n;j++) if(cost[i][j]==0) cost[i][j]=31999; cout <<"ORDER OF VISITED VERTICES"; k=1; while(k<n) { m=31999; if(k==1) { for(i=1;i<=n;i++) for(j=1;j<=m;j++) if(cost[i][j]<m) { m=cost[i][j]; u=i; } }

else { for(j=n;j>=1;j--) if(cost[v][j]<m && visited[j]!=1 && visit[j]!=1) { visit[j]=1; stk[top]=j; top++; m=cost[v][j]; u=j; } } cost[v][u]=31999; v=u; cout<<v << " "; k++; visit[v]=0; visited[v]=1; } }

OUTPUT enterno of vertices7 ente no of edges9 EDGES Cost 1 6 10 6 5 25 5 4 22 4 3 12 3 2 16 2 7 14 5 7 24 4 7 18 1 2 28 ORDER OF VISITED VERTICES1 6 5 4 3 2
/*****************************************************************

File

: kruskal.cpp

* Purpose : Kruskal's algorithm implementation to find the edges and cost of the * minimal spanning tree. * Author : Rakesh R and Nanda Kishor K N * Mail Id : knnkishor@yahoo.com, nandakishorkn@rediffmail.com * Website : www.c4swimmers.net ( WEB MASTER ) * Group : c4swimmers@yahoogroups.com ( GROUP OWNER ) * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License as * published by the Free Software Foundation; either version 2 of * the License,or (at your option) any later version. *

* This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. * *****************************************************************/ #include<iostream.h> #define MAX 100 class kruskal

{
private : struct edge_info

{
int u, v, weight; } edge[MAX]; int tree[MAX][2], set[MAX]; int n; public : int readedges(); void makeset(); int find(int); void join(int, int); void arrange_edges(int); int spanningtree(int); void display(int);

};
int kruskal :: readedges()

{
int i, j, k, cost; k = 1; cout << "\nEnter the number of Vertices in the Graph : "; cin >> n; cout << endl; for (i = 1; i <= n; i++) for (j = 1; j < i; j++)

{
cout << "weight[" << i << "][" << j << "] : "; cin >> cost; if (cost != 999)

{
edge[k].u = i; edge[k].v = j; edge[k++].weight = cost;

} } return (k - 1); }
void kruskal :: makeset()

{
int i; for (i = 1; i <= n; i++) set[i] = i;

}
int kruskal :: find(int vertex)

{
return (set[vertex]);

}
void kruskal :: join(int v1, int v2)

{
int i, j; if (v1 < v2) set[v2] = v1; else set[v1] = v2;

}
void kruskal :: arrange_edges(int k)

{
int i, j; struct edge_info temp; for (i = 1; i < k; i++) for (j = 1; j <= k - i; j++) if (edge[j].weight > edge[j + 1].weight)

{
temp = edge[j]; edge[j] = edge[j + 1]; edge[j + 1] = temp;

} }
int kruskal :: spanningtree(int k)

{
int i, t, sum; arrange_edges(k); t = 1; sum = 0; for (i=1;i<=k;i++) cout<<edge[i].u<<edge[i].v<<" "<<edge[i].weight<<endl;getch();

for (i = 1; i <= k; i++) if (find (edge[i].u) != find (edge[i].v))

{
tree[t][1] = edge[i].u; tree[t][2] = edge[i].v; sum += edge[i].weight; join (edge[t].u, edge[t].v); t++;

}
return sum;

}
void kruskal :: display(int cost)

{
int i; cout << "\nThe Edges of the Minimum Spanning Tree are\n\n"; for (i = 1; i < n; i++) cout << tree[i][1] << " - " << tree[i][2] << endl; cout << "\nThe Cost of the Minimum Spanning Tree is : " << cost;

}
int main()

{
int ecount, totalcost; kruskal k; ecount = k.readedges(); k.makeset(); totalcost = k.spanningtree(ecount); k.display(totalcost); return 0;

You might also like