Advertisement
Arnab_Manna

Binary Search TREE (height)

Jan 20th, 2023 (edited)
1,060
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 6.34 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3.  
  4. int count=0,max=0,min=0,len=0;
  5. int pre[100],post[100],in[100],ic=0,pc=0,poc=0;
  6. typedef struct node
  7. {
  8.     int info;
  9.     struct node *lc,*rc;
  10. }bst;
  11.  
  12. void create(bst *root)
  13. {  
  14.     len++;
  15.     char ans;
  16.     bst *ptr;
  17.     printf("\nDo you want to create the left child of %d? ",root->info);
  18.     fflush(stdin);
  19.     scanf("%c",&ans);
  20.     if(ans=='Y' || ans=='y')
  21.     {
  22.         ptr=(bst*)malloc(sizeof(bst));
  23.         printf("\nenter the information for the left child: ");
  24.         scanf("%d",&ptr->info);
  25.         ptr->lc=ptr->rc=NULL;
  26.         root->lc=ptr;
  27.         create(root->lc);
  28.     }
  29.     else
  30.         root->lc=NULL;
  31.     printf("\nDo you want to create the right child of %d? ",root->info);
  32.     fflush(stdin);
  33.     scanf("%c",&ans);
  34.     if(ans=='Y' || ans=='y')
  35.     {
  36.         ptr=(bst*)malloc(sizeof(bst));
  37.         printf("\nenter the information for the right child: ");
  38.         scanf("%d",&ptr->info);
  39.         ptr->lc=ptr->rc=NULL;
  40.         root->rc=ptr;
  41.         create(root->rc);
  42.     }
  43.     else
  44.         root->rc=NULL;
  45. }
  46.  
  47. void preorder(bst *root)
  48. {
  49.     if(root==NULL)
  50.         return;
  51.     printf("%d ",root->info);
  52.     preorder(root->lc);
  53.     preorder(root->rc);
  54. }
  55.  
  56. void inorder(bst *root)
  57. {
  58.     if(root==NULL)
  59.         return;
  60.     inorder(root->lc);
  61.     printf("%d ",root->info);
  62.     inorder(root->rc);
  63. }
  64.  
  65. void postorder(bst *root)
  66. {
  67.     if(root==NULL)
  68.         return;
  69.     postorder(root->lc);
  70.     postorder(root->rc);
  71.     printf("%d ",root->info);
  72. }
  73.  
  74. void cntleaf(bst *root)
  75. {
  76.    
  77.     if(root==NULL)
  78.         return;
  79.     if(root->lc==NULL && root->rc==NULL)
  80.     {
  81.         count++;
  82.         printf("leaf=%d\n",root->info);
  83.     }
  84.            
  85.     cntleaf(root->lc);
  86.     cntleaf(root->rc);
  87.      
  88. }
  89. void Search(bst *root,int n)
  90. {
  91.    
  92.     if(root==NULL)
  93.         return;
  94.     if(root->info==n)
  95.     {
  96.         count=1;
  97.         return;
  98.     }
  99.     Search(root->lc,n);
  100.     Search(root->rc,n);
  101. }
  102.  
  103. void min_max(bst *root)
  104. {
  105.    
  106.     if(root==NULL)
  107.         return;
  108.     if(root->info>=max)
  109.     {
  110.         max=root->info;
  111.     }
  112.     if(root->info<=min)
  113.     {
  114.         min=root->info;
  115.     }  
  116.     min_max(root->lc);
  117.     min_max(root->rc);
  118. }
  119.  
  120. void inSuc(bst *root)
  121. {
  122.     if(root==NULL)
  123.         return;
  124.     inSuc(root->lc);
  125.     //printf("%d ",root->info);
  126.     in[ic++]=root->info;
  127.     inSuc(root->rc);
  128. }
  129.  
  130. void posSuc(bst *root)
  131. {
  132.     if(root==NULL)
  133.         return;
  134.     posSuc(root->lc);
  135.     posSuc(root->rc);
  136.     //printf("%d ",root->info);
  137.     post[poc++]=root->info;
  138. }
  139.  
  140. void preSuc(bst *root)
  141. {
  142.     if(root==NULL)
  143.         return;
  144.     //printf("%d ",root->info);
  145.     pre[pc++]=root->info;
  146.     preSuc(root->lc);
  147.     preSuc(root->rc);
  148. }
  149.  
  150. int height(bst *root)
  151. {
  152.     if(root==NULL)
  153.     return 0;
  154.    
  155.     int left_height = height(root->lc);
  156.     int right_height = height(root->rc);
  157.    
  158.     return (left_height>right_height?left_height:right_height)+1;
  159.    
  160. }
  161.  
  162. int main()
  163. {
  164.     bst *root=NULL,*ptr;
  165.     int ch,n,i,j=0,len=0;
  166.     printf("\nenter the information for the root: ");
  167.     ptr=(bst*)malloc(sizeof(bst));
  168.     scanf("%d",&ptr->info);
  169.     ptr->lc=ptr->rc=NULL;
  170.     root=ptr;
  171.     create(root);
  172.     while(1)
  173.     {
  174.         printf("\n**MENU**");
  175.         printf("\n1. Preorder\n2. Inorder\n3. Postorder\n4. no of leafs \n5. search\n6. max min\n7. inorder Successor \n8. postorder Successor \n9. preorder successor \n10. inorder predicessor \n11. postorder predicessor \n12. preorder predicessor \n13. height of tree \n14. exit");
  176.         printf("\nenter your choice: ");
  177.         scanf("%d",&ch);
  178.         switch(ch)
  179.         {
  180.             case 1: preorder(root);
  181.                     break;
  182.             case 2: inorder(root);
  183.                     break;
  184.             case 3: postorder(root);
  185.                     break;
  186.             case 4:
  187.                     cntleaf(root);
  188.                     printf("no of leafs =%d",count);
  189.                     break;
  190.             case 5:
  191.                 printf("\nenter element to be searched: ");
  192.                 scanf("%d",&n);
  193.                 Search(root,n);
  194.                 if(count==1){
  195.                     printf("found");
  196.                 }
  197.                 else
  198.                 {
  199.                     printf("not found");
  200.                 }
  201.                 count=0;
  202.                 break;
  203.             case 6:
  204.                 max=root->info;
  205.                 min=root->info;
  206.                 min_max(root);
  207.                 printf("max =%d  min =%d",max,min);
  208.                 break;
  209.             case 7:
  210.                 printf("enter the value :");
  211.                 scanf("%d",&n);
  212.                  inSuc(root);
  213.                  j=0;
  214.                  len=ic;
  215.                  //printf("%d",len);
  216.                  for(i=0;i<len;i++)
  217.                  {
  218.                    
  219.                     if(in[i]==n)
  220.                     {
  221.                         j=1;
  222.                         break;
  223.                     }
  224.                  }
  225.                  if(j==1)
  226.                  {
  227.                     printf("%d",i);
  228.                     if(i==len-1)
  229.                         printf("no Successor");
  230.                     else
  231.                         printf("the successor of %d is %d",n,in[i+1]);
  232.                  }
  233.                  else
  234.                     printf("no successor");
  235.                  break;
  236.             case 8:
  237.                 printf("enter the value :");
  238.                 scanf("%d",&n);
  239.                  posSuc(root);
  240.                  j=0;
  241.                  len=poc;
  242.                  for(i=0;i<len;i++)
  243.                  {
  244.                     if(post[i]==n)
  245.                     {
  246.                         j=1;
  247.                          break;
  248.                     }
  249.                  }
  250.                  if(j==1){
  251.                     if(i==len-1)
  252.                     printf("no Successor");
  253.                     else
  254.                     printf("the successor of %d is %d",n,post[i+1]);
  255.                  }
  256.                  else
  257.                  printf("no node");
  258.                  break;
  259.             case 9:
  260.                 printf("enter the value :");
  261.                 scanf("%d",&n);
  262.                  preSuc(root);
  263.                  j=0;
  264.                  len=pc;
  265.                  for(i=0;i<len;i++)
  266.                  {
  267.                     if(pre[i]==n)
  268.                     {
  269.                         j=1;
  270.                          break;
  271.                     }
  272.                  }
  273.                  if(j==1){
  274.                     if(i==len-1)
  275.                     printf("no Successor");
  276.                     else
  277.                     printf("the successor of %d is %d",n,pre[i+1]);
  278.                  }
  279.                  else
  280.                  printf("no node");
  281.                  break;
  282.             case 10:
  283.                  printf("enter the value :");
  284.                  scanf("%d",&n);
  285.                  inSuc(root);
  286.                  j=0;
  287.                  len=ic;
  288.                  //printf("%d",len);
  289.                  for(i=0;i<len;i++)
  290.                  { 
  291.                     if(in[i]==n)
  292.                     {
  293.                         j=1;
  294.                         break;
  295.                     }
  296.                  }
  297.                  if(j==1)
  298.                  {
  299.                     printf("%d",i);
  300.                     if(i==0)
  301.                         printf("no predicessor");
  302.                     else
  303.                         printf("the predicessor of %d is %d",n,in[i-1]);
  304.                  }
  305.                  else
  306.                     printf("no successor");
  307.                  break;
  308.             case 11:
  309.                  printf("enter the value :");
  310.                  scanf("%d",&n);
  311.                  posSuc(root);
  312.                  j=0;
  313.                  len=poc;
  314.                  for(i=0;i<len;i++)
  315.                  {
  316.                     if(post[i]==n)
  317.                     {
  318.                         j=1;
  319.                          break;
  320.                     }
  321.                  }
  322.                  if(j==1){
  323.                     if(i==0)
  324.                     printf("no predicessor");
  325.                     else
  326.                     printf("the predicessor of %d is %d",n,post[i-1]);
  327.                  }
  328.                  else
  329.                  printf("no node");
  330.                  break;
  331.             case 12:
  332.                  printf("enter the value :");
  333.                  scanf("%d",&n);
  334.                  preSuc(root);
  335.                  j=0;
  336.                  len=pc;
  337.                  for(i=0;i<len;i++)
  338.                  {
  339.                     if(pre[i]==n)
  340.                     {
  341.                         j=1;
  342.                          break;
  343.                     }
  344.                  }
  345.                  if(j==1){
  346.                     if(i==0)
  347.                     printf("no predicessor");
  348.                     else
  349.                     printf("the predicessor of %d is %d",n,pre[i-1]);
  350.                  }
  351.                  else
  352.                  printf("no node");
  353.                  break;
  354.             case 13:
  355.                 printf("height of the tree is %d",height(root));
  356.             case 14:
  357.             exit(0);
  358.         }
  359.     }
  360. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement