Escolar Documentos
Profissional Documentos
Cultura Documentos
#include<iostream.h>
#include<iomanip.h>
#include<conio.h>
#include<stdio.h>
#include<process.h>
struct Node
{
Node *Left ;
int data;
Node *Right;
};
struct Snode
{
NodePtr ele ;
Snode *next ;
};
class Stack
{
private:
Snode *top;
public :
Stack()
{
top = NULL ;
}
void Push(NodePtr P)
{
Snode *temp;
temp = new Snode;
temp -> ele = P ;
temp -> next = top ;
top = temp;
}
void Pop()
{
if( top != NULL )
{
NodePtr t;
Snode *temp;
temp = top;
top = top -> next ;
delete temp;
}
}
NodePtr Top()
{
if( top != NULL ) return top -> ele ;
else return NULL ;
}
int IsEmpty()
{
return ( ( top == NULL) ? 1 : 0 ) ;
}
};
class BST
{
public :
void Insert(int,NodePtr &);
void Delete(int,NodePtr &);
int DeleteMin(NodePtr &);
void Find(int,NodePtr &);
void Ances(int,NodePtr &);
void Desc(int,NodePtr &);
void PreOrder(NodePtr);
void InOrder(NodePtr);
void PostOrder(NodePtr);
void PreOrderNR(NodePtr);
void InOrderNR(NodePtr);
void PostOrderNR(NodePtr);
};
void BST :: Insert( int x , NodePtr &P)
{
if( P == NULL )
{
P = new Node;
P -> data = x ;
P -> Left = P -> Right = NULL ;
cout << "\n\n\tElement " << x << " Is Inserted Successfully" ;
}
else
{
if( x < P -> data ) Insert( x , P -> Left ) ;
else if( x > P -> data ) Insert( x , P -> Right) ;
else cout << "\n\n\t\tElement Already Exists !" ;
}
}
}
}
void BST :: InOrderNR(NodePtr P)
{
Stack S;
while(1)
{
if( P != NULL )
{
S.Push(P);
P = P -> Left ;
}
else if(S.IsEmpty())
return;
else
{
NodePtr Temp;
Temp = S.Top();
cout << setw(5) << Temp -> data ;
P = Temp -> Right ;
S.Pop();
}
}
}
void BST :: PostOrderNR( NodePtr P )
Stack S;
while(1)
{
if( P != NULL )
{
S.Push(P);
P = P -> Left ;
}
else
{
if( S.IsEmpty() )
return ;
if(!S.IsEmpty())
else
P = NULL ;
}
}
}
void main( )
{
BST B;
NodePtr Root = NULL;
int ele ;
char ch;
clrscr( );
while(1)
{
cout << "\n\nBINARY SEARCH TREE\n----------------------------" ;
cout << "\n1 . Insertion" ;
cout << "\n2 . Deletion" ;
cout << "\n3 . Find" ;
cout << "\n4 . Preorder" ;
cout << "\n5 . Inorder" ;
cout << "\n6 . Postorder" ;
cout << "\n7 . Exit" ;
cout << "\n\n\t\tEnter Ur Choice :: " ;
fflush(stdin);
ch = getchar( );
switch(ch)
{
case '1':
cout << "\n\nEnter Data Part Of The Node :: " ;
cin >> ele ;
B.Insert( ele , Root );
cout << "\n\n\t\tThe Inorder Traversal Is\n\n" ;
B.InOrder(Root);
break;
case '2':
cout << "\n\nEnter An Element That Is To Be Deleted ::" ;
cin >> ele ;
B.Delete( ele , Root );
cout << "\n\n\t\tThe Inorder Traversal Is\n\n" ;
B.InOrder(Root);
break;
case '3':
cout << "\n\nEnter An Element To Search ::" ;
cin >> ele ;
B.Find( ele , Root );
cout << "\n\n\t\tThe Ancestors Are \n\n" ;
B.Ances( ele , Root );
cout << "\n\n\t\tThe Descendants Are \n\n" ;
B.Desc( ele , Root );
break;
case '4':
cout << "\n\nPREORDER" ;
if ( Root == NULL )
cout << "\n\n\t\tThe Tree Is Empty" ;
else
{
cout << "\nPreorder Traversal (Recursive) :: " ;
B.PreOrder(Root);
cout << "\nPreorder Traversal (Non Recursive) :: " ;
B.PreOrderNR(Root);
}
break ;
case '5':
cout << "\n\nINORDER" ;
if ( Root == NULL )
cout << "\n\n\t\tThe Tree Is Empty" ;
else
{
cout << "\nInorder Traversal (Recursive) :: " ;
B.InOrder(Root);
cout << "\nInorder Traversal (Non Recursive) :: " ;
B.InOrderNR(Root);
}
break ;
case '6':
cout << "\n\nPOSTORDER" ;
if ( Root == NULL )
cout << "\n\n\t\tThe Tree Is Empty" ;
else
{
cout << "\nPostorder Traversal (Recursive) :: " ;
B.PostOrder(Root);
cout << "\nPostorder Traversal (Non Recursive) :: " ;
B.PostOrderNR(Root);
}
break ;
default : exit(0);
}
}
}
Observations :
Element Is Found
PREORDER
Preorder Traversal (Recursive) ::
100 50 25 75 150 125 175
Preorder Traversal (Non Recursive) ::
100 50 25 75 150 125 175
INORDER
Inorder Traversal (Recursive) ::
25 50 75 100 125 150 175
Inorder Traversal (Non Recursive) ::
25 50 75 100 125 150 175