Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
7 views

Tree

The document contains C code to implement a binary tree data structure. It includes functions to create and traverse a binary tree using inorder, preorder and postorder traversal algorithms.

Uploaded by

GR R
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

Tree

The document contains C code to implement a binary tree data structure. It includes functions to create and traverse a binary tree using inorder, preorder and postorder traversal algorithms.

Uploaded by

GR R
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

#include<stdio.

h>
#include<stdlib.h>
struct node
{
char info;
struct node *lchild;
struct node *rchild;
};
node *root;
int tag=0;
void inorder(node *);
void preorder(node *);
void postorder(node *);
void create(node *);
node *getnode();
int main()
{
int ch;
do
{
if (tag==0)
{
printf("\n Enter the root node ");
root=getnode();
create(root);
tag=1;
ch=0;
}
else
{
printf("\n 1. Inorder");
printf("\n 1. Preoder");
printf("\n 1. Postorder");
printf("\n 1. Enter your choice");

fflush(stdin);
scanf("%d",&ch);
switch(ch)
{
case 1:
inorder(root);
break;
case 2:
preorder(root);
break;
case 3:
postorder(root);
break;
case 4:
break;
}

}
} while(ch!=4);
return 0;
}
node *getnode()
{
node *t;
t=(node*)malloc(sizeof(node));
fflush(stdin);
scanf("%c",&t->info);
t->lchild=NULL;
t->rchild=NULL;
return(t);
}

void create(node *t)


{
node *m;
printf("\n Enter the left child of %c ",t->info);
m=getnode();
if(m->info!='\n')
{
t->lchild=m;
create(m);
}
else
{
t->lchild=NULL;
}
printf("\n Enter the right child of %c ",t->info);
m=getnode();
if(m->info!='\n')
{
t->rchild=m;
create(m);
}
else
{
t->rchild=NULL;
}
}

void inorder(node *t)


{
if (t!=NULL)
{
inorder(t->lchild);
printf("%c",t->info);
inorder(t->rchild);
}
}
void preorder(node *t)
{
if (t!=NULL)
{
printf("%c",t->info);
preorder(t->lchild);
preorder(t->rchild);
}
}
void postorder(node *t)
{
if (t!=NULL)
{
postorder(t->lchild);
postorder(t->rchild);
printf("%c",t->info);
}
}

You might also like