HmHimu

bst

Apr 10th, 2017
49
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.57 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. struct Node
  4. {
  5.     int data;
  6.     struct Node *left;
  7.     struct Node *right;
  8. } *root;
  9.  
  10. //struct Node *root = NULL;
  11. /*struct Node *newNode(int item)
  12. {
  13.     struct Node *temp =  (struct Node *)malloc(sizeof(struct Node));
  14.     temp->data = item;
  15.     temp->left = temp->right = NULL;
  16.     return temp;
  17. }*/
  18. void insert(struct Node *temp_root, int data )
  19. {
  20.     if(temp_root == NULL)
  21.     {
  22.         struct Node *p_root = (struct Node*)malloc(sizeof(struct Node));
  23.         p_root->left = NULL;
  24.         p_root->right = NULL;
  25.         p_root->data = data;
  26.         root = p_root;
  27.         return;
  28.     }
  29.     else if(data < temp_root->data && temp_root->left != NULL)
  30.     {
  31.         temp_root = temp_root->left;
  32.         insert(temp_root,data);
  33.     }
  34.     else if(data > temp_root->data && temp_root->right != NULL)
  35.     {
  36.         temp_root = temp_root->right;
  37.         insert(temp_root,data);
  38.     }
  39.     else if(temp_root->left == NULL && data < temp_root->data)
  40.     {
  41.         struct Node *p_root = (struct Node*)malloc(sizeof(struct Node));
  42.         p_root->data = data;
  43.         p_root->right = NULL;
  44.         p_root->left = NULL;
  45.         temp_root->left = p_root;
  46.         return;
  47.     }
  48.     else if(temp_root->right == NULL  && data > temp_root->data)
  49.     {
  50.         struct Node *p_root = (struct Node*)malloc(sizeof(struct Node));
  51.         p_root->data = data;
  52.         p_root->right = NULL;
  53.         p_root->left = NULL;
  54.         temp_root->right = p_root;
  55.         return;
  56.     }
  57. }
  58. struct Node *find_minimum(struct Node* node)
  59. {
  60.     struct node* current = node;
  61.     while(node->left != NULL)
  62.         node = node->left;
  63.     return current;
  64. }
  65. struct Node* Delete(struct Node *root, int data)
  66. //void Delete(struct Node *root, int data )
  67. {
  68.     if(root == NULL)
  69.         return root;
  70.     else if(data < root->data)
  71.         root->left = Delete(root->left,data);
  72.     else if (data > root->data)
  73.         root->right = Delete(root->right,data);
  74.     else
  75.     {
  76.         if(root->left == NULL && root->right == NULL)
  77.         {
  78.             free(root);
  79.             root = NULL;
  80.         }
  81.         else if(root->left == NULL)
  82.         {
  83.             //struct Node *temp = root;
  84.             free(root);
  85.             root = root->right;
  86.  
  87.         }
  88.         else if(root->right == NULL)
  89.         {
  90.            // struct Node *temp = root;
  91.             free(root);
  92.             root = root->left;
  93.  
  94.         }
  95.         else
  96.         {
  97.             struct Node *temp = find_minimum(root->right);
  98.             root->data = temp->data;
  99.             root->right= Delete(root->right,temp->data);
  100.         }
  101.         //return root;
  102.     }
  103.     return root;
  104. }
  105.  
  106. void preorder (struct Node *curr)
  107. {
  108.     if(curr!=NULL)
  109.     {
  110.         printf(" %d ",curr->data);
  111.         preorder(curr->left);
  112.         preorder(curr->right);
  113.     }
  114. }
  115.  
  116. void inorder(struct Node *curr)
  117. {
  118.     if(curr!=NULL)
  119.     {
  120.         inorder(curr->left);
  121.         printf(" %d ", curr->data);
  122.         inorder(curr->right);
  123.     }
  124. }
  125.  
  126. void postorder(struct Node *curr)
  127. {
  128.     if(curr!=NULL)
  129.     {
  130.         postorder(curr->left);
  131.         postorder(curr->right);
  132.         printf(" %d ",curr->data);
  133.     }
  134. }
  135.  
  136. int main()
  137. {
  138.     int choice,value;
  139.     for(;;)
  140.     {
  141.         printf("\n\n\t\t---------------------\n");
  142.         printf("\t\t:    Binary Tree    :\n");
  143.         printf("\t\t---------------------\n");
  144.         printf("\t\t:  1.  Insert       :\n");
  145.         printf("\t\t:  2.  Delete       :\n");
  146.         printf("\t\t:  3.  Preorder     :\n");
  147.         printf("\t\t:  4.  Postorder    :\n");
  148.         printf("\t\t:  5.  Inorder      :\n");
  149.         printf("\t\t:  6.  exit         :\n");
  150.         printf("\t\t---------------------\n");
  151.         printf("\t\tEnter Choice: ");
  152.         scanf("\t\t%d",&choice);
  153.  
  154.         switch (choice)
  155.         {
  156.         case 1 :
  157.             printf("\t\tEnter a number: ");
  158.             scanf("%d",&value);
  159.             insert(root, value);
  160.             break;
  161.         case 2 :
  162.             printf("\t\tEnter a number: ");
  163.             scanf("%d",&value);
  164.             Delete(root, value);
  165.             break;
  166.         case 3 :
  167.             printf("\n  Preorder:");
  168.             preorder(root);
  169.             break;
  170.         case 4 :
  171.             printf("\n  Postorder:");
  172.             postorder(root);
  173.             break;
  174.         case 5 :
  175.             printf("\n  Inorder:");
  176.             inorder(root);
  177.             break;
  178.         case 6 :
  179.             return EXIT_SUCCESS;
  180.         default:
  181.             printf("\t\tEnter only 1 to 6");
  182.             break;
  183.         }
  184.     }
  185. }
Add Comment
Please, Sign In to add comment