Sunday, August 28, 2011

Binary tree | Data Structures

#include<iostream>
using namespace std;

struct tree
{
 int data;
 struct tree *left;
 struct tree *right;
}*root=NULL;

typedef struct stack node;

node *insert(node *tree,int num);
void preorder(node *tree);
void inorder(node *tree);
void postorder(node *tree);





main()
{

   int choice;
   do
  {
   cout<<"\n 1. Push";
   cout<<"\n 2. Pop ";
   cout<<"\n 3.Display";
   cout<<"\n Enter the choice :";
   cin>>choice;
  
  switch(choice)
 
  {
    case 1 :  push();
              break;
   
    case 2 :  pop();
              break;
   
    case 3 :  display();
              break;
   
    default : cout<<"\n Wrong choice! ";
              break;
  }

 }while(choice>0&&choice<4);

}

void push()
{  

  int item;
  node *temp;
 // temp=new node;
  cout<<"\n Enter the item to insert ";
  cin>>temp->data;
  temp->next=top;
  top=temp;

}

void pop()
{
  if(top==NULL)
    cout<<"\n No item to pop ";
  else
 { 
  node *temp;
  temp=top;
  cout<<"\n item popped is "<<temp->data;
  top=top->next;
  delete temp;
 }
}

void display()
{
  node *temp;

 if(top==NULL)
    cout<<"\n No item to display ";
else
 {
   temp=top;
   while(temp->next!=NULL)
   {
     cout<<"\n "<<temp->data;
     temp=temp->next;
   }
  cout<<"\n "<<temp->data;
 }
}
 

No comments:

Post a Comment