Advertisement
Guest User

BST.CPP

a guest
Jun 18th, 2018
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.57 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <conio.h>
  3. #include <malloc.h>
  4. struct tree
  5. {
  6.     int data;
  7.     struct tree *left, *right;
  8. };
  9. typedef struct tree node;
  10. node *root;
  11. void insert(void);
  12. void search(void);
  13. void inorder(node *ptr);
  14. void preorder(node *ptr);
  15. void postorder(node *ptr);
  16. int main()
  17. {
  18.     int c;
  19.     clrscr();
  20.     do
  21.     {
  22.         printf("\n press 1 for insert");
  23.         printf("\n press 2 for search");
  24.         printf("\n press 3 for inorder ");
  25.         printf("\n press 4 for preorder ");
  26.         printf("\n press 5 for postorder ");
  27.         printf("\n press 0 for exit");
  28.         scanf("%d", &c);
  29.         if (c == 1)
  30.         {
  31.             insert();
  32.         }
  33.         else if (c == 2)
  34.         {
  35.             search();
  36.         }
  37.         else if (c == 3)
  38.         {
  39.             inorder(root);
  40.         }
  41.         else if (c == 4)
  42.         {
  43.             preorder(root);
  44.         }
  45.         else if (c == 5)
  46.         {
  47.             postorder(root);
  48.         }
  49.     } while (c != 0);
  50.     getch();
  51.     return 0;
  52. }
  53. void insert(void)
  54. {
  55.     int f = 0;
  56.     node *temp, *ptr, *parent;
  57.     temp = (node *)malloc(sizeof(node));
  58.     printf("\n enter any number");
  59.     scanf("%d", &temp->data);
  60.     temp->left = NULL;
  61.     temp->right = NULL;
  62.     if (root == NULL)
  63.     {
  64.         root = temp;
  65.     }
  66.     else
  67.     {
  68.         ptr = root;
  69.         while (ptr->data != temp->data)
  70.         {
  71.             parent = ptr;
  72.             if (temp->data < ptr->data)
  73.             {
  74.                 ptr = ptr->left;
  75.                 if (ptr == NULL)
  76.                 {
  77.                     f = 1;
  78.                     break;
  79.                 }
  80.             }
  81.             else if (temp->data > ptr->data)
  82.             {
  83.                 ptr = ptr->right;
  84.                 if (ptr == NULL)
  85.                 {
  86.                     f = 2;
  87.                     break;
  88.                 }
  89.             }
  90.             else if (ptr->data == temp->data)
  91.             {
  92.                 printf("\n node already exist");
  93.             }
  94.         }
  95.     }
  96.  
  97.     if (f == 1)
  98.     {
  99.         parent->left = temp;
  100.     }
  101.     else if (f == 2)
  102.     {
  103.         parent->right = temp;
  104.     }
  105. }
  106.  
  107. void search()
  108. {
  109.     node *ptr;
  110.     int c;
  111.     printf("\n enter tthe number for search");
  112.     scanf("%d", &c);
  113.  
  114.     ptr = root;
  115.     while (ptr->data != c)
  116.     {
  117.         printf("\n %d", ptr->data);
  118.         if (c < ptr->data)
  119.         {
  120.             ptr = ptr->left;
  121.             if (ptr == NULL)
  122.             {
  123.                 printf("\nnodenot found in the list");
  124.                 break;
  125.             }
  126.         }
  127.         else if (c > ptr->data)
  128.         {
  129.             ptr = ptr->right;
  130.             if (ptr == NULL)
  131.             {
  132.                 printf("\n nodeis not found in the list");
  133.                 break;
  134.             }
  135.         }
  136.         else if (c == ptr->data)
  137.         {
  138.             printf("\n no. isfound in the list");
  139.         }
  140.     }
  141. }
  142.  
  143. void inorder(node *ptr)
  144. {
  145.     if (ptr != NULL)
  146.     {
  147.         inorder(ptr->left);
  148.         printf("\t %d", ptr->data);
  149.         inorder(ptr->right);
  150.     }
  151. }
  152.  
  153. void preorder(node *ptr)
  154. {
  155.     if (ptr != NULL)
  156.     {
  157.         printf("\t %d", ptr->data);
  158.         preorder(ptr->left);
  159.         preorder(ptr->right);
  160.     }
  161. }
  162.  
  163. void postorder(node *ptr)
  164. {
  165.     if (ptr != NULL)
  166.     {
  167.         postorder(ptr->left);
  168.         postorder(ptr->right);
  169.         printf("\t %d", ptr->data);
  170.     }
  171. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement