Vous êtes sur la page 1sur 0

p

a
n
c
h
a
m
u
k
e
s
h
.
b
l
o
g
s
p
o
t
.
c
o
m
DATA STRUCTURES
A Data Structure is a set of domains D,set of functions F,set of axioms A.This is tiple(D,F,A) denotes the
data structure d.
Types of data structures
Linear abstract data types are Lists,stacks,queues .
Non Linear abstract data types are trees and graphs .
ADT:-
Abstract data type is tripe(D,F,A) in which only what is to be done is mentioned but how is to be done is
not mentioned.
List ADT :-
Is a collection of elements arranged in sequential manner .we can represent in two ways
1.elements stored with arrays .
2.elements stored with pointers .
memory allotment for lists
ADT for LIST can be written as :-
INSTANCES :
Data_type arrayname[max_size];
Data_type array_index;
The list is created in the array
PRECONDITION:
The array_index should be within 0 to max_size of array .
OPERATION:
1.create()
2.display()
p
a
n
c
h
a
m
u
k
e
s
h
.
b
l
o
g
s
p
o
t
.
c
o
m
3.insert()
4.delete()
TYPES:-
1.singly linear linked list.
2.singly circular linked list.
3.doubly linear linked list.
4.doubly circular linked list.
Advantages of linked list:-
p
a
n
c
h
a
m
u
k
e
s
h
.
b
l
o
g
s
p
o
t
.
c
o
m
1.memory is allocated dynamically.
2.insertion and deletion is easy.
3.data is deleted physically.
Applications of linked list:-
1.using linked list we can implement stacks,queues,trees,graphs.
2.to construct associative arrays .
3.used as primary data structure in LISP .
Stacks ADT :-
The stack is a collection ofordered list but it can access,insertion and deletion by using a concept called
LIFO .
stack representation
ADT for STACK can be written as :-
INSTANCES :
Data_type stack[max_size];
Data_type top;
PRECONDITION:
The top should exceed the limit of max_size
OPERATION:
1.create()
2.display()
3.push()
4.pop()
5.stfull()
6.stempty()
EXAMPLE:-
p
a
n
c
h
a
m
u
k
e
s
h
.
b
l
o
g
s
p
o
t
.
c
o
m
/*write a CPP to implement all the operations of stacks using arrays*/
#include<iostream.h>
#include<conio.h>
#define max 10
template <class type>
class stack
{
type st[max];
int top;
public:
type item;
void init()
{
top=0;
}
int size()
{
return top;
}
void push(type ch);
void pop();
void display();
};
template<class type>
void stack<type>::push(type item)
{
if(top==max)
{
cout<<"stack is full"<<endl;
}
st[top++]=item;
}
template <class type>
void stack<type>::pop()
{
if(top==0)
{
cout<<"stack is empty"<<endl;
}
cout<<"deleted element is "<<st[top--];
}
template<class type>
void stack<type>::display()
{
int i;
cout<<"elements are : ";
if(top==0)
{
cout<<"stack is empty"<<endl;
}
else if(top==max)
{
p
a
n
c
h
a
m
u
k
e
s
h
.
b
l
o
g
s
p
o
t
.
c
o
m
cout<<"stack is full"<<endl;
}
for(i=top;i>=0;i--)
cout<<st[i];
}
void main()
{
clrscr();
stack <char>s1;
stack <int>s2;
char ch_item;
int int_item;
int i,j,q;
s1.init();
s2.init();
cout<<"how many elements your want to insert"<<endl;
cin>>j;
for(i=1;i<=j;i++)
{
cout<<"enter the character item to be pushed"<<endl;
cin>>ch_item;
s1.push(ch_item);
}
cout<<"how many integer elements your want to insert"<<endl;
cin>>q;
for(i=1;i<=q;i++)
{
cout<<"enter the integer item to be pushed"<<endl;
cin>>int_item;
s2.push(int_item);
}
cout<<"how many elements you want to pop from characters"<<endl;
cin>>j;
for(i=1;i<=j;i++)
{
s1.pop();
}
cout<<"how many elements you want to pop from characters"<<endl;
cin>>j;
for(i=1;i<=j;i++)
{
s2.pop();
}
s1.display();
s2.display();
getch();
}
Example:-
/*write a CPP to implement all the operations of stacks using single linked list*/
#include<iostream.h>
p
a
n
c
h
a
m
u
k
e
s
h
.
b
l
o
g
s
p
o
t
.
c
o
m
#include<conio.h>
#include<stdlib.h>
#define NULL 0
template <class T>
class StackList
{
struct stack
{
T info;
stack *next;
}*first,*list,*root;
public:
StackList()
{
root=NULL;
}
void push();
void pop();
void disp();
};
template <class T>void StackList<T>::push()
{
first=new stack;
cout<<"Enter the element"<<endl;
cin>>first->info;
first->next=root;
root=first;
}
template <class T>void StackList<T>::pop()
{
if(root==NULL)
cout<<"List is empty";
else
{
cout<<"The popped element is" <<root->info;
root=root->next;
}
}
template <class T>void StackList<T>::disp()
{
list=root;
if(list==NULL)
cout<<"\n List is empty"<<endl;
else
{
cout<<"The stack elements are"<<endl;
while(list!=NULL)
{
cout<<"\t"<<list->info;
list=list->next;
}
p
a
n
c
h
a
m
u
k
e
s
h
.
b
l
o
g
s
p
o
t
.
c
o
m
}
}
void main()
{
StackList<int>s1;
int ch;
do
{
cout<<"\n1.Insert\n2. Delete\n3.Display\n4.Exit"<<endl;
cout<<"Enter u r choice"<<endl;
cin>>ch;
switch(ch)
{
case 1: s1.push();
break;
case 2: cout<<"\n";
s1.pop();
break;
case 3: s1.disp();
break;
case 4: exit(1);
break;
}
}while(ch>0 && ch<=4);
getche();
}
APPLICATION OF STACKS:-
1.conversion of one from of expression to another expression .
2.for evaluating the expression.
3.decimal to binary conversion .
4.is used to reversing the string.
QUEUE:-
DEF :Is an ordered collection of elements that has both front and rear ends.
ADT for QUEUES can be written as :-
INSTANCES :
Que[max] is an finite collection of elements that has both front and rear ends.
PRECONDITION:
The front and rear should be within the maximum size MAX .
Before insertion , check whether the queue is full or not.
Before deletion , check whether the queue is empty or not.
OPERATION:
1.create()
2.display()
3.insert()
4.delete()
representations of queues :
1.using arrays .
2.using linked lists.
EXAMPLE:-
p
a
n
c
h
a
m
u
k
e
s
h
.
b
l
o
g
s
p
o
t
.
c
o
m
/*write a CPP to implement all the operations of queue using single linked list*/
#include<iostream.h>
#include<conio.h>
#define max 10
template <class type>
class queue
{
type qu[max];
int front,rear;
public:
type item;
void init()
{
front=rear=-1;
}
void insert(type ch);
void del();
void display();
};
template<class type>
void queue<type>::insert(type item)
{
if(rear>=max)
{
cout<<"queue is full"<<endl;
front=rear=-1;
return;
}
qu[++rear]=item;
}
template <class type>
void queue<type>::del()
{
if((front==rear)
{
cout<<"queue is empty or underflow"<<endl;
}
cout<<"deleted elements are" <<qu[++front];
}
template<class type>
void queue<type>::display()
{
if(rear==front)
{
cout <<" queue empty";
return;
}
for(int i=front+1;i<=rear;i++)
cout <<qu[i]<<" ";
}
}
p
a
n
c
h
a
m
u
k
e
s
h
.
b
l
o
g
s
p
o
t
.
c
o
m
void main()
{
clrscr();
queue <char>q1;
queue <int>q2;
char ch_item;
int int_item;
int i,j,q;
q1.init();
q2.init();
cout<<"how many elements your want to insert"<<endl;
cin>>j;
for(i=1;i<=j;i++)
{
cout<<"enter the character item to be inserted"<<endl;
cin>>ch_item;
q1.insert(ch_item);
}
cout<<"how many integer elements your want to insert"<<endl;
cin>>q;
for(i=1;i<=q;i++)
{
cout<<"enter the integer item to be inserted"<<endl;
cin>>int_item;
q2.insert(int_item);
}
cout<<"how many elements you want to delete from characters"<<endl;
cin>>j;
for(i=1;i<=j;i++)
{
q1.del();
}
cout<<"how many elements you want to delete from characters"<<endl;
cin>>j;
for(i=1;i<=j;i++)
{
q2.del();
}
q1.display();
q2.display();
getch();
}
Example :-
/*Write C++ program to implement queue ADT using Singly linked list.*/
#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
#define NULL 0
template <class T>
class QueueList
{
p
a
n
c
h
a
m
u
k
e
s
h
.
b
l
o
g
s
p
o
t
.
c
o
m
private:
struct queue
{
T info;
queue *next;
}*first, *list, *front, *rear;
public:
QueueList()
{
front=NULL;
rear=NULL;
}
void insert();
void del();
void disp();
};
template <class T>void QueueList<T>::insert()
{
first=new queue;
cout<<"Emter element"<<endl;
cin>>first->info;
first->next=NULL;
if(rear==NULL)
{
rear=first;
front=first;
}
else
rear->next=first;
rear=first;
}
template <class T>void QueueList<T>::del()
{
if(front==NULL)
{
cout<<"List is empty"<<endl;
getch();
}
else
{
cout<<"the deleted element is "<<front->info<<"\n";
if(front==rear)
{
front=NULL;
rear=NULL;
}
else
front=front->next;
}
}
template <class T>void QueueList<T>::disp()
{
list=front;
if(list==NULL)
{
cout<<"List is empty"<<endl;
}
p
a
n
c
h
a
m
u
k
e
s
h
.
b
l
o
g
s
p
o
t
.
c
o
m
else
{
cout<<"\n front->"<<" ";
while(list!=rear)
{
cout<<list->info<<" ";
list=list->next;
}
cout<<list->info;
cout<<"rear"<<"\n";
}
}
void main()
{
QueueList<int>q1;
int ch;
do
{
cout<<"\n1.Insert\n2. Delete\n3.Display\n4.Exit"<<endl;
cout<<"Enter u r choice"<<endl;
cin>>ch;
switch(ch)
{
case 1: q1.insert();
break;
case 2: cout<<"\n";
q1.del();
break;
case 3: q1.disp();
break;
case 4: exit(1);
break;
}
}while(ch>0 && ch<=4);
getche();
}
There are 3 types of QUEUES .
1.circular queue.
2.dequeue .
3.priority queue .
1.circular queue.
/* Write a C++ program to implement circular queue*/
#include<iostream.h>
#include<stdlib.h>
#include<conio.h>
const MAX=5;
p
a
n
c
h
a
m
u
k
e
s
h
.
b
l
o
g
s
p
o
t
.
c
o
m
template <class T>
class Queue
{
T q[MAX];
T ele;
T temp;
int rear,front;
public:
Queue()
{
rear=front=-1;
}
void insert();
void delete1();
void disp();
};
template <class T> void Queue<T>::insert()
{
if((front==0) && (rear==MAX-1) || (front==rear+1))
{
cout<<"Overflow\n";
getch();
}
else
{
cout<<"Enter the element\n "<<endl;
cin>>ele;
if(front==-1)
{
front=0;
rear=0;
}
else if(rear== MAX-1)
rear=0;
else
rear=rear+1;
q[rear]=ele;
}
}
template <class T> void Queue<T>::disp()
{
int i;
for(i=front;i<=rear;i++)
{
cout<<"\t"<<q[i];
}
}
template <class T> void Queue<T>::delete1()
{
if(front==-1)
{
cout<<"Underflow";
p
a
n
c
h
a
m
u
k
e
s
h
.
b
l
o
g
s
p
o
t
.
c
o
m
getche();
}
else
{
ele=q[front];
if(front==rear)
{
front=-1;
rear=-1;
}
else if(front==MAX-1)
front=0;
else
front++;
cout<<"\nThe deleted element is \t"<<ele;
}
}
void main()
{
Queue<char> q1;
Queue<int> q2;
int ch;
clrscr();
do
{
cout<<"1.Insert\n2.Delete\n3. Display\n4.Exit\n"<<endl;
cout<<"Entr u r choice"<<endl;
cin>>ch;
switch(ch)
{
case 1: cout<<"enter character data";
q1.insert();
cout<<"\n\nenter integer data";
q2.insert();
break;
case 2: q1.delete1();
q2.delete1();
break;
case 3: q1.disp();
q2.disp();
break;
case 4: exit(1);
break;
}
}while(ch>0 && ch<=4);
getche();
}
p
a
n
c
h
a
m
u
k
e
s
h
.
b
l
o
g
s
p
o
t
.
c
o
m
Applications of circular queues
a.Job Scheduling .
b.categorizing data .
a.Job scheduling :
Runnig state,ready state,blocked state .
Dequeue :-
Double ended queue .
Example :-
Write C++ program to implement dqueue using doubly linked list.
#include <iostream.h>
#include <conio.h>
#include<stdlib.h>
template <class T>
class dqueue
{
private:
struct link_list
{
T no;
struct link_list *next;
struct link_list *prev;
}*list,*head;
public:
dqueue()
{
head= NULL;
}
int display_menu();
void insert_first();
void insert_last();
void delete_first();
void delete_last();
void disp();
};
template <class T>voiddqueue<T>:: insert_first()
{
link_list *newnode;
newnode=new link_list;
cout<<"Enter Number :";
cin>>newnode->no;
list=head;
if(list==NULL)
{
head=newnode;
newnode->next=NULL;
newnode->prev=NULL;
return;
}
newnode->next=list;
newnode->prev=NULL;
list->prev=newnode;
p
a
n
c
h
a
m
u
k
e
s
h
.
b
l
o
g
s
p
o
t
.
c
o
m
head=newnode;
}
template<class T>void dqueue<T>::insert_last()
{
link_list *newnode;
newnode=new link_list;
cout<<"Enter number";
cin>>newnode->no;
list=head;
if(list==NULL)
{
head=newnode;
newnode->next=NULL;
newnode->prev=NULL;
return;
}
while(list->next!=NULL)
{
list=list->next;
}
list->next=newnode;
newnode->prev=list;
newnode->next=NULL;
}
template<class T>void dqueue<T>::disp()
{
cout<<endl;
link_list *tail;
list=head;
if(head==NULL)
{
cout<<"Empty Queue";
return;
}
cout<<endl;
cout<<"Forward display..."<<endl;
while(list!=NULL)
{
cout<<list->no<<"\t";
if(list->next==NULL)
{
tail=list;
}
list=list->next;
}
list=tail;
cout<<endl<<"Backward display...."<<endl;
while(list!=NULL)
{
cout<<list->no<<"\t";
list=list->prev;
}
}
p
a
n
c
h
a
m
u
k
e
s
h
.
b
l
o
g
s
p
o
t
.
c
o
m
template<classT>void dqueue<T>::delete_first()
{
list=head;
list->next->prev=NULL;
head=list->next;
}
template<class T>void dqueue<T>::delete_last()
{
list=head;
while(list->next->next!=NULL)
{
list=list->next;
}
list->next=NULL;
}
void main()
{
dqueue<char>d;
clrscr();
while(1)
{
switch(d.display_menu())
{
case 1: d.insert_first();
d.disp();
getch();
break;
case 2: d.insert_last();
d.disp();
getch();
break;
case 3: d.delete_first();
d.disp();
getch();
break;
case 4: d.delete_last();
d.disp();
getch();
break;
case 5: d.disp();
getch();
break;
case 6: exit(1);
}
}
}
template <class T>int dqueue<T>::display_menu()
{
int ch;
clrscr();
cout<<"1.insert first\n2.insert last\n3.delete fron firstr\n4.delete from last\n5. display\n6.exit\n";
cout<<"Enter u r choice";
cin>>ch;
return(ch);
p
a
n
c
h
a
m
u
k
e
s
h
.
b
l
o
g
s
p
o
t
.
c
o
m
}
priority queue :-
ADT OF PRIORITY :
INSTANCES :
1.que[max] is a finite collection of elements associated with some priority.
PRECONDITIONS :
2.front and rear should be with in maximum size MAX .
3.check whether queue is full or not before insertion .
4.check whether queue is empty or not before deletion .
OPERATIONS :
5.creat().
6.insert().
7.delete().
8.display().
Program :-
/* Circular Queues */
#include<iostream.h>
#include<conio.h>
const int MAX = 5;
class cqueue
{
int a[MAX],front,rear;
public :
cqueue()
{
front=rear=-1;
}
void insert(int );
int deletion();
void display();
};
void cqueue :: insert(int val)
{
if((front==0 && rear==MAX-1) || (rear+1==front))
cout<<" Circular Queue is Full";
else
{
if(rear==MAX-1)
rear=0;
else
rear++;
a[rear]=val;
}
if(front==-1)
front=0;
}
int cqueue :: deletion()
{
int k;
if(front==-1)
cout<<"Circular Queue is Empty";
else
{
k=a[front];
if(front==rear)
front=rear=-1;
p
a
n
c
h
a
m
u
k
e
s
h
.
b
l
o
g
s
p
o
t
.
c
o
m
else
{
if(front==MAX-1)
front=0;
else
front++;
}
}
return k;
}
void cqueue :: display()
{
int i;
if(front==-1)
cout<<"Circular Queue is Empty";
else
{
if(rear < front)
{
for(i=front;i<=MAX-1;i++)
cout<<a[i]<<" ";
for(i=0;i<=rear;i++)
cout<<a[i]<<" ";
}
else
{
for(i=front;i<=rear;i++)
cout<<a[i]<<" ";
cout<<endl;
}
}
}
void main()
{
cqueue c1;
int ch,val;
char op;
do
{
clrscr();
cout<<"-----------Menu-------------";
cout<<"1.Insertion"<<endl;
cout<<"2.Deletion"<<endl;
cout<<"3.Display"<<endl;
cout<<"4.Exit"<<endl;
cout<<"Enter Your Choice <1..4> ?"<<endl;
cin>>ch;
switch(ch)
{
case 1 : cout<<"Enter Element to Insert ?";
cin>>val;
c1.insert(val);
break;
case 2 : val=c1.deletion();
cout<<"Deleted Element :"<<val<<endl;
break;
p
a
n
c
h
a
m
u
k
e
s
h
.
b
l
o
g
s
p
o
t
.
c
o
m
case 3 : c1.display();
break;
}
cout<<"Do you want to continue<Y/N> ?";
cin>>op;
}while(op=='Y' || op=='y');
getch();
}
/* Write C++ program that uses non-recursive functions to traverse a binary tree in Pre-order
*/
#include<iostream>
#include<conio.h>
#include<stdlib.h>
using namespace std;
class node
{
public:
class node *left;
class node *right;
int data;
};
class tree: public node
{
public:
int stk[50],top;
node *root;
tree()
{
root=NULL;
top=0;
}
void insert(int ch)
{
node *temp,*temp1;
if(root==NULL)
{
root=new node;
root->data=ch;
root->left=NULL;
root->right=NULL;
return;
}
temp1=new node;
temp1->data=ch;
temp1->right=temp1->left=NULL;
temp=search(root,ch);
if(temp->data>ch)
temp->left=temp1;
else
p
a
n
c
h
a
m
u
k
e
s
h
.
b
l
o
g
s
p
o
t
.
c
o
m
temp->right=temp1;
}
node *search(node *temp,int ch)
{
if(root==NULL)
{
cout <<"no node present";
return NULL;
}
if(temp->left==NULL && temp->right==NULL)
return temp;
if(temp->data>ch)
{ if(temp->left==NULL) return temp;
search(temp->left,ch);}
else
{ if(temp->right==NULL) return temp;
search(temp->right,ch);
} }
void display(node *temp)
{
if(temp==NULL)
return ;
display(temp->left);
cout<<temp->data <<" ";
display(temp->right);
}
void preorder( node *root)
{
node *p,*q;
p=root;
q=NULL;
top=0;
while(p!=NULL)
{
cout <<p->data <<" ";
if(p->right!=NULL)
{
stk[top]=p->right->data;
top++;
}
p=p->left;
if(p==NULL && top>0)
{
p=pop(root);
p
a
n
c
h
a
m
u
k
e
s
h
.
b
l
o
g
s
p
o
t
.
c
o
m
}
}
}
node * pop(node *p)
{
int ch;
ch=stk[top-1];
if(p->data==ch)
{
top--;
return p;
}
if(p->data>ch)
pop(p->left);
else
pop(p->right);
}
};
main()
{
tree t1;
int ch,n,i;
while(1)
{
cout <<"\n1.INSERT\n2.DISPLAY 3.PREORDER TRAVERSE\n4.EXIT\nEnter
your choice:";
cin >>ch;
switch(ch)
{
case 1: cout <<"enter no of elements to insert:";
cout<<"\n enter the elements";
cin >>n;
for(i=1;i<=n;i++)
{ cin >>ch;
t1.insert(ch);
}
break;
case 2: t1.display(t1.root);break;
case 3: t1.preorder(t1.root); break;
case 4: exit(1);
}
}
}
OUTPUT
1.INSERT
2.DISPLAY 3.PREORDER TRAVERSE
4.EXIT
Enter your choice:1
enter no of elements to insert
enter the elements7
5 24 36 11 44 2 21
p
a
n
c
h
a
m
u
k
e
s
h
.
b
l
o
g
s
p
o
t
.
c
o
m
1.INSERT
2.DISPLAY 3.PREORDER TRAVERSE
4.EXIT
Enter your choice:2
2 5 11 21 24 36 44
1.INSERT
2.DISPLAY 3.PREORDER TRAVERSE
4.EXIT
Enter your choice:3
5 2 24 11 21 36 44
1.INSERT
2.DISPLAY 3.PREORDER TRAVERSE
4.EXIT
Enter your choice:4
/* Write C++ program that uses non-recursive functions to traverse a binary tree in In-order */
#include<iostream>
#include<conio.h>
#include<stdlib.h>
using namespace std;
class node
{
public:
class node *left;
class node *right;
int data;
};
class tree: public node
{
public:
int stk[50],top;
node *root;
tree()
{
root=NULL;
top=0;
}
void insert(int ch)
{
node *temp,*temp1;
if(root==NULL)
{
root=new node;
root->data=ch;
root->left=NULL;
root->right=NULL;
return;
}
p
a
n
c
h
a
m
u
k
e
s
h
.
b
l
o
g
s
p
o
t
.
c
o
m
temp1=new node;
temp1->data=ch;
temp1->right=temp1->left=NULL;
temp=search(root,ch);
if(temp->data>ch)
temp->left=temp1;
else
temp->right=temp1;
}
node *search(node *temp,int ch)
{
if(root==NULL)
{
cout <<"no node present";
return NULL;
}
if(temp->left==NULL && temp->right==NULL)
return temp;
if(temp->data>ch)
{ if(temp->left==NULL) return temp;
search(temp->left,ch);}
else
{ if(temp->right==NULL) return temp;
search(temp->right,ch);
} }
void display(node *temp)
{
if(temp==NULL)
return ;
display(temp->left);
cout<<temp->data;
display(temp->right);
}
void inorder( node *root)
{
node *p;
p=root;
top=0;
do
{
while(p!=NULL)
{
stk[top]=p->data;
top++;
p=p->left;
p
a
n
c
h
a
m
u
k
e
s
h
.
b
l
o
g
s
p
o
t
.
c
o
m
}
if(top>0)
{
p=pop(root);
cout <<p->data;
p=p->right;
}
}while(top!=0 || p!=NULL);
}
node * pop(node *p)
{
int ch;
ch=stk[top-1];
if(p->data==ch)
{
top--;
return p;
}
if(p->data>ch)
pop(p->left);
else
pop(p->right);
}
};
main()
{
tree t1;
int ch,n,i;
while(1)
{
cout <<"\n1.INSERT\n2.DISPLAY 3.INORDER TRAVERSE\n4.EXIT\nEnter your
choice:";
cin >>ch;
switch(ch)
{
case 1: cout <<"enter no of elements to insert:";
cin >>n;
for(i=1;i<=n;i++)
{ cin >>ch;
t1.insert(ch);
}
break;
case 2: t1.display(t1.root);break;
case 3: t1.inorder(t1.root); break;
case 4: exit(1);
}
}
}
OUTPUT
1.INSERT
p
a
n
c
h
a
m
u
k
e
s
h
.
b
l
o
g
s
p
o
t
.
c
o
m
2.DISPLAY 3.INORDER TRAVERSE
4.EXIT
Enter your choice:1
enter no of elements to inser
5 24 36 11 44 2 21
1.INSERT
2.DISPLAY 3.INORDER TRAVERSE
4.EXIT
Enter your choice:3
251121243644
1.INSERT
2.DISPLAY 3.INORDER TRAVERSE
4.EXIT
Enter your choice:3
251121243644
1.INSERT
2.DISPLAY 3.INORDER TRAVERSE
4.EXIT
Enter your choice:4
/* Write C++ program that uses non-recursive functions to traverse a binary tree in Post-order
*/
#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
class node
{
public:
class node *left;
class node *right;
int data;
};
class tree: public node
{
public:
int stk[50],top;
node *root;
tree()
{
root=NULL;
top=0;
}
void insert(int ch)
{
node *temp,*temp1;
if(root==NULL)
{
p
a
n
c
h
a
m
u
k
e
s
h
.
b
l
o
g
s
p
o
t
.
c
o
m
root=new node;
root->data=ch;
root->left=NULL;
root->right=NULL;
return;
}
temp1=new node;
temp1->data=ch;
temp1->right=temp1->left=NULL;
temp=search(root,ch);
if(temp->data>ch)
temp->left=temp1;
else
temp->right=temp1;
}
node *search(node *temp,int ch)
{
if(root==NULL)
{
cout <<"no node present";
return NULL;
}
if(temp->left==NULL && temp->right==NULL)
return temp;
if(temp->data>ch)
{ if(temp->left==NULL) return temp;
search(temp->left,ch);}
else
{if(temp->right==NULL) return temp;
search(temp->right,ch);
} }
void display(node *temp)
{
if(temp==NULL)
return ;
display(temp->left);
cout<<temp->data <<" ";
display(temp->right);
}
void postorder( node *root)
{
node *p;
p=root;
top=0;
while(1)
{
while(p!=NULL)
{
p
a
n
c
h
a
m
u
k
e
s
h
.
b
l
o
g
s
p
o
t
.
c
o
m
stk[top]=p->data;
top++;
if(p->right!=NULL)
stk[top++]=-p->right->data;
p=p->left;
}
while(stk[top-1] >0 || top==0)
{
if(top==0) return;
cout <<stk[top-1] <<" ";
p=pop(root);
}
if(stk[top-1]<0)
{
stk[top-1]=-stk[top-1];
p=pop(root);
} }
}
node * pop(node *p)
{
int ch;
ch=stk[top-1];
if(p->data==ch)
{
top--;
return p;
}
if(p->data>ch)
pop(p->left);
else
pop(p->right);
}
};
void main()
{
tree t1;
int ch,n,i;
clrscr();
while(1)
{
cout <<"\n1.INSERT\n2.DISPLAY 3.POSTORDER
TRAVERSE\n4.EXIT\nEnter your choice:";
cin >>ch;
switch(ch)
{
case 1: cout <<"enter no of elements to insert:";
cout<<"\n enter the elements";
cin >>n;
for(i=1;i<=n;i++)
{ cin >>ch;
t1.insert(ch);
p
a
n
c
h
a
m
u
k
e
s
h
.
b
l
o
g
s
p
o
t
.
c
o
m
}
break;
case 2: t1.display(t1.root);break;
case 3: t1.postorder(t1.root); break;
case 4: exit(1);
}
}
}
OUTPUT
1.INSERT
2.DISPLAY 3.POSTORDER TRAVERSE
4.EXIT
Enter your choice:1
enter no of elements to insert:
enter the elements7
5 24 36 11 44 2 21
1.INSERT
2.DISPLAY 3.POSTORDER TRAVERSE
4.EXIT
Enter your choice:2
2 5 11 21 24 36 44
1.INSERT
2.DISPLAY 3.POSTORDER TRAVERSE
4.EXIT
Enter your choice:3
2 21 11 44 36 24 5
1.INSERT
2.DISPLAY 3.POSTORDER TRAVERSE
4.EXIT
Enter your choice:4

Vous aimerez peut-être aussi