F Binary Search Tree Creation And Traversal Using Recursion Technique In C | CodeTheta

Binary Search Tree Creation And Traversal Using Recursion Technique In C

December 21, 2015


#include<stdio.h>
#include<conio.h>
#include<alloc.h>

struct node
{
 struct node *left;
 int DATA;
 struct node *right;
};

void insert();
void In-Oreder();
void Pre-Oreder();
void Post-Oreder();

void main()
{
 struct node *Binary_Tree;
 int x;
 clrscr();

 printf("\n\n\t ENTER A NUMBER ( 0 TO STOP ) :: ");
 scanf("%d",x);

 while(x!=0)
 {
  insert(&Binary_Tree,x);
  printf("\n\n\t ENTER A NUMBER ( 0 TO STOP ) :: ");
  scanf("%d",&x);
 }

 printf("\n\n\t IN-ORDER TRAVERSAL :: ");
 In-Oreder(Binary_Tree);

 printf("\n\n\t PRE-ORDER TRAVERSAL :: ");
 Pre-Oreder(Binary_Tree);

 printf("\n\n\t POST-ORDER TRAVERSAL :: ");
 Post-Oreder(Binary_Tree);

getch();
}

void insert(struct node **root,int n)
{
 if(*root==NULL)
 {
  *root=malloc(sizeof(struct node));

  (*root)->left=NULL;
  (*root)->DATA=n;
  (*root)->right=NULL;

  return;
 }
 else
 {
  if(n<(*root)->DATA)
    insert(&((*root)->left),n);
  else
    insert(&((*root)->right),n);
 }
 return;
}



void In-Oreder(struct node *root)
{
 if(root!=NULL)
 {
  In-Oreder(root->left);
  printf("%d ",root->right);
  In-Oreder(root->right);
 }
 else
 return;
}

void Pre-Oreder(struct node *root)
{
 if(root!=NULL)
 {
  printf("%d ",root->DATA);
  Pre-Oreder(root->left);
  Pre-Oreder(root->right);
 }
 else
 return;
}

void Post-Oreder(struct node *root)
{
 if(root!=NULL)
 {
  Post-Oreder(root->left);
  Post-Oreder(root->right);
  printf("%d ",root->DATA);
 }
 else
 return;
}

Try this code in your computer for better understanding. Enjoy the code. If you have any Question you can contact us or mail us.We will reply you as soon as possible.

1 comments:

This comment has been removed by a blog administrator.

Post a Comment