0% found this document useful (0 votes)
28 views6 pages

Treetraversal

The document contains C code to implement binary search tree operations like insertion, inorder, preorder and postorder tree traversals. It defines a node structure with left and right child pointers, includes header files, declares functions for insertion and different tree traversals and contains a main function to take user input for operation and display output.

Uploaded by

SHIVALKAR J
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
28 views6 pages

Treetraversal

The document contains C code to implement binary search tree operations like insertion, inorder, preorder and postorder tree traversals. It defines a node structure with left and right child pointers, includes header files, declares functions for insertion and different tree traversals and contains a main function to take user input for operation and display output.

Uploaded by

SHIVALKAR J
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 6

PROGRAM CODING:

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<alloc.h>
struct tree
{ int data;
struct tree *right,*left;
};
typedef struct tree node;
node *t=NULL;
node* insert(int x,node *t)
{
if(t==NULL)
{
t=(node*)malloc(sizeof(node));
t->data=x;
t->left=NULL;
t->right=NULL;
}
else
if(x<t->data)
t->left=insert(x,t->left);
else
if(x>t->data)
t->right=insert(x,t->right);
return t;
}
void inorder(node *t)
{
if(t!=NULL)
{
inorder(t->left);
printf(" %d",t->data);
inorder(t->right);
}
}
void preorder(node *t)
{
if(t!=NULL)
{
printf(" %d",t->data);
preorder(t->left);
preorder(t->right);
}
}

void postorder(node *t)


{
if(t!=NULL)
{
postorder(t->left);
postorder(t->right);
printf(" %d",t->data);
}
}

void main()
{
int data,ch,s;
char c;
while(1)
{
clrscr();
printf("\n1.Insert");
printf("\n2.Inorder Traversal");
printf("\n3.Preorder Traversal");
printf("\n4.Postorder Traversal");
printf("\n5.Exit");
printf("\nEnter your choice:");
scanf("%d",&ch);
switch(ch)
{ case 1: printf("\nEnter data to be inserted:");
scanf("%d",&data);
t=insert(data,t);
break;
case 2: printf("\nThe Inorder Display:\n");
inorder(t);

break;
case 3: printf("\nThe Preorder Display:\n");
preorder(t);

break;
case 4: printf("\nThe Postorder Display:\n");
postorder(t);

break;
case 5: exit(0);
default:printf("\nEnter valid option");

}
getch();
}
}
OUTPUT:

You might also like