Advertisement
Shailrshah

Binary Trees Practice problems

Sep 11th, 2013
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.92 KB | None | 0 0
  1. /*Write a menu-driven program to display the number of nodes, height of the tree, maximum and minimum values, leaf and non-leaf nodes, sum of all nodes of the tree and odd & even values.*/
  2.  
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. struct tree{
  6.     int data;
  7.     struct tree *left,*right;
  8. }*root = NULL;
  9. int totalCount, evenCount, oddCount;
  10. void insert(int value){
  11.     struct tree *newNode = (struct tree*) malloc(sizeof(struct tree));
  12.     newNode->data = value; newNode->right = newNode->left = NULL;
  13.     if(!root)   root = newNode;
  14.     else{
  15.         struct tree *help = root, *previous = NULL;
  16.         while(help){
  17.             previous = help;
  18.             if(value > help->data) help = help ->right;
  19.             else help = help->left;
  20.         }
  21.         if(value > previous->data)   previous->right = newNode;
  22.         else previous->left = newNode;
  23.     }
  24. }
  25. void count(struct tree* help){
  26.     if(help){
  27.         count(help->right);
  28.         totalCount++;
  29.         count(help->left);
  30.     }
  31. }
  32. int height(struct tree *help){
  33.     int leftHeight, rightHeight;
  34.     if(!help)   return 0;
  35.     leftHeight = height(help->left);
  36.     rightHeight = height(help->right);
  37.     if(leftHeight > rightHeight)    return leftHeight+1;
  38.     else return rightHeight+1;
  39. }
  40. int largest(struct tree* help){
  41.     if(!help)   return 0;
  42.     else if (!help-> right) return help->data;
  43.     else return largest(help->right);
  44. }
  45. int smallest(struct tree* help){
  46.     if(!help)   return 0;
  47.     else if (!help-> left) return help->data;
  48.     else return smallest(help->left);
  49. }
  50. int leaf(struct tree *help){
  51.     if(help){
  52.         if(!help->right && !help->left){
  53.             printf("%d ", help-> data);
  54.             return 1;
  55.         }
  56.         else return leaf(help->left) + leaf(help->right);
  57.     }
  58.     else return 0;
  59. }
  60. int nonleaf(struct tree *help){
  61.     if(help){
  62.         if(help->right || help->left){
  63.             printf("%d ", help-> data);
  64.             return 1 + nonleaf(help->left) + nonleaf(help->right);
  65.         }
  66.     }
  67.     else return 0;
  68. }
  69. int sum(struct tree* help){
  70.     if(help)
  71.         return help->data + sum(help->right) + sum(help->left);
  72.     else return 0;
  73. }
  74. void odd(struct tree *help){
  75.     if(help){
  76.         odd(help->right);
  77.         if(help->data % 2 == 1 || help->data % 2 == -1){
  78.             oddCount++;
  79.             printf("%d ", help->data);
  80.         }
  81.         odd(help->left);
  82.     }
  83. }
  84. void even(struct tree *help){
  85.     if(help){
  86.         even(help->right);
  87.         if(help->data % 2 == 0){
  88.             evenCount++;
  89.             printf("%d ", help->data);
  90.         }
  91.         even(help->left);
  92.     }
  93. }
  94. int main(){
  95.     int n;
  96.     up: printf("\nEnter nodes, input -1 to stop.\n");
  97.     while(1){
  98.         printf("\nInput: ");
  99.         scanf("%d",&n);
  100.         if(n==-1) break;
  101.         insert(n);
  102.     }
  103.     printf("\n1.Total 2.Height 3.Max 4.Min 5.Leaf 6.Non-Leaf 7.Sum 8.Odd 9.Even 10.Exit 11.Insert\n");
  104.     while(1){
  105.         printf("\n\nSelect an option: ");
  106.         scanf("%d",&n);
  107.         switch(n){
  108.             case 1: totalCount = 0; count(root); printf("\nThe number of nodes: %d.", totalCount);break;
  109.             case 2: printf("\nThe height of the tree is %d.", height(root)-1); break;
  110.             case 3: printf("The maximum value is %d.", largest(root)); break;
  111.             case 4: printf("The minimum value is %d.", smallest(root)); break;
  112.             case 5: printf("\n%d leaf node(s) found:  ", leaf(root)); break;
  113.             case 6: printf("\n%d non-leaf node(s) found.", nonleaf(root)); break;
  114.             case 7: printf("\n The sum of all the elements in the tree is %d.",sum(root));   break;
  115.             case 8: odd(root); printf("\n%d odd node(s) found.", oddCount); break;//
  116.             case 9: even(root); printf("\n%d even node(s) found.", evenCount); break;
  117.             case 10:return 0;
  118.             case 11: goto up;
  119.             default: printf("\nEnter a number in between 1 and 12.");
  120.         }
  121.     }
  122. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement