Ads All Codes
Ads All Codes
#include<iostream>
class graph{
int cost[10][10];
int tcities;
public:
graph();
void create();
void display();
int prims(int);
};
graph::graph(){
tcities = 0;
cin>>tcities;
cost[i][j]=999;
void graph::create(){
char ch;
cin>>ch;
if(ch == 'y'){
cin>>cost[i][j];
cost[j][i] = cost[i][j];
void graph::display(){
cout<<cost[i][j]<<" ";
cout<<"\n";
nearest[v] = -1;
if(i != v)
nearest[i] = v;
j = k;
min = cost[k][nearest[k]];
cout<<"\n"<<j<<" "<<cost[j][nearest[j]];
nearest[j] = -1;
nearest[k] = j;
return mincost;
int main(){
graph a;
int v,b;
a.create();
a.display();
cin>>v;
b = a.prims(v);
class tbt_node{
char data;
bool rbit;
bool lbit;
tbt_node *rightc;
tbt_node *leftc;
public:
tbt_node();
};
tbt_node::tbt_node(){
lbit=0;
rbit=0;
class tbt {
tbt_node *head;
public:
void create();
void preorder();
void inorder();
tbt();
};
tbt::tbt() {
head->leftc= head;
head->rightc = head;
void tbt::create()
char ch;
cout<<"Enter data"<<endl;
cin>>root->data;
head->lbit=1;
root->leftc=head;
root->rightc =head;
head->leftc=root;
do
int flag=0;
char ch1;
tbt_node *temp=root;
cout<<"Enter data"<<endl;
cin>>curr->data;
while(flag==0)
cin>>ch1;
if (ch1=='l')
{
if(temp->lbit==0)
curr->rightc=temp;
curr->leftc=temp->leftc;
temp->leftc=curr;
temp->lbit=1;
flag=1;
else
temp=temp->leftc;
if(ch1=='r') {
if(temp->rbit==0)
curr
->leftc=temp;
curr->rightc=temp->rightc;
temp->rightc=curr;
temp->rbit=1;
flag=1;
else
temp=temp->rightc;
cin>>ch;
}while(ch=='a');
tbt_node *x=temp->rightc;
if(temp->rbit==1){
while(x->lbit==1)
x=x->leftc;
return x;
void tbt::inorder(){
tbt_node *temp=head;
while(1){
temp=insuccr(temp);
void tbt::preorder(){
tbt_node *temp=head->leftc;
while(temp!=head)
cout<<temp->data<<" ";
while(temp->lbit != 0)
temp=temp->leftc;
cout<<temp->data<<" ";
while(temp->rbit==0)
temp=temp->rightc;
temp=temp->rightc;
}
int main()
tbt t;
t.create();
cout<<endl;
t.preorder();
cout<<endl;
t.inorder();
cout<<endl;
#include <iostream>
#include <stack>
class treenode{
char data[10];
treenode *left;
treenode *right;
};
class tree{
treenode *root;
public:
tree();
void create();
void create(treenode*);
void inorder();
void inorder(treenode*);
void r_inorder(treenode*);
void preorder();
void preorder(treenode*);
void r_preorder(treenode*);
void postorder();
void postorder(treenode*);
void r_postorder(treenode*);
};
tree::tree(){
root = NULL;
void tree::create(){
create(root);
char ch;
int a;
do{
int flag = 0;
temp = root;
while(flag == 0){
cout<<"Enter l to enter data to left or r to enter data to right of "<< temp->data <<endl;
if(ch == 'l'){
if(temp->left == NULL){
temp->left = curr;
flag = 1;
temp = temp->left;
else {
if(temp->right == NULL){
temp->right = curr;
flag = 1;
temp = temp->right;
cin >> a;
}while(a!=0);
void tree::inorder(){
inorder(root);
cout<<endl;
cout<<"Non Recursive"<<endl;
r_inorder(root);
if(temp != NULL){
inorder(temp->left);
cout<<temp->data<<" , ";
inorder(temp->right);
stack<treenode*> s;
while(1) {
while(temp != NULL){
s.push(temp) ;
if (s.empty())
break;
temp = s.top();
s.pop();
cout<<temp->data<<",";
void tree::preorder(){
preorder(root);
cout<<endl;
cout<<"Non Recursive"<<endl;
r_preorder(root);
if(temp != NULL){
cout<<temp->data<<" , ";
preorder(temp->left);
preorder(temp->right);
stack<treenode*> s;
while(1) {
while(temp != NULL){
cout<<temp->data<<",";
s.push(temp) ;
if (s.empty())
break;
temp = s.top();
s.pop();
void tree::postorder(){
postorder(root);
cout<<endl;
cout<<"Non Recursive"<<endl;
r_postorder(root);
if(temp != NULL){
postorder(temp->left);
postorder(temp->right);
cout<<temp->data<<" , ";
stack<treenode*> s;
while(1) {
while(temp != NULL){
s.push(temp) ;
if (s.empty())
break;
if (s.top()->right == NULL) {
temp = s.top();
s.pop();
temp = s.top();
s.pop();
}
temp = s.top()->right;
int main(){
tree a;
a.create();
cout<<"Inorder traversal"<<endl;
a.inorder();
cout<<endl;
cout<<"Preorder traversal"<<endl;
a.preorder();
cout<<endl;
cout<<"Postorder traversal"<<endl;
a.postorder();
HEAP SORT :
#include<iostream>
class heap{
int a[20];
public:
int num;
int add();
void insert(int*,int);
void display(int*,int);
void Delete(int);
void deleteheap(int*,int);
void adjust(int*,int,int);
void Sort(int);
void sort(int*,int);
void deleteh(int*,int);
void Select(int);
void select(int*,int);
};
int heap::add(){
int j=0,element,c=0;
char ch;
do{
cin>>element;
a[j]=element;
insert(a,j+1);
j++;
c++;
cin>>ch;
}while(ch!='n');
display(a,c);
return c;
int i=n,element=a[n-1];
if(i!=1){
while((i>1)&&(a[(i/2)-1]<element)){
a[i-1]=a[(i/2)-1];
i=(i/2);
}
a[i-1]=element;
for(int i=0;i<n;i++){
cout<<endl;
cout<<a[i];
deleteheap(a,c);
char ch;
do{
int t=a[0];
a[0]=a[n-1];
a[n-1]=t;
n--;
cin>>ch;
}while(ch=='y');
num = n;
display(a,n);
}
void heap::adjust(int a[20],int n,int i){
int j;
while(2*i+1<=n){
j=2*i+1;
j=j+1;
int temp=a[i];
a[i]=a[j];
a[j]=temp;
i=j;
else{
int temp=a[i];
a[i]=a[j];
a[j]=temp;
i=j;
i=j;
sort(a,n);
display(a,n);
while(n>0){
int t=a[0];
a[0]=a[n-1];
a[n-1]=t;
n--;
adjust(a,n-1,0);
int t=a[0];
a[0]=a[n-1];
a[n-1]=t;
n--;
num = n;
select(a,n);
int b;
cin>>b;
for(int i=0;i<b;i++){
deleteh(a,n);
}
cout<<"\nThe selected element is : "<<a[0];
int main(){
heap h;
int c=h.add();
h.Delete(c);
h.Sort(h.num);
h.Select(h.num);
#include<iostream>
class gnode{
int vertex;
};
class stack {
int st[20];
int top;
public:
stack() {
top = -1;
}
void push(int);
int pop();
int isEmpty();
};
top++;
st[top] = temp;
int stack::pop() {
int t;
t = st[top];
top--;
return t;
int stack::isEmpty() {
if (top == -1) {
return 1;
else
return 0;
class queue{
int str[20];
int front;
int rear;
public:
queue(){
front = -1;
rear = -1;
void enqueue(int);
int dequeue();
int isempty();
};
if (front == -1){
front = 0;
rear++;
str[rear] = temp;
int queue::dequeue(){
front++;
return temp;
int queue::isempty(){
return -1;
class graph{
public:
gnode *head[20];
int n;
int visited[10];
graph(){
cin>>n;
head[i]->vertex = i;
void create();
void traversal();
void dfs(int);
void ndfs(int);
void bfs(int);
};
void graph::create(){
int ans;
do{
int v;
cin>>v;
if(v==i)
else{
temp->next=curr;
temp=temp->next;
cin>>ans;
}while(ans==1);
void graph::traversal(){
int v;
visited[i]=0;
cin>>v;
dfs(v);
ndfs(v);
cout<<endl<<"BFS : ";
bfs(v);
visited[v]=1;
while(temp->next != NULL){
int w = temp->next->vertex;
if(!visited[w])
dfs(w);
temp = temp->next;
int t;
stack s;
visited[i]=0;
s.push(v);
visited[v]=1;
do{
t = s.pop();
while(temp->next != NULL){
int w = temp->next->vertex;
if(!visited[w]){
s.push(w);
visited[w]=1;
temp = temp->next;
}while(s.isEmpty()!=1);
int t;
queue q;
for(int i=0;i<n;i++)
visited[i]=0;
q.enqueue(v);
visited[v]=1;
while(q.isempty() != -1){
t = q.dequeue();
while(temp->next != NULL){
int w = temp->next->vertex;
if(!visited[w]){
q.enqueue(w);
visited[w]=1;
temp = temp->next;
int main(){
graph a;
a.create();
cout<<endl;
a.traversal();
TREE ALL :
#include <iostream>
#include <cstring>
#include <stack>
using namespace std;
class tree_node
char key[20];
char value[50];
tree_node *left;
tree_node *right;
};
class queue{
tree_node* st[20];
int front;
int rear;
public:
queue()
front = -1;
rear = -1;
void enqueue(tree_node*);
tree_node* dequeue();
int isempty();
};
if (front == -1)
{
front = 0;
rear++;
st[rear] = temp;
tree_node* queue::dequeue()
front++;
return temp;
int queue::isempty()
return -1;
class bi_tree
public:
tree_node *root;
bi_tree();
void create();
void inorder(tree_node*);
void BFS(tree_node*);
tree_node* copy(tree_node*);
void mirror(tree_node*);
};
bi_tree::bi_tree()
root = NULL;
void bi_tree::create()
root->left = NULL;
root->right = NULL;
int a;
do{
int flag = 0;
while(flag == 0){
if(temp->left == NULL){
temp->left = curr;
flag = 1;
temp = temp->left;
if(temp->right == NULL){
temp->right = curr;
flag = 1;
temp = temp->right;
cin >> a;
while(a!=0);
stack<tree_node*> s;
while(1)
while(temp != NULL)
s.push(temp) ;
if (s.empty())
break;
temp = s.top();
s.pop();
temp = temp->right;
temp=root;
queue q;
q.enqueue(temp);
while(q.isempty() != -1)
temp = q.dequeue();
if(temp->left != NULL)
q.enqueue(temp->left);
if(temp->right != NULL)
q.enqueue(temp->right);
if(root == NULL){
return NULL;
}
strcpy(temp->key,root->key);
strcpy(temp->value,root->value);
temp->left = copy(root->left);
temp->right = copy(root->right);
return temp;
if(root == NULL)
return;
else
tree_node *temp;
mirror(root->left);
mirror(root->right);
temp = root->left;
root->left = root->right;
root->right = temp;
int main()
bi_tree a;
a.create();
cout<<"Inorder traversal"<<endl;
cout<<"Non Recursive"<<endl;
a.inorder(a.root);
cout<<endl;
a.BFS(a.root);
cout<<endl;
cout<<"Copied tree"<<endl;
a.inorder(newnode);
cout<<endl;
cout<<"Mirrored Tree"<<endl;
a.mirror(a.root);
a.inorder(a.root);
HASHING :
#include<iostream>
#include<fstream>
#include<string.h>
class employee
int id;
char name[20];
int sal;
};
class Hashing{
public:
Hashing(){
int i = 0;
employee e;
file.seekp(0, ios::end);
e.id = -1;
strcpy(e.name, "----");
e.sal = -1;
file.write((char*)&e, sizeof(e));
file.close();
void display_hashtable();
void linearprowithoutrep();
void linearprobingwithrep();
};
void Hashing::linearprobingwithrep(){
employee e;
char ch;
do{
fflush(stdin);
file.seekp(0, ios::beg);
loc = e.id%10;
employee temp;
file.read((char*)&temp, sizeof(temp));
if(temp.id == -1){
file.write((char*)&e, sizeof(e));
else{
newloc = temp.id%10;
if(newloc == loc){
file.read((char*)&temp, sizeof(temp));
if(temp.id == -1){
file.write((char*)&e, sizeof(e));
break;
else{
file.seekp(loc*sizeof(e), ios::beg);
file.write((char*)&e, sizeof(e));
file.seekg(i*sizeof(e), ios::beg);
file.read((char*)&temp, sizeof(temp));
if(temp.id == -1){
file.seekp(i*(sizeof(e)), ios::beg);
file.write((char*)&temp1, sizeof(temp1));
break;
if(i == loc){
file.flush();
display_hashtable();
}while(ch == 'y');
void Hashing::linearprowithoutrep(){
employee e;
char ch;
do{
fflush(stdin);
file.seekp(0, ios::beg);
loc = e.id%10;
employee temp;
file.read((char*)&temp, sizeof(temp));
if(temp.id == -1){
file.write((char*)&e, sizeof(e));
else{
file.read((char*)&temp, sizeof(temp));
if(temp.id == -1){
file.write((char*)&e, sizeof(e));
break;
if(i == loc){
file.flush();
display_hashtable();
}while(ch == 'y');
void Hashing::display_hashtable(){
int i;
employee a;
while(file.read((char*)&a, sizeof(a))){
file.close();
int main(){
Hashing h;
// h.display_hashtable();
// h.linearprowithoutrep();
h.display_hashtable();
h.linearprobingwithrep();
return 0;
AVL TREE :
#include<iostream>
using namespace std;
class avl{
int key;
avl *right;
avl *left;
int height;
};
class atree{
public:
avl* root;
atree();
int height(avl*);
int max(int,int);
avl* rightrotate(avl*);
avl* leftrotate(avl*);
avl* insert(avl*,int);
avl* balance(avl*,int);
void inorder(avl*);
void preorder(avl*);
void postorder(avl*);
};
atree::atree(){
root = NULL;
return 0;
return node->height;
return (a>b)?a:b;
avl* x = node->left;
avl* y = x->right;
x->right = node;
node->left = y;
node->height = max(height(node->left),height(node->right))+1;
x->height = max(height(x->left),height(x->right))+1;
return x;
avl* x = node->right;
avl* y = x->left;
x->left = node;
node->right = y;
node->height = max(height(node->left),height(node->right))+1;
x->height = max(height(x->left),height(x->right))+1;
return x;
if(node == NULL)
return 0;
return (height(node->left)-height(node->right));
if(node == NULL){
temp->key = key;
temp->left = NULL;
temp->right = NULL;
temp->height = 1;
return temp;
node->left = insert(node->left,key);
node->right = insert(node->right,key);
node->height = 1+max(height(node->left),height(node->right));
return node;
return rightrotate(node);
return leftrotate(node);
}
if(balance > 1 && key > node->left->key){
node->left = leftrotate(node->left);
return rightrotate(node);
node->right = rightrotate(node->right);
return leftrotate(node);
return node;
if(node != NULL){
inorder(node->left);
cout<<node->key<<" ";
inorder(node->right);
if(node!= NULL){
cout<<node->key<<" ";
preorder(node->left);
preorder(node->right);
if(node != NULL){
postorder(node->left);
postorder(node->right);
cout<<node->key<<" ";
int main(){
atree a;
int ch;
do{
int key;
cin>>key;
a.root = a.insert(a.root,key);
a.root = a.balance(a.root,key);
cin>>ch;
}while(ch!=0);
a.inorder(a.root);
a.preorder(a.root);
a.postorder(a.root);