Docstoc

pro7

Document Sample
pro7 Powered By Docstoc
					#include<stdio.h>
#include<conio.h>
#include<alloc.h>
typedef struct btree
{
      int data;
      struct btree *left,*right;
}*tree;
tree head;
void main()
{
      int ch=0,n,val;
      void create();
      void display();
      clrscr();
      printf("\n\n\t\t\t BINARY TREE TRAVERSALS\n");
      while(1)
      {
            printf("\n\n 1-CREATE\t2-DIAPLAY\t3-EXIT");
            printf("\n\nyour option ");
            scanf("%d",&ch);
            switch(ch)
            {
                  case 1:     create();
                        break;
                  case 2:     display();
                        break;
                  case 3:     exit(0);
            }
      }
}
tree getnewnode()
{
      tree temp;
      printf("\n ENTER THE DATA:    ");
      temp=(tree)malloc(sizeof(struct btree));
      scanf("%d",&temp->data);
      temp->left=0;
      temp->right=0;
      return(temp);
}
void inorder(tree root)
{
      tree temp;
      temp=root;
      if(temp)
      {
            inorder(temp->left);
            printf("\t%d",temp->data);
            inorder(temp->right);
      }
}
void preorder(tree root)
{
     tree temp;
     temp=root;
     if(temp)
     {
           printf("\t%d",temp->data);
           preorder(temp->left);
           preorder(temp->right);
     }
}
void postorder(tree root)
{
      tree temp;
      temp=root;
if(temp)
      {
            postorder(temp->left);
            postorder(temp->right);
            printf("\t%d",temp->data);
      }
}
void create()
{
      tree temp,newnode;
      int flag=0,n;
      do
      {
            temp=head;
            if(temp->left==NULL)
            {
                  temp->left=getnewnode();
                  temp=temp->left;
            }
            else
                  temp=temp->left;
                  flag=0;
                  newnode=getnewnode();
            while(flag==0)
            {
                  if(newnode->data<temp->data)
                  {
                  if(temp->left==NULL)
                  {
                        temp->left=newnode;
                        printf("\n%d is attached to the left of %d",
                        newnode->data,temp->data);
                        flag=1;
                  }
                  else
                        temp=temp->left;
                  }
                  else
                  {
                  if(temp->right==NULL)
                  {
                       temp->right=newnode;
                       printf("%d is attached to right of %d\n",
                       newnode->data,temp->data);
                       flag=1;
                 }
                 else
                 temp=temp->right;
           }
     }
     printf("\n\n DO YOU WANT TO CONTINUE Y/N");
     }
     while(getch()=='Y');
}
void display()
{
      printf("\n\n IN ORDER :");
      inorder(head->left);
      printf("\n\n PRE ORDER :");
      preorder(head->left);
      printf("\n\nPOST ORDER :");
      postorder(head->left);
}

				
DOCUMENT INFO
Shared By:
Categories:
Tags:
Stats:
views:0
posted:10/18/2012
language:English
pages:3