Data Structures Record
Data Structures Record
Nadergul, Hyderabad
CERTIFICATE
Department of COMPUTER SCIENCE & ENGINEERING
course of Data Structures Lab prescribed by Osmania University for BE 2/4 Sem-1 of
q->next=first;
first=q;
}
}
template<class t>
void chain<t>::insertatend(t x)
{
node<t>*q=new node<t>(x);
node<t>*cur=first;
while(cur->next)
cur=cur->next;
cur->next=q;
if(first==NULL)
first=q;
}
template<class t>
void chain<t>::insertatpos(t x,int p)
{
int i;
node<t>*q=new node<t>(x);
node<t>*cur=first;
if(p==1)
{
q->next=first;
first=q;
}
else
{
for(i=1;i<p-1&&cur->next;i++)
cur=cur->next;
if(i==p-1)
{
q->next=cur->next;
cur->next=q;
}
else
{
cout<<"invalid position,cant insert\n";
return;
}
}
}
template<class t>
void chain<t>::display()
{
if(first)
{
for(node<t>*cur=first;cur;cur=cur->next)
cout<<cur->data<<"\t";
cout<<endl;
}
else
{
cout<<"chain empty\n";
}
}
template<class t>
t chain<t>::delfrombeg()
{
if(first)
{
node<t>*cur=first;
t x=cur->data;
first=first->next;
delete cur;
return x;
}
else
{
cout<<"empty chain,cant delete\n";
return(-1);
}
}
template<class t>
t chain<t>::delfromend()
{
if(first)
{
node<t>*prev,*cur;
t x;
for(cur=first;cur->next;cur=cur->next)
prev=cur;
if(first->next==NULL)
first=NULL;
else
prev->next=NULL;
x=cur->data;
delete cur;
return x;
}
else
{
cout<<"empty chain,cant delete\n";
return(-1);
}
}
template<class t>
t chain<t>::delfrompos(int p)
{
if(first)
{
node<t>*prev,*cur;
t x;
if(p==1)
{
cur=first;
first=first->next;
}
else
{
int i=1;
for(cur=first;i<p&&cur;cur=cur->next,i++)
prev=cur;
if(cur)
prev->next=cur->next;
else
{
cout<<"invalid position,cant delete\n";
return(-1);
}
}
x=cur->data;
delete cur;
return x;
}
cout<<"chain empty,cant delete\n";
return(-1);
}
template<class t>
chain<t>::~chain()
{
for(node<t>*cur=first;cur;cur=cur->next)
{
first=first->next;
delete cur;
cur=first;
}
}
template<class t>
int chain<t>::find(t d)
{
node<t> *q=first;
if(q!=NULL)
{
for(int i=1;i<d&&q;i++)
q=q->next;
if(q)
{
t p=q->data;
return(p);
}
}
cout<<"invalid positon\n";
return(-1);
}
template<class t>
int chain<t>::search(t d)
{
int c=0;
node<t>*q=first;
while(q!=NULL)
{
c++;
if(q->data==d)
return (c);
else
q=q->next;
}
cout<<"no such data\n";
return(-1);
}
template<class t>
int chain<t>::count()
{
int c=0;
if(first)
{
for(node<t>*cur=first;cur;cur=cur->next)
c++;
}
return c;
}
template<class t>
void chain<t>::reverse()
{
if(first)
{
node<t>*prev=0,*cur=first;
while(cur)
{
node<t>*p=prev;
prev=cur;
cur=cur->next;
prev->next=p;
}
first=prev;
}
}
int main()
{
chain<int>s1;
int c,d,p;
while(1)
{
cout<<"MENU\n1.insert at begining\n2.insert at end\n3.insert at requird position\n";
cout<<"4.del from begining\n5.del from end\n6.del from position\n7.display\n";
cout<<"8.count no of nodes\n9.reverse\n10.find\n11.search\n12.exit\nSELECT\n";
cin>>c;
switch(c)
{
case 1:
cout<<"enter data\n";
cin>>d;
s1.insertatbeg(d);
break;
case 2:
cout<<"enter data\n";
cin>>d;
s1.insertatend(d);
break;
case 3:
cout<<"enter data and positon\n";
cin>>d>>p;
s1.insertatpos(d,p);
break;
case 4:
d=s1.delfrombeg();
if(d)
cout<<"the deleted data is"<<d<<endl;
else
cout<<"list is empty\n";
break;
case 5:
d=s1.delfromend();
if(d)
cout<<"the deleted data is"<<d<<endl;
else
cout<<"list is empty\n";
break;
case 6:
cout<<"enter the position to be deleted\n";
cin>>c;
d=s1.delfrompos(c);
if(d)
cout<<"deleted element is: "<<d<<endl;
else
cout<<"invalid position\n";
break;
case 7:
s1.display();
break;
case 8:
d=s1.count();
cout<<"the numbr of nodes are"<<d<<endl;
break;
case 9:
s1.reverse();
break;
case 10:
cout<<"enter the location\n";
cin>>c;
d=s1.find(c);
cout<<"the value at given position is"<<d<<endl;
break;
case 11:
cout<<"ent\er the data to be searched\n";
cin>>d;
c=s1.search(d);
cout<<"the data is at position"<<c<<endl;
break;
case 12:exit(0);
}
}
}
OUTPUT:
}
void insert(T ele,int pos)
{
if(pos<0||pos>ls)
{
cout<<"invalid position given"<<endl;
return;
}
node<T> *q=new node<T> (ele);
if(isempty())
{
first=last=q;
ls++;
}
else
{
if(pos<=(int)(ls)/2)
{
if(pos==0)
{
q->next=first;
first->prev=q;
first=q;
ls++;
return;
}
node<T> *p=first;
for(int i=0;i<pos-1;i++,p=p->next);
q->next=p->next;
q->prev=p;
q->next->prev=q;
p->next=q;
}
else
{
if(pos==ls)
{
last->next=q;
q->prev=last;
last=q;
ls++;
return;
}
node<T> *p=last;
for(int i=ls-1;i>=pos;i--,p=p->prev);
q->next=p->next;
q->prev=p;
q->next->prev=q;
p->next=q;
}
ls++;
}
}
first=first->next;
cout<<p->ele<<" is deleted"<<endl;
delete p;
return;
}
for(int i=0;i<pos-1;i++)
{
p=p->next;
}
node<T> *x=p->next;
cout<<x->ele<<endl;
p->next=p->next->next;
delete x;
}
};
main()
{
int x,p,ch,k;
dlinkedlist <int>a;
while(1)
{
cout<<"1.isempty 2.size 3.insert 4.index 5.show 6.exit"<<endl;
cin>>ch;
switch(ch)
{
case 1:x=a.isempty();
if(x==1)
cout<<"list is empty"<<endl;
else
cout<<"list is not empty"<<endl;
break;
case 2:x=a.size();
cout<<"list size is"<<endl;
cout<<x;
break;
case 3:cout<<"enter the element and position"<<endl;
cin>>x>>p;
a.insert(x,p);
break;
case 4:cout<<"enter the element whose index is to be found"<<endl;
cin>>x;
k=a.indexof(x);
if(k!=-1)
cout<<x<<"element is fount at position"<<k<<endl;
else
cout<<"element is not found in list"<<endl;
break;
case 5:a.show();
break;
case 6:exit(1);
}
}
}
OUTPUT:
{
t=t->next;
}
if(t!=header)
return t->data;
else
return -1;
}
template<class T>
int cll<T>::indexOf(const T& x) const
{
int index=0;
header->data=x;
node<T>* ptr=header->next;
while(ptr->data!=x)
{
ptr=ptr->next;
index++;
}
if(ptr==header)
return -1;
else
return index;
}
template<class T>
void cll<T>::erase(int index)
{
node<T>* del;
if(index<0 || index>ls)
{
cout<<"Illegal index\n";
return;
}
node<T>* p;
p=header;
for(int i=0;i<index;i++)
p=p->next;
del=p->next;
p->next=p->next->next;
ls--;
delete del;
}
template<class T>
void cll<T>::insert(int index,const T& x)
{
if(index<0 || index>ls)
{
cout<<"Illegal index\n";
return;
}
node<T>* p;
p=header;
for(int i=0;i<index;i++)
p=p->next;
node<T>* nn=new node<T>(x);
nn->next=p->next;
p->next=nn;
ls++;
}
template<class T>
void cll<T>::output(ostream& out) const
{
cout<<"Display function\n";
for(node<T>* current=header->next;current!=header;current=current->next)
cout<<current->data<<" ";
}
template<class T>
ostream& operator<<(ostream& out, const cll<T> &c1)
{
c1.output(out);
return out;
}
main()
{
cout<<"Main begins\n";
int ch,x,ele,in,index;
char op;
cll<int> c1;
do{
cout<<"MENU:\n1.Empty\n2.Size\n3.Get\n4.IndexOf\n5.Insert\n6.Erase\n7.Display\n";
cout<<"Enter your choice\n";
cin>>ch;
switch(ch)
{
case 1:if(c1.empty()==1)
cout<<"The linked list is empty\n";
else
cout<<"List contain elements\n";
break;
case 2:cout<<"Size of list is "<<c1.size();
cout<<endl;
break;
case 3:cout<<"Enter index to get element\n";
cin>>index;
cout<<"Your ele is "<<c1.get(index);
break;
case 4:cout<<"Enter element to get index\n";
cin>>x;
cout<<"Your index is "<<c1.indexOf(x);
break;
case 5:cout<<"Insert the index and the element\n";
cin>>in>>ele;
c1.insert(in,ele);
break;
case 6:cout<<"Enter the index of ele you want to delete\n";
cin>>x;
c1.erase(x);
break;
case 7:cout<<c1;
break;
default:exit(0);
}
cout<<"Do you want to continue?\n";
cin>>op;
}while(op=='y');
cout<<"Main ends\n";
}
OUTPUT:
template<class t>
void stack<t>::push(t &x)
{
if(top==capacity-1)
{
enhancecapacity();
capacity*=2;
}
st[++top]=x;
}
template<class t>
void stack<t>::enhancecapacity()
{
t *tmp=new t[2*capacity];
for(int i=0;i<=top;i++)
tmp[i]=st[i];
delete[]st;
st=tmp;
cout<<"Stack size doubled\n";
}
template<class t>
t stack<t>::pop()
{
if(isempty())
return NULL;
else
{
t x=st[top--];
return x;
}
}
template<class t>
void stack<t>::display() const
{
if(isempty())
cout<"stack is empty\n";
else
for(int i=top;i>=0;i--)
cout<<st[i]<<endl;
}
template<class t>
t stack<t>::topmost()const
{
if(isempty())
return NULL;
else
return st[top];
}
int main()
{
stack<int> s(2);
int c,d;
while(1)
{
cout<<"MENU\n1.push\n2.pop\n3.display\n4.top element\n5.exit\n";
cout<<"SELECT YOUR OPTION\n";
cin>>c;
switch(c)
{
case 1:
cout<<"enter data\n";
cin>>d;
s.push(d);
break;
case 2:
d=s.pop();
if(d!=0)
cout<<"the popped data is "<<d<<endl;
else
cout<<"stack underflow\n";
break;
case 3:
cout<<"the contents of stack are\n";
s.display();
break;
case 4: d=s.topmost();
if(d)
cout<<"top element is : "<<d<<endl;
else
cout<<"stack underflow\n";
break;
case 5: exit(0);
}
}
}
OUTPUT:
delete tmp;
return x;
}
return 0;
}
template<class t>
void stack<t>::display()
{
if(top)
{
node<t>*tmp=top;
while(tmp)
{
cout<<tmp->data<<endl;
tmp=tmp->link;
}
}
else
cout<<"stack underflow\n";
}
template<class t>
t stack<t>::topmost()
{
if(top)
return top->data;
return 0;
}
int main()
{
stack<int> s;
int c,d;
while(1)
{
cout<<"MENU\n1.push\n2.pop\n3.display\n4.top element\n5.exit\n";
cout<<"SELECT YOUR OPTION\n";
cin>>c;
switch(c)
{
case 1:
cout<<"enter data\n";
cin>>d;
s.push(d);
break;
case 2:
d=s.pop();
if(d!=0)
cout<<"the popped data is "<<d<<endl;
else
cout<<"stack underflow\n";
break;
case 3:
cout<<"the contents of stack are\n";
s.display();
break;
case 4: d=s.topmost();
if(d)
cout<<"top element is : "<<d<<endl;
else
cout<<"stack underflow\n";
break;
case 5: exit(0);
}
}
}
OUTPUT:
rear--;
}
q[++rear]=x;
if(front==-1)
front=0;
}
template<class t>
void queue<t>::enhancecapacity()
{
t *tmp=new t[2*capacity];
for(int i=front;i<=rear;i++)
tmp[i]=q[i];
delete []q;
q=tmp;
cout<<"Q size doubled\n";
}
template<class t>
t queue<t>::pop()
{
if(isempty())
return 0;
t x=q[front];
if(front==rear)
front=rear=-1;
else
front++;
return x;
}
template<class t>
t queue<t>::frontele()const
{
if(isempty())
return 0;
else
return q[front];
}
template<class t>
t queue<t>::rearele()const
{
if(isempty()) return 0;
else return q[rear];
}
template<class t>
void queue<t>::display()const
{
if(isempty()) cout<<"queue underflow\n";
else
{
for(int i=front;i<=rear;i++)
cout<<q[i]<<endl;
}
}
int main()
{
queue<int> c1(2);
int c,d;
while(1)
{
cout<<"MENU\n1.push\n2.pop\n3.display\n4.frontele\n5.rearele\n6.exit\n";
cout<<"SELECT YOUR OPTION\n";
cin>>c;
switch(c)
{
case 1:
cout<<"enter data\n";
cin>>d;
c1.push(d);
break;
case 2:
d=c1.pop();
if(d!=0)
cout<<"the popped data is "<<d<<endl;
else
cout<<"empty queue\n";
break;
case 3:
cout<<"the contents of queue are\n";
c1.display();
break;
case 4: d=c1.frontele();
if(d)
cout<<"front element is : "<<d<<endl;
else
cout<<"q underflow\n";
break;
case 5: d=c1.rearele();
if(d)
cout<<"front element is :"<<d<<endl;
else
cout<<"q underflow\n";
break;
case 6: exit(0);
}
}
}
OUTPUT:
node<t>* tmp=front;
front=front->link;
if(front==NULL)
rear=NULL;
t x=tmp->data;
delete tmp;
return x;
}
return 0;
}
template<class t>
void queue<t>::display()
{
if(front)
{
cout<<"Q elements are:\n";
node<t>*tmp=front;
while(tmp)
{
cout<<tmp->data<<endl;
tmp=tmp->link;
}
}
else
cout<<"queue underflow\n";
}
template<class t>
t queue<t>::frontele()
{
if(front)
return front->data;
return 0;
}
template<class t>
t queue<t>::rearele()
{
if(rear)
return rear->data;
return 0;
}
int main()
{
queue<int> c1;
int c,d;
while(1)
{
cout<<"MENU\n1.push\n2.pop\n3.display\n4.frontele\n5.rearele\n6.exit\n";
cout<<"SELECT YOUR OPTION\n";
cin>>c;
switch(c)
{
case 1:
cout<<"enter data\n";
cin>>d;
c1.push(d);
break;
case 2:
d=c1.pop();
if(d!=0)
cout<<"the popped data is "<<d<<endl;
else
cout<<"empty queue\n";
break;
case 3:
cout<<"the contents of queue are\n";
c1.display();
break;
case 4: d=c1.frontele();
if(d)
cout<<"front element is : "<<d<<endl;
else
cout<<"q underflow\n";
break;
case 5: d=c1.rearele();
if(d)
cout<<"front element is :"<<d<<endl;
else
cout<<"q underflow\n";
break;
case 6: exit(0);
}
}
}
OUTPUT:
class stack
{
t *st;
int top,capacity,i;
public:stack(int max)
{
capacity=max;
if(capacity<1)
{
cout<<"capacity must be >1\n";
exit(0);
}
st=new t[capacity];
top=-1;
int isempty()
{
return top==-1;
}
void enhancecapacity()
{
t *tmp=new t[capacity*2];
for(i=0;i<=top;i++)
tmp[i]=st[i];
delete []st;
st=tmp;
cout<<"stack size is doubled\n";
}
void push(t &x)
{
if(top==capacity-1)
{
enhancecapacity();
capacity*=2;
}
st[++top]=x;
}
t pop()
{
if(isempty())
return -1;
t x=st[top--];
return x;
}
void disp()
{
if(isempty())
cout<<"empty\n";
else
{
for(i=top;i>=0;i--)
cout<<st[i]<<"\t";
cout<<endl;
}
}
t topmost()
{
if(isempty())
return -1;
return st[top];
}
};
int priority(char ch);
void convert(char *a)
{
int x,j=0;
stack<char>st(20);
char b[20];
int len=strlen(a);
for(int i=0;i<len;i++)
{
char ch=a[i];
if(isalpha(ch))
b[j++]=ch;
else if(isdigit(ch))
b[j++]=ch;
else if(ch=='(')
{
while(st.topmost()!='(')
b[j++]=st.pop();
st.pop();
}
else
{
while(priority(ch)<=priority(st.topmost()))
b[j++]=st.pop();
st.push(ch);
}
}
while(!st.isempty())
b[j++]=st.pop();
b[j]='\0';
cout<<"postfix expression is..."<<b;
}
main()
{
char p[20];
cout<<"enter infix expression : ";
cin>>p;
convert(p);
}
OUTPUT:
}
}
T topmost()
{
if(isempty()) return 0;
else return s[top];
}
};
void eval(char *b)
{
int i=0,p1,p2,p3;
stack<int> s1(20);
while(b[i]!='\0')
{
if(b[i]>='0' && b[i]<='9')
s1.push(b[i]-48);
else
{
p1=s1.pop(); p2=s1.pop();
switch(b[i])
{
case '^':p3=pow(p2,p1);
s1.push(p3); break;
case '+':p3=p2+p1;
s1.push(p3); break;
case '-':p3=p2-p1;
s1.push(p3); break;
case '*':p3=p1*p2;
s1.push(p3); break;
case '/':p3=p2/p1;
s1.push(p3); break;
}
}
i++;
}
cout<<endl<<"result :"<<s1.pop()<<endl;
}
main()
{
char p[20];
cout<<"enter the postfix expression"<<endl;
cin>>p;
eval(p);
}
OUTPUT:
k->p=pow;
append(k);
cin>>coe;
}
}
T eval(T x)
{
T sum=0;
node<T> *temp;
temp=first;
while(temp!=NULL)
{
sum+=temp->c*pow(x,temp->p);
temp=temp->link;
}
return sum;
}
void add(pol<T> a,pol<T> b)
{
node<T> *t1,*t2,*k;
T sum;
t1=a.first;
t2=b.first;
while(t1!=NULL&&t2!=NULL)
{
if(t1->p>t2->p)
{
k=new node<T>;
k->c=t1->c;
k->p=t1->p;
append(k);
t1=t1->link;
}
else if(t1->p<t2->p)
{
k=new node<T>;
k->c=t2->c;
k->p=t2->p;
append(k);
t2=t2->link;
}
else
{
sum=t1->c+t2->c;
if(sum==0)
{
t1=t1->link;
t2=t2->link;
}
else
{
k=new node<T>;
k->c=sum;
k->p=t1->p;
append(k);
t1=t1->link;
t2=t2->link;
}
}
}
while(t1!=NULL)
{
k=new node<T>;
k->c=t1->c;
k->p=t1->p;
append(k);
t1=t1->link;
}
while(t2!=NULL)
{
k=new node<T>;
k->c=t2->c;
k->p=t2->p;
append(k);
t2=t2->link;
}
}
void display()
{
node<T> *q;
if(first==NULL)
cout<<"no polynomial created\n";
else
{
cout<<"polynomial is\n";
q=first;
while(q)
{
cout<<q->c<<"x^"<<q->p;
if(q->link!=NULL)
cout<<"+";
q=q->link;
}
}
}
void multiply(pol<T> a,pol<T> b)
{
node<T> *t1,*t2;
pol<T> d,e;
d.first=new node<T>;
t1=a.first;
t2=b.first;
while(t1!=NULL)
{
while(t2!=NULL)
{
d.first->c=t1->c*t2->c;
d.first->p=t1->p+t2->p;
d.first->link=NULL;
e.add(*this,d);
erase();
t2=t2->link;
}
t1=t1->link;
t2=b.first;
}
e.display();
}
void erase()
{
node<T> *q;
while(first!=NULL)
{
q=first;
first=first->link;
delete q;
}
}
};
main()
{
pol<int> j,l,w,r;
j.create();
l.create();
r.add(j,l);
r.display();
r.multiply(j,l);
}
OUTPUT:
/*
* HashEntry Class Declaration
*/
class HashEntry
{
public:
int key;
int value;
HashEntry(int key, int value)
{
this->key = key;
this->value = value;
}
};
class HashMap
{
private:
HashEntry **table;
int neverUsed[TABLE_SIZE];
public:
HashMap()
{
table = new HashEntry * [TABLE_SIZE];
for (int i = 0; i< TABLE_SIZE; i++)
{
table[i] = NULL;
neverUsed[i]=1;
}
}
/*
* Hash Function
*/
int HashFunc(int key)
{
return key % TABLE_SIZE;
}
void Insert(int key, int value)
{
int hash = HashFunc(key);
int tmp=hash;
while (table[hash] != NULL && table[hash]->key != key)
{
if (neverUsed[hash]||f)
{
cout<<"Element Not Found:";
}
else
{
delete table[hash];
table[hash]=NULL;
cout<<"Element Deleted"<<endl;
}
void display()
{
for(int i=0;i<TABLE_SIZE;i++)
{
if(table[i]!=NULL)
cout<<"["<<i<<","<<table[i]->key<<","<<table[i]->value<<"] ";
else
cout<<"["<<i<<",,]";
}
}
~HashMap()
{
for (int i = 0; i < TABLE_SIZE; i++)
{
if (table[i] != NULL)
delete table[i];
delete[] table;
}
}
};
int main()
{
HashMap hash;
int key, value;
int choice;
while (1)
{
cout<<"\n----------------------"<<endl;
cout<<"Operations on Hash Table"<<endl;
cout<<"\n----------------------"<<endl;
cout<<"1.Insert element into the table"<<endl;
cout<<"2.Search element from the key"<<endl;
cout<<"3.Delete element at a key"<<endl;
cout<<"4.Display"<<endl;
cout<<"5.Exit"<<endl;
cout<<"Enter your choice: ";
cin>>choice;
switch(choice)
{
case 1:
cout<<"Enter element to be inserted: ";
cin>>value;
cout<<"Enter key at which element to be inserted: ";
cin>>key;
hash.Insert(key, value);
break;
case 2:
cout<<"Enter key of the element to be searched: ";
cin>>key;
if (hash.Search(key) == -1)
{
cout<<"No element found at key "<<key<<endl;
continue;
}
else
{
cout<<"Element at key "<<key<<" : ";
cout<<hash.Search(key)<<endl;
}
break;
case 3:
cout<<"Enter key of the element to be deleted: ";
cin>>key;
hash.Remove(key);
break;
case 4:
hash.display();
break;
case 5:
exit(1);
default:
cout<<"\nEnter correct option\n";
}
}
return 0;
}
OUTPUT:
}
}
template <class T>
void postOrder(Node<T> *t)
{
if (t != NULL)
{
postOrder(t->left); // do left subtree
postOrder(t->right); // do right subtree
visit(t); // visit tree root
}
}
template <class T>
void levelOrder(Node<T> *t)
{
queue<Node<T>*> q(10);
while (t != NULL)
{
visit(t); // visit t
// put t's children on queue
if (t->left != NULL)
q.push(t->left);
if (t->right != NULL)
q.push(t->right);
preOrder(n5);
cout << endl;
cout << "Postorder sequence is ";
postOrder(n5);
cout << endl;
cout << "Level order sequence is ";
levelOrder(n5);
}
OUTPUT:
curr = curr->right;
}
}
}
template<class t>
{
if (root == NULL)
return;
stack<Node<t> *> s(20);
s.push(root);
while (!s.isempty())
{
Node<t> *curr = s.topmost();
cout<<curr->element<<" ";
s.pop();
if (curr->right)
s.push(curr->right);
if (curr->left)
s.push(curr->left);
}
}
template<class t>
void postOrder(Node<t>* root)
{
if(root==NULL)
return;
stack<Node<t>*> s(20);
s.push(root);
stack<int> out(20);
while (!s.isempty())
{
Node<t> *curr = s.topmost();
s.pop();
out.push(curr->element);
if (curr->left)
s.push(curr->left);
if (curr->right)
s.push(curr->right);
}
while (!out.isempty())
{
cout << out.topmost() << " ";
out.pop();
}
}
int main(void)
{
OUTPUT:
#include <iostream>
using namespace std;
//****INCLUDE QUEUE PROGRAM HERE WITHOUT MAIN
template <class T>
struct Node
{
T element;
Node<T> *left, // left subtree
*right; // right subtree
Node() {left = right = NULL;}
Node(const T& theElement):element(theElement)
{
left = right = NULL;
}
Node(const T& theElement,
Node *theleft,
Node *theright)
:element(theElement)
{
left = theleft;
right = theright;
}
};
template<class t>
bool iterativeSearch(Node<t> *root,t x)
{
if (root == NULL)
return false;
queue<Node<t> *> q(20);
q.push(root);
while (!q.isempty())
{
Node<t> *curr = q.frontele();
if (curr->element == x)
return true;
q.pop();
if (curr->left != NULL)
q.push(curr->left);
if (curr->right != NULL)
q.push(curr->right);
}
return false;
}
template<class T>
int height(Node<T> *temp)
{
int h = 0;
if (temp != NULL)
{
int l_height = height (temp->left);
int r_height = height (temp->right);
int max_height = max (l_height, r_height);
h = max_height + 1;
}
return h;
}
template<class t>
void deleteTree(Node<t>* root)
{
if (root == NULL) return;
deleteTree(root->left);
deleteTree(root->right);
cout<<"\n Deleting node:"<< root->element;
delete root;
}
template<class t>
Node<t>* clone(Node<t>* root)
{
Node<t>* nn=NULL;
if(root)
{
nn=new Node<t>();
nn->element=root->element;
nn->left=clone(root->left);
nn->right=clone(root->right);
}
return nn;
}
template<class t>
Node<t>* mirror(Node<t>* root)
{
Node<t>* m_root = NULL;
if(root)
{
m_root = new Node<t>();
m_root->element=root->element;
m_root->left = mirror(root->right);
m_root->right = mirror(root->left);
}
return m_root;
}
{
inOrder(t->left); // do left subtree
cout<<t->element<<" "; // visit tree root
inOrder(t->right); // do right subtree
}
}
int main(void)
{
return 0;
}
OUTPUT:
*root = *temp;
delete temp;
}
else
{
Node* temp = minValueNode(root->right);
root->data = temp->data;
root->right = deleteNode(root->right, temp->data);
}
}
return root;
}
Node *BST::insert(Node *root, int value)
{
if (root == NULL)
{
root = new Node;
root->data = value;
root->left = NULL;
root->right = NULL;
return root;
}
else if (value < root->data)
{
root->left = insert(root->left, value);
}
else if (value > root->data)
{
root->right = insert(root->right, value);
}
else
{
cout<<"INFO: Element already Exist"<<endl;
return root;
}
}
void BST::display(Node *ptr, int level)
{
int i;
if (ptr!=NULL)
{
display(ptr->right, level + 1);
printf("\n");
if (ptr == root)
cout<<"Root -> ";
for (i = 0; i < level && ptr != root; i++)
cout<<" ";
cout<<ptr->data;
display(ptr->left, level + 1);
}
}
int main()
{
int choice, item;
BST avl;
while (1)
{
cout<<"\n---------------------"<<endl;
cout<<"BST Implementation"<<endl;
cout<<"\n---------------------"<<endl;
cout<<"1.Insert Element into the tree"<<endl;
cout<<"2.Delete Element"<<endl;
cout<<"3.Display Tree"<<endl;
cout<<"4.Exit"<<endl;
cout<<"Enter your Choice: ";
cin>>choice;
switch(choice)
{
case 1:
cout<<"Enter value to be inserted: ";
cin>>item;
root = avl.insert(root, item);
break;
case 2:
cout<<"Enter the value to be deleted:";
cin>>item;
avl.deleteNode(root,item);
break;
case 3:
if (root == NULL)
{
cout<<"Tree is Empty"<<endl;
continue;
}
cout<<"BST:"<<endl;
avl.display(root, 1);
break;
case 4:
exit(1);
break;
default:
cout<<"Wrong Choice"<<endl;
}
}
return 0;
} OUTPUT:
temp = parent->right;
parent->right = temp->left;
temp->left = parent;
return temp;
}
Node *avlTree::ll_rotation(Node *parent)
{
Node *temp;
temp = parent->left;
parent->left = temp->right;
temp->right = parent;
return temp;
}
Node *avlTree::lr_rotation(Node *parent)
{
Node *temp;
temp = parent->left;
parent->left = rr_rotation (temp);
return ll_rotation (parent);
}
Node *avlTree::rl_rotation(Node *parent)
{
Node *temp;
temp = parent->right;
parent->right = ll_rotation (temp);
return rr_rotation (parent);
}
Node *avlTree::balance(Node *temp)
{
int bal_factor = diff (temp);
if (bal_factor > 1)
{
if (diff (temp->left) > 0)
temp = ll_rotation (temp);
else
temp = lr_rotation (temp);
}
else if (bal_factor < -1)
{
if (diff (temp->right) > 0)
temp = rl_rotation (temp);
else
temp = rr_rotation (temp);
}
return temp;
}
Node * minValueNode(Node* node)
{
Node* current = node;
if (root == NULL)
return root;
if ( data < root->data )
root->left = deleteNode(root->left, data);
else if( data > root->data )
root->right = deleteNode(root->right, data);
else
{
if( (root->left == NULL) || (root->right == NULL) )
{
Node *temp = root->left ? root->left : root->right;
if (temp == NULL)
{
temp = root;
root = NULL;
}
else
*root = *temp;
delete temp;
}
else
{
Node* temp = minValueNode(root->right);
root->data = temp->data;
root->right = deleteNode(root->right, temp->data);
}
}
if (root == NULL)
return root;
root=balance(root);
return root;
}
Node *avlTree::insert(Node *root, int value)
{
if (root == NULL)
{
root = new Node;
root->data = value;
root->left = NULL;
root->right = NULL;
return root;
}
else if (value < root->data)
{
root->left = insert(root->left, value);
root = balance (root);
}
else if (value >= root->data)
{
root->right = insert(root->right, value);
root = balance (root);
}
return root;
}
void avlTree::display(Node *ptr, int level)
{
int i;
if (ptr!=NULL)
{
display(ptr->right, level + 1);
printf("\n");
if (ptr == root)
cout<<"Root -> ";
for (i = 0; i < level && ptr != root; i++)
cout<<" ";
cout<<ptr->data;
display(ptr->left, level + 1);
}
}
int main()
{
int choice, item;
avlTree avl;
while (1)
{
cout<<"\n---------------------"<<endl;
cout<<"AVL Tree Implementation"<<endl;
cout<<"\n---------------------"<<endl;
cout<<"1.Insert Element into the tree"<<endl;
cout<<"2.Delete Element"<<endl;
cout<<"3.Display Balanced AVL Tree"<<endl;
cout<<"4.Exit"<<endl;
cout<<"Enter your Choice: ";
cin>>choice;
switch(choice)
{
case 1:
cout<<"Enter value to be inserted: ";
cin>>item;
OUTPUT:
OUTPUT:
}
// Insert all the remaining values from i to mid into temp[].
while (i <= mid)
{
temp[k] = a[i];
k++;
i++;
}
// Insert all the remaining values from j to high into temp[].
while (j <= high)
{
temp[k] = a[j];
k++;
j++;
}
// Assign sorted data stored in temp[] to a[].
for (i = low; i <= high; i++)
{
a[i] = temp[i-low];
}
}
// A function to split array into two parts.
void MergeSort(int low, int high)
{
int mid;
if (low < high)
{
mid=(low+high)/2;
// Split the data into two half.
MergeSort(low, mid);
MergeSort(mid+1, high);
// Merge them to get sorted output
Merge(low, high, mid);
}
}
};
int main()
{
Sorting s;
s.sort();
s.dis();
}
OUTPUT:
void sort()
{
quicksort(0,n);
}
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);
}
};
int main()
{
Sorting s;
s.sort();
s.dis();
}
OUTPUT:
}
i=j;
j=2*i+1;
}
}
template<class t>
void heap<t>::accept()
{
for(int i=0;i<n;i++)
cin>>a[i];
}
template<class t>
void heap<t>::display()
{
for(int i=0;i<n;i++)
cout<<a[i]<<"\t";
cout<<endl;
}
int main()
{
int k;
cout<<"enter number of elements\n";
cin>>k;
heap<int> h(k);
h.accept();
cout<<"Before sorting array is:\n";
h.display();
h.heapsort();
cout<<"After sorting array is:\n";
h.display();
}
OUTPUT:
OUTPUT:
OUTPUT:
}
else if(arr[middle] == search)
{
cout<<search<<" found at location "<<middle+1<<"\n";
break;
}
else
{
last = middle - 1;
}
middle = (first + last)/2;
}
if(first > last)
{
cout<<"Not found! "<<search<<" is not present in the list.";
}
}
OUTPUT: