SHARE
TWEET

Contra

a guest Oct 21st, 2019 81 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <stdlib.h>
  4.  
  5. typedef struct tree
  6. {
  7.         int data;
  8.         struct tree *left;  
  9.         struct tree *right;
  10. } tree;
  11.  
  12.  
  13. void add1(tree** list, int a, int level_loc, int *level_max)
  14. {
  15.     tree *newNode;
  16.     if ((*list) == NULL)
  17.     {
  18.         newNode = malloc(sizeof(tree));
  19.         newNode->data = a;
  20.         newNode->right = NULL;
  21.         newNode->left = NULL;
  22.         (*list) = newNode;
  23.     }
  24.     else if (a < (*list)->data)
  25.     {
  26.         add1(&((*list)->left), a, level_loc + 1 , level_max);  
  27.     }
  28.     else if (a > (*list)->data)
  29.     {
  30.         add1(&((*list)->right), a, level_loc + 1 , level_max);
  31.     }
  32.     if ((level_loc)>(*level_max))
  33.     {
  34.         (*level_max) = level_loc;  
  35.     }
  36.     return;
  37.    
  38.    
  39. }
  40.  
  41. void output(tree **list, int current_height, int needed_height)
  42. {
  43.     if ((*list) != NULL)
  44.     {
  45.         if (current_height == needed_height)
  46.         {
  47.         printf("%d ", (*list)->data);  
  48.         }
  49.         output(&((*list)->left), current_height + 1, needed_height);
  50.         output(&((*list)->right), current_height + 1, needed_height);
  51.     }  
  52.     return;
  53. }
  54.  
  55.  
  56. int main(int argc, char **argv)
  57. {
  58.  
  59.     int level_counter, tree_counter = 0;
  60.     int i, j, a, height = 0;
  61.     tree *tree_0 = NULL;
  62.     for (i = 1; i < argc; i++)
  63.     {
  64.         a = atoi(argv[i]);
  65.         add1(&tree_0, a, 0, &height);
  66.     }
  67.     for (j = height; j >= 0; j--)
  68.     {
  69.     output(&tree_0, j, height);
  70.     printf("\n");
  71.        
  72.     }
  73.    
  74.     return 0;  
  75. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top