Sreekar_0125

Untitled

Sep 25th, 2022
265
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.18 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. struct node {
  4.     int data;
  5.     struct node *left,*right;
  6. }*root=NULL;
  7. void insert(int x) {
  8.     struct node *p;
  9.     p = (struct node *)malloc(sizeof(struct node));
  10.     p->data=x;
  11.     p->right=NULL;
  12.     p->left=NULL;
  13.     if(root==NULL) root=p;
  14.     else {
  15.         struct node *tmp=root,*prev;
  16.         while(tmp!=NULL) {
  17.             prev = tmp;
  18.             if(x > tmp->data) {
  19.                 tmp = tmp->right;
  20.             }
  21.             else {
  22.                 tmp = tmp->left;
  23.             }
  24.         }
  25.         if(x < prev->data) prev->left=p;
  26.         else prev->right=p;
  27.     }
  28. }
  29. void preorder(struct node* tmp) {
  30.     if(tmp==NULL) return;
  31.     printf("%d ",tmp->data);
  32.     preorder(tmp->left);
  33.     preorder(tmp->right);
  34. }
  35. void inorder(struct node* tmp) {
  36.     if(tmp==NULL) return;
  37.     inorder(tmp->left);
  38.     printf("%d ",tmp->data);
  39.     inorder(tmp->right);
  40. }
  41. void postorder(struct node* tmp) {
  42.     if(tmp==NULL) return;
  43.     postorder(tmp->left);
  44.     postorder(tmp->right);
  45.     printf("%d ",tmp->data);
  46. }
  47. void delete(int x) {
  48.     struct node *prev,*tmp = root;
  49.     while(tmp->data!=x) {
  50.         if(x < tmp->data) {
  51.             prev = tmp;
  52.             tmp=tmp->left;
  53.         }
  54.         else if(x > tmp->data) {
  55.             prev = tmp;
  56.             tmp=tmp->right;
  57.         }
  58.     }
  59.     if(tmp->right==NULL && tmp->left==NULL) {
  60.         if(prev->right == tmp) prev->right=NULL;
  61.         else prev->left = NULL;
  62.         free(tmp);
  63.     }
  64.     else if((tmp->right==NULL && tmp->left!=NULL)) {
  65.         if(prev->left==tmp) prev->left = tmp->left;
  66.         else prev->right = tmp->left;
  67.         free(tmp);
  68.     }
  69.     else if((tmp->right!=NULL && tmp->left==NULL)) {
  70.         if(prev->left==tmp) prev->left = tmp->right;
  71.         else prev->right = tmp->right;
  72.         free(tmp);
  73.     }
  74.     else if(tmp->left!=NULL && tmp->right!=NULL) {
  75.         struct node *newprev,*curr = tmp->right;
  76.         newprev = tmp;
  77.         while(curr->left!=NULL) {
  78.             newprev = curr;
  79.             curr=curr->left;
  80.         }
  81.         tmp->data = curr->data;
  82.         if(newprev==tmp) newprev->right=NULL;
  83.         else
  84.         newprev->left = NULL;
  85.         free(curr);
  86.     }
  87. }
  88. int main () {
  89.     int c,n,x;
  90.     while(1) {
  91.         scanf("%d",&c);
  92.         switch(c) {
  93.             case 1: scanf("%d",&n);
  94.                     for(int i=0;i<n;i++) {
  95.                         scanf("%d",&x);
  96.                         insert(x);
  97.                     }
  98.                     printf("BST with %d nodes is ready to use!\n",n);
  99.             break;
  100.            
  101.             case 2: printf("BST Traversal in INORDER\n");
  102.                     inorder(root);
  103.                     printf("\n");
  104.             break;
  105.            
  106.             case 3: printf("BST Traversal in PREORDER\n");
  107.                     preorder(root);
  108.                     printf("\n");
  109.             break;
  110.            
  111.             case 4: printf("BST Traversal in POSTORDER\n");
  112.                     postorder(root);
  113.                     printf("\n");
  114.             break;
  115.            
  116.             case 5: scanf("%d",&x); delete(x); break;
  117.  
  118.             case 6: exit(0);
  119.            
  120.         }
  121.     }
  122. }
Advertisement
Add Comment
Please, Sign In to add comment