-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.c
69 lines (60 loc) · 1.32 KB
/
test.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#include <stdio.h>
#include <stdlib.h>
typedef struct node{
int value;
struct node * left;
struct node * right;
}NODE;
typedef struct {
NODE* root;
}BINARY_TREE;
NODE* createNode(int value);
void insertTree(NODE* r, int v);
void printTree(BINARY_TREE* bt, int nv);
void preOrder(NODE* r);
int main() {
BINARY_TREE* Btree = (BINARY_TREE*) malloc(sizeof(BINARY_TREE));
Btree->root = NULL;
int v;
while (scanf("%d", &v) != EOF){
insertTree(Btree->root, v);
printf("raiz principal -> %p\n", Btree->root);
printTree(Btree, v);
}
return 0;
}
NODE* createNode(int value){
NODE* node = (NODE*) malloc(sizeof(NODE));
node->value = value;
node->left = NULL;
node->right = NULL;
return node;
}
void insertTree(NODE* r, int v){
if (r != NULL){
printf("\nr!=null\n");
if (r->value > v) insertTree(r->left, v);
else insertTree(r->right, v);
return;
}
printf("\nr:-%p-", r);
r = createNode(v);
printf("\nr:-%p-\n", r);
printf("r->v: %d\n", r->value);
}
void printTree(BINARY_TREE* bt, int nv){
printf("----\nAdicionando %d\n\t", nv);
preOrder(bt->root);
printf("\n");
}
void preOrder(NODE* r){
if (r == NULL) {
printf("\n2r:-%p-\n", r);
printf(" () ");
return;
}
printf(" ( %d ", r->value);
preOrder(r->left);
preOrder(r->right);
printf(" )");
}