Binary search tree

Write a Program to implement a binary search tree.                           

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
struct node
{
 int data;
 struct node *left;
 struct node *right;
};
struct node *tree;

struct node *insertElement(struct node *,int);
void preorderTraversal(struct node*);
void inorderTraversal(struct node *);
void postorderTraversal(struct node*);
void main()
{
int option,val;
struct node *ptr;
clrscr();
do
{
printf("\n*****************MAIN MENU****************\n");
printf("\n1.Insert Element");
printf("\n2.Preorder traversal");
printf("\n3.Inodere Traversal");
printf("\n4.Postorder Traversal");
printf("\n\n***********************************************");
printf("\n\nEnter Your Option");
scanf("%d",&option);
switch(option)
{
case 1: printf("\n Enter the value of the new node :");
       scanf("%d",&val);
       tree=insertElement(tree,val);
       break;
case 2:printf("\n the Element of the tree are :\n");
       preorderTraversal(tree);
       break;
case 3:printf("\n the elements f the tree are:");
       inorderTraversal(tree);
       break;
case 4:printf("\n the element of tree are:\n");
       postorderTraversal(tree);
       break;
}
}
while(option!=5);
getch();
}
struct node *insertElement(struct node *tree,int val)
{
struct node *ptr,*nodeptr,*parentptr;
ptr=(struct node *)malloc(sizeof(struct node));
ptr->data=val;
ptr->left=NULL;
ptr->right=NULL;
if(tree==NULL)
{
tree=ptr;
tree->left=NULL;
tree->right=NULL;
}
else
{
parentptr=NULL;
nodeptr=tree;
while(nodeptr!=NULL)
{
 parentptr=nodeptr;
 if(val<nodeptr->data)
 nodeptr=nodeptr->left;
 else
 nodeptr=nodeptr->right;
}
if(val<parentptr->data)
{
parentptr->left=ptr;
}
else
{
parentptr->right=ptr;
}
}
return tree;
}
void preorderTraversal(struct node *tree)
{
if(tree!=NULL)
{
printf("%d \t",tree->data);
preorderTraversal(tree->left);
preorderTraversal(tree->right);
}
}
void inorderTraversal(struct node *tree)
{
if(tree!=NULL)
{
inorderTraversal(tree->left);
printf("%d \t",tree->data);
inorderTraversal(tree->right);
}
}
void postorderTraversal(struct node *tree)
{
if(tree!=NULL)
{
postorderTraversal (tree->left);
postorderTraversal (tree->right);
printf("%d \t",tree->data);
}
}
  

OUTPUT:

Comments

Popular posts from this blog

Sequential File Allocation

Indexed File Allocation method

Linked File Allocation