Advertisement
globalbus

drzewko_chmiel

May 31st, 2011
361
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.83 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. typedef struct node
  5. {
  6.     double value;
  7.     struct node *L, *P;
  8.  
  9. } node;
  10. typedef node * tree_t;
  11.  
  12. int add_tree(tree_t *tree, double add_value)
  13. {
  14.     tree_t temp= *tree;
  15.     if(temp==NULL)
  16.     {
  17.         temp = malloc(sizeof(node));
  18.         if(temp==NULL)
  19.             return 1;
  20.         temp->value=add_value;
  21.         //printf("%f\n", temp-> value);
  22.         temp->L=NULL;
  23.         temp->P=NULL;
  24.         *tree = temp;
  25.         return 0;
  26.     }
  27.     if(temp->value>add_value)
  28.         add_tree(&(temp->L),add_value);
  29.     else
  30.         add_tree(&(temp->P),add_value);
  31.     return 0;
  32. }
  33.  
  34. void tree_print(tree_t tree)
  35. {
  36.     if(tree)
  37.     {
  38.         tree_print(tree->L);
  39.         printf("%f\n", tree-> value);
  40.         tree_print(tree->P);
  41.     }
  42.     return;
  43. }
  44.  
  45. int main()
  46. {
  47.     tree_t tree = NULL;
  48.     int i;
  49.     for(i=0;i<10;i++)
  50.         add_tree(&tree,rand()%100);
  51.     tree_print(tree);
  52.     system("pause");
  53.     return 0;
  54. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement