Skip to content

Instantly share code, notes, and snippets.

@anshumanatri
Created March 12, 2009 10:13
Show Gist options
  • Save anshumanatri/77993 to your computer and use it in GitHub Desktop.
Save anshumanatri/77993 to your computer and use it in GitHub Desktop.
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#define null 0
struct btree
{
int data;
struct btree * left;
struct btree * right;
};
typedef struct btree * node;
void inorder(node tre);
void preorder(node tre);
void postorder(node tre);
node avail()
{
node p=(node)malloc(sizeof(struct btree));
printf("\n Enter the data:\t");
scanf("%d",&p->data);
p->left=null;
p->right=null;
return(p);
}
void main()
{
int count,n;
char ch='y';
node tree=null,cur,p;
clrscr();
tree=avail();
while(ch=='y'||ch=='Y')
{
count=1;
cur=tree;
p=avail();
while(count==1)
{
if(p->data>=cur->data)
{
if(cur->right==null)
{
cur->right=p;
count=0;
}
else
cur=cur->right;
}
else
if(p->data<cur->data)
{
if(cur->left==null)
{
cur->left=p;
count=0;
}
else
cur=cur->left;
}
}
printf("\n press y to continue insertion");
scanf("%s",&ch);
}
printf("\n inorder:\t");
inorder(tree);
printf("\npreorder:\t");
preorder(tree);
printf("\npostorder:\t");
postorder(tree);
getch();
}
void inorder(node tre)
{
if(tre!=null)
{
inorder(tre->left);
printf("%d\t",tre->data);
inorder(tre->right);
}
}
void preorder(node tre)
{
if(tre!=null)
{
printf("%d\t",tre->data);
preorder(tre->left);
preorder(tre->right);
}
}
void postorder(node tre)
{
if(tre!=null)
{
postorder(tre->left);
postorder(tre->right);
printf("%d\t",tre->data);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment