Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- typedef struct node
- {
- double value;
- struct node *L, *P;
- } node;
- typedef node * tree_t;
- int add_tree(tree_t *tree, double add_value)
- {
- tree_t temp= *tree;
- if(temp==NULL)
- {
- temp = malloc(sizeof(node));
- if(temp==NULL)
- return 1;
- temp->value=add_value;
- //printf("%f\n", temp-> value);
- temp->L=NULL;
- temp->P=NULL;
- *tree = temp;
- return 0;
- }
- if(temp->value>add_value)
- add_tree(&(temp->L),add_value);
- else
- add_tree(&(temp->P),add_value);
- return 0;
- }
- void tree_print(tree_t tree)
- {
- if(tree)
- {
- tree_print(tree->L);
- printf("%f\n", tree-> value);
- tree_print(tree->P);
- }
- return;
- }
- int main()
- {
- tree_t tree = NULL;
- int i;
- for(i=0;i<10;i++)
- add_tree(&tree,rand()%100);
- tree_print(tree);
- system("pause");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement