Jan

23

#include<stdio.h>

struct node{

int num;

struct node *left;

struct node *right;

};

typedef struct node NODE;

NODE *root=NULL;

NODE *insert(NODE *tree, int num);

void preorder(NODE *tree);

void inorder(NODE *tree);

void postorder(NODE *tree);

int count=1;

void main(){

int choice;

int digit;

clrscr();

do{

printf(“\nEnter 1 : Insert a node in the BT”);

printf(“\nEnter 2 : Display (preorder) the BT”);

printf(“\nEnter 3 : Display (inorder) the BT”);

printf(“\nEnter 4 : Display (postorder) the BT”);

printf(“\nEnter 5 : END\n”);

scanf(“\n %d”,&choice);

switch(choice){

case 1: printf(“Enter ineger: To quit enter 0:: “);

scanf(“%d”,&digit);

while(digit !=0){

root=insert(root,digit);

scanf(“%d”,&digit);

}

break;

case 2: printf(“\n Preorder traversing TREE”);

preorder(root);

break;

case 3: printf(“\n Inorder traversing TREE”);

inorder(root);

break;

case 4: printf(“\n Postorder traversing TREE”);

postorder(root);

break;

case 5: printf(“\n END”);exit(0);

}

}while(choice>=1 && choice<=5);

}

NODE *insert(NODE *p,int digit){

if(p==NULL){

p=(NODE*) malloc(sizeof(NODE));

p->left=p->right=NULL;

p->num=digit;

}

else{

if(digit<=p->num)

p->left=insert(p->left,digit);

else

p->right=insert(p->right,digit);

}

return(p);

}

void preorder(NODE *p){

if(p!=NULL){

printf(“\n%d”,p->num);

preorder(p->left);

preorder(p->right);

}

}

void inorder(NODE *p){

if(p!=NULL){

inorder(p->left);

printf(“\n%d”,p->num);

inorder(p->right);

}

}

void postorder(NODE *p){

if(p!=NULL){

postorder(p->left);

postorder(p->right);

printf(“\n%d”,p->num);

}

}


Leave a Reply