Advertisement
Sathvikks8

BST

Dec 18th, 2020
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.91 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. typedef struct node * BST;
  4. struct node
  5. {
  6.   struct node *leftChild;
  7.   int data;
  8.   struct node *rightChild;
  9. };
  10. BST temp=NULL, ptr=NULL, root=NULL, prev=NULL;
  11. BST newNode()
  12. {
  13.   BST X;
  14.   X=(struct node *)malloc(sizeof(struct node));
  15.   X->leftChild=X->rightChild=NULL;
  16.   return X;
  17. }
  18. void createBST(int elem)
  19. {
  20.   temp=newNode();
  21.   temp->data=elem;
  22.   if(!root)
  23.     root=temp;
  24.   else
  25.   {
  26.     prev=NULL;
  27.     ptr=root;
  28.     while(ptr)
  29.     {
  30.       prev=ptr;
  31.       ptr=(ptr->data<temp->data)?ptr->rightChild:ptr->leftChild;
  32.       if(prev->data==elem) // remove duplicate entries
  33.       {
  34.         free(temp);
  35.         return;
  36.       }
  37.     }
  38.     if(prev->data<temp->data)
  39.       prev->rightChild=temp;
  40.     else if(prev->data>temp->data)
  41.       prev->leftChild=temp;
  42.   }
  43. }
  44. void inOrder(BST ptr)
  45. {
  46.   if(ptr)
  47.   {
  48.     inOrder(ptr->leftChild);
  49.     printf("%d ",ptr->data);
  50.     inOrder(ptr->rightChild);
  51.   }
  52. }
  53. void preOrder(BST ptr)
  54. {
  55.   if(ptr)
  56.   {
  57.     printf("%d ",ptr->data);
  58.     preOrder(ptr->leftChild);
  59.     preOrder(ptr->rightChild);
  60.   }
  61. }
  62. void postOrder(BST ptr)
  63. {
  64.   if(ptr)
  65.   {
  66.     postOrder(ptr->leftChild);
  67.     postOrder(ptr->rightChild);
  68.     printf("%d ",ptr->data);
  69.   }
  70. }
  71. void traverBST()
  72. {
  73.   if(!root)
  74.   {
  75.     printf("\nThe Binary Search Tree is empty\n");
  76.     return;
  77.   }
  78.   printf("\nThe inorder traversal of the entered BST is: ");
  79.   inOrder(root);
  80.   printf("\nThe preorder traversal of the entered BST is: ");
  81.   preOrder(root);
  82.   printf("\nThe postorder traversal of the entered BST is: ");
  83.   postOrder(root);
  84.   printf("\n");
  85. }
  86. void searchBST()
  87. {
  88.   if(!root)
  89.   {
  90.     printf("\nThe Binary Search Tree is empty\n");
  91.     return;
  92.   }
  93.   int KEY;
  94.   printf("\nEnter the KEY Element to be searched: ");
  95.   scanf("%d",&KEY);
  96.   ptr=root;
  97.   while(ptr)
  98.   {
  99.     if(ptr->data==KEY)
  100.     {
  101.       printf("\nThe KEY %d is present in the entered BST\n",KEY);
  102.       return;
  103.     }
  104.     ptr=(ptr->data<KEY)?ptr->rightChild:ptr->leftChild;
  105.   }
  106.   printf("\nThe KEY %d is not present in the enterd BST\n",KEY);
  107. }
  108. void exitProgram()
  109. {
  110.   printf("\nThank you for using the program");
  111.   exit(0);
  112. }
  113. int main()
  114. {
  115.   int choice;
  116.   while(1)
  117.   {
  118.     printf("\nPick a Binary Search Tree operation\n1) Create a Binary Search Tree\n2) Traverse the Binary Search Tree\n3) Seach for a KEY element in the Binary Search Tree\n4) Exit\n>| ");
  119.     scanf("%d",&choice);
  120.     switch(choice)
  121.     {
  122.       case 1:
  123.       {
  124.         int num,elem;
  125.         printf("\nEnter the number of elements to be inserted into the Binary Search Tree: ");
  126.         scanf("%d",&num);
  127.         printf("\nEnter the elements to be inserted into the Binary Search Tree: ");
  128.         for(int i=1;i<=num;i++)
  129.         {
  130.           scanf("%d",&elem);
  131.           createBST(elem);
  132.         }
  133.       }
  134.       break;
  135.       case 2:
  136.         traverBST();
  137.       break;
  138.       case 3:
  139.         searchBST();
  140.       break;
  141.       case 4:
  142.         exitProgram();
  143.       break;
  144.       default:
  145.         printf("\nInvalid Choice\n");
  146.       }
  147.     }
  148.     return 0;
  149. }
  150.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement