Advertisement
Sathvikks8

bst1

Dec 18th, 2020
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.74 KB | None | 0 0
  1. #include<stdio.h>
  2.  
  3. #include<stdlib.h>
  4.  
  5. struct node {
  6.   struct node * lchild;
  7.   int info;
  8.   struct node * rchild;
  9. };
  10. typedef struct node * NODE;
  11. NODE temp = NULL, cur = NULL, prev = NULL, root = NULL;
  12.  
  13. NODE getnode() {
  14.   NODE x;
  15.   x = (struct node * ) malloc(sizeof(struct node));
  16.   x -> lchild = NULL;
  17.   x -> rchild = NULL;
  18.   return x;
  19. }
  20.  
  21. NODE insert(int item, NODE root) {
  22.   NODE temp, cur, prev;
  23.   temp = getnode();
  24.   temp -> info = item;
  25.   if (root == NULL) {
  26.     root = temp;
  27.     return root;
  28.   } else {
  29.     prev = NULL;
  30.     cur = root;
  31.     while (cur != NULL) {
  32.       prev = cur;
  33.       cur = (temp -> info < cur -> info) ? cur -> lchild : cur -> rchild;
  34.     }
  35.     if (temp -> info < prev -> info)
  36.       prev -> lchild = temp;
  37.     else if (temp -> info > prev -> info)
  38.       prev -> rchild = temp;
  39.   }
  40.   return root;
  41. }
  42. void inorder(NODE ptr1) {
  43.   if (ptr1 != NULL) {
  44.     inorder(ptr1 -> lchild);
  45.     printf("%d\t", ptr1 -> info);
  46.     inorder(ptr1 -> rchild);
  47.   }
  48. }
  49.  
  50. void preorder(NODE ptr2) {
  51.   if (ptr2 != NULL) {
  52.     printf("%d\t", ptr2 -> info);
  53.     preorder(ptr2 -> lchild);
  54.     preorder(ptr2 -> rchild);
  55.   }
  56.  
  57. }
  58.  
  59. void postorder(NODE ptr3) {
  60.   if (ptr3 != NULL) {
  61.     postorder(ptr3 -> lchild);
  62.     postorder(ptr3 -> rchild);
  63.     printf("%d\t", ptr3 -> info);
  64.   }
  65.  
  66. }
  67.  
  68. void search(NODE root) {
  69.   int item, found = 0;
  70.   NODE cur;
  71.   printf("enter the element to be searched\n");
  72.   scanf("%d", &item);
  73.   if (root == NULL)
  74.   {
  75.     printf("tree is empty\n");
  76.     return;
  77.   }
  78.   cur = root;
  79.   while (cur != NULL) {
  80.     printf("\nComparing key(%d) : cur(%d)",item,cur->info);
  81.     if (item == cur -> info) {
  82.       printf("\nHere");
  83.       printf("found key %d in tree \n", cur -> info);
  84.       return;
  85.     }
  86.     if (item < cur -> info)
  87.       cur = cur -> lchild;
  88.     else
  89.       cur = cur -> rchild;
  90.   }
  91.     printf("key not found\n");
  92. }
  93. int main() {
  94.   int choice, item, n, i;
  95.   NODE root = NULL;
  96.   while (1) {
  97.     printf("1.create BST of N integers\t 2.BST traversal\n");
  98.     printf("3. binary search\t 4.exit\n");
  99.     printf("enter your choice:");
  100.     scanf("%d", & choice);
  101.     switch (choice) {
  102.     case 1:
  103.       printf("\n enter the nuber of elements:");
  104.       scanf("%d", & n);
  105.       for (i = 0; i < n; i++) {
  106.         printf("enter the item to be inserted\n");
  107.         scanf("%d", &item);
  108.         root = insert(item, root);
  109.       }
  110.       break;
  111.     case 2:
  112.       if (root == NULL)
  113.         printf("\ntree is empty\n");
  114.       else {
  115.         printf("\ninorder traversal");
  116.         inorder(root);
  117.         printf("\n preorder traversal");
  118.         preorder(root);
  119.         printf("\n postorder traversal");
  120.         postorder(root);
  121.       }
  122.       break;
  123.     case 3:
  124.       search(root);
  125.       break;
  126.     case 4:
  127.       exit(0);
  128.       break;
  129.     default:
  130.       printf("invalid choice\n");
  131.       break;
  132.     }
  133.   }
  134. }
  135.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement