-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbinaryTree.c
77 lines (73 loc) · 1.34 KB
/
binaryTree.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
70
71
72
73
74
75
76
77
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
//https://thehuxley.com/problem/783?quizId=6236
typedef struct Node
{
int data;
struct Node* left;
struct Node* right;
}binaryTree;
binaryTree* iniT()
{
return NULL;
}
binaryTree* createBinTree(int item, binaryTree* left, binaryTree* right)
{
binaryTree* new = (binaryTree*) malloc(sizeof(binaryTree));
new->data = item;
new->left = left;
new->right = right;
return new;
}
binaryTree* add(int item, binaryTree* bt)
{
if (bt == NULL)
{
bt = createBinTree(item,NULL,NULL);
}
else if (item < bt->data) //11>2
{
bt->left = add(item, bt->left);
}
else
{
bt->right = add(item, bt->right);
}
return bt;
}
int isEmpty(binaryTree* bt)
{
return (bt == NULL);
}
void preOrder(binaryTree* bt)
{
if (!(isEmpty(bt)))
{
printf(" ( %d ",bt->data);
preOrder(bt->left);
preOrder(bt->right);
//printf(") ");
printf(") ");
}
else
{
printf(" () ");
}
}
int main()
{
int n;
binaryTree* bT = iniT();
while (scanf("%d",&n) != EOF)
{
printf("----\nAdicionando %d\n",n);
bT = add(n,bT);
printf(" ");
preOrder(bT);
printf("\n");
}
printf("----\n");
return 0;
}