Advertisement
GamerBhai02

3. BST to insert and traverse using LL

Nov 4th, 2024 (edited)
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.18 KB | Source Code | 0 0
  1. https://onlinegdb.com/buyRXUjEE
  2. #include<stdio.h>
  3. #include<stdlib.h>
  4. struct node {
  5.     int data;
  6.     struct node *left, *right;
  7. };
  8. typedef struct node *BSTNODE;
  9. BSTNODE newNodeInBST(int item) {
  10.     BSTNODE temp =  (BSTNODE)malloc(sizeof(struct node));
  11.     temp->data = item;
  12.     temp->left = temp->right = NULL;
  13.     return temp;
  14. }
  15. BSTNODE insertNodeInBST(BSTNODE node, int data) {
  16.     if(node==NULL){
  17.         printf("Successfully inserted.\n");
  18.         return newNodeInBST(data);
  19.     }
  20.     if(data<node->data)
  21.         node->left=insertNodeInBST(node->left,data);
  22.     else if(data>node->data)
  23.         node->right=insertNodeInBST(node->right,data);
  24.     else
  25.         printf("Element already exists in BST.\n");
  26.     return node;
  27. }
  28. void inorderInBST(BSTNODE node) {
  29.     if(node!=NULL){
  30.         inorderInBST(node->left);
  31.         printf("%d ",node->data);
  32.         inorderInBST(node->right);
  33.     }
  34. }
  35. void preorderInBST(BSTNODE node) {
  36.     if(node!=NULL){
  37.         printf("%d ",node->data);
  38.         preorderInBST(node->left);
  39.         preorderInBST(node->right);
  40.     }
  41. }
  42. void postorderInBST(BSTNODE node) {
  43.     if(node!=NULL){
  44.         postorderInBST(node->left);
  45.         postorderInBST(node->right);
  46.         printf("%d ",node->data);
  47.     }
  48. }
  49. void main() {
  50.     int x, op;
  51.     BSTNODE root = NULL;
  52.     while(1) {
  53.         printf("1.Insert 2.Inorder Traversal 3.Preorder Traversal 4.Postorder Traversal 5.Exit\n");
  54.         printf("Enter your option : ");
  55.         scanf("%d", &op);
  56.         switch(op) {
  57.             case 1:
  58.                 printf("Enter an element to be inserted : ");
  59.                 scanf("%d", &x);
  60.                 root = insertNodeInBST(root,x);
  61.                 break;
  62.             case 2:
  63.                 if(root == NULL) {
  64.                     printf("Binary Search Tree is empty.\n");
  65.                 }
  66.                 else {
  67.                     printf("Elements of the BST (in-order traversal): ");
  68.                     inorderInBST(root);
  69.                     printf("\n");
  70.                 }
  71.                 break;
  72.             case 3:
  73.                 if(root == NULL) {
  74.                     printf("Binary Search Tree is empty.\n");
  75.                 }
  76.                 else {
  77.                     printf("Elements of the BST (pre-order traversal): ");
  78.                     preorderInBST(root);
  79.                     printf("\n");
  80.                 }
  81.                 break;
  82.             case 4:
  83.                 if(root == NULL) {
  84.                     printf("Binary Search Tree is empty.\n");
  85.                 }
  86.                 else {
  87.                     printf("Elements of the BST (post-order traversal): ");
  88.                     postorderInBST(root);
  89.                     printf("\n");
  90.                 }
  91.                 break;
  92.             case 5:
  93.                 exit(0);
  94.         }
  95.     }
  96. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement