#include<stdio.h> #include<conio.h> #include<malloc.h> struct node { int data; struct node *right, *left; }; struct node *nw, *root, *temp; void creation(struct node *root, struct node *nw) { if(nw -> data < root-> data) { if(root -> left == NULL) root -> left = nw; else creation(root->left, nw); } if(nw -> data > root -> data) { if(root -> right == NULL) root -> right = nw; else creation(root -> right , nw); } } void sort_a(struct node *temp) { if(temp != NULL) { sort_a(temp -> left); printf(" %d ",temp->data); sort_a(temp -> right); } } void main() { int ch,item; root = NULL; do { clrscr(); printf("1. Creation \n2. Sorting \n3. Exit\n\nEnter Your Choice : "); scanf("%d",&ch); switch(ch) { case 1: { printf("Enter Item : "); scanf("%d",&item); nw= (struct node *)malloc(sizeof(struct node)); nw -> data = item; nw -> left = NULL; nw -> right = NULL; if(root == NULL) root = nw; else creation(root,nw); } break; case 2: sort_a(root); getch(); break; } }while(ch != 3); }
OUTPUT 1. Creation 2. Searching 3. Inorder 4. Preorder 5. Postorder 6. Exit Enter Your Choice : 1 Enter Element : 50 1. Creation 2. Searching 3. Inorder 4. Preorder 5. Postorder 6. Exit Enter Your Choice : 1 Enter Element : 30 1. Creation 2. Searching 3. Inorder 4. Preorder 5. Postorder 6. Exit Enter Your Choice : 1 Enter Element : 70 1. Creation 2. Searching 3. Inorder 4. Preorder 5. Postorder 6. Exit Enter Your Choice : 1 Enter Element : 10 1. Creation 2. Searching 3. Inorder 4. Preorder 5. Postorder 6. Exit Enter Your Choice : 1 Enter Element : 40 1. Creation 2. Searching 3. Inorder 4. Preorder 5. Postorder 6. Exit Enter Your Choice : 1 Enter Element : 60 1. Creation 2. Searching 3. Inorder 4. Preorder 5. Postorder 6. Exit Enter Your Choice : 1 Enter Element : 80 1. Creation 2. Searching 3. Inorder 4. Preorder 5. Postorder 6. Exit Enter Your Choice : 2 10 30 40 50 60 70 80