0% found this document useful (0 votes)
48 views5 pages

14 - WAP To Implement Binary Tree Traversal (Inorder, Preoder and Postorder)

The document contains code to implement traversal of a binary tree in C including preorder, inorder, and postorder traversal. It defines a node structure with left, right, and data pointers. Functions are included to insert nodes, traverse the tree preorder, inorder, and postorder, and a menu driver is provided to test the different traversal methods.

Uploaded by

gyugam
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
48 views5 pages

14 - WAP To Implement Binary Tree Traversal (Inorder, Preoder and Postorder)

The document contains code to implement traversal of a binary tree in C including preorder, inorder, and postorder traversal. It defines a node structure with left, right, and data pointers. Functions are included to insert nodes, traverse the tree preorder, inorder, and postorder, and a menu driver is provided to test the different traversal methods.

Uploaded by

gyugam
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

// 14 .

WAP to implement Binary Tree traversal ( Inorder , Preoder and


Postorder )

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

struct node
{
long num;
struct node *left;
struct node *right;
} ;

struct node *tree=NULL;
struct node *insert(struct node *tree,long num);
void preorder(struct node *tree);
void inorder(struct node *tree);
void postorder(struct node *tree);
int count=1;

main()

{
int choice;
long digit;

do

{
printf("\n\n\t MENU ");
printf("\n\n Enter 1: Insert a node in the BT ");
printf("\n\n Enter 2: Display ( preorder ) the BT ");
printf("\n\n Enter 3: Display ( inorder ) the BT");
printf("\n\n Enter 4: Display ( postorder ) the BT");
printf("\n\n Enter 5: END");
printf("\n\n Enter your choice : ");
scanf("%d" , &choice );

switch(choice )
{

case 1 : printf(" \n\n Enter integer : To quit enter 0 \n\n\t ");
scanf("%ld",&digit);
while(digit!=0)
{
tree=insert(tree,digit);
scanf("%ld",&digit);
}
continue;

case 2 : printf("\n Preorder traversing TREE :\n\n");
preorder(tree);
continue;

case 3 : printf("\n Inorder traversing TREE :\n\n");
inorder(tree);
continue;

case 4 : printf("\n Postorder traversing TREE :\n\n");
postorder(tree);
continue;

case 5 : printf("\n\n END");
getch();
exit(0);
break ;
}
} while(choice!=5);
}
// End of main( )

node *insert(struct node *tree,long digit)
{
if(tree==NULL)
{
tree=(struct node *)malloc(sizeof(struct node));
tree->left=tree->right=NULL;
tree->num=digit;
count++;
}

else
if(count%2==0)
tree->left=insert(tree->left,digit);

else
tree->right=insert(tree->right,digit);

return(tree) ;
}

void preorder(struct node *tree)
{
if(tree!=NULL)
{
printf("%12ld\n",tree->num);
preorder(tree->left);
preorder(tree->right);
}
}


void inorder(struct node *tree)
{
if(tree!=NULL)
{
inorder(tree->left);
printf("%12ld\n",tree->num);
inorder(tree->right);
}
}

void postorder(struct node *tree)
{
if(tree!=NULL)
{
postorder(tree->left);
postorder(tree->right);
printf("%12ld\n",tree->num);
}

}

You might also like