Sreekar_0125

bst

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