-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathanotherBinaryTree.c
More file actions
186 lines (149 loc) · 2.57 KB
/
anotherBinaryTree.c
File metadata and controls
186 lines (149 loc) · 2.57 KB
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
//----> Declarations
struct btree;
typedef struct btree btree;
struct btree_node;
typedef struct btree_node btree_node;
btree* btree_create();
btree_node* node_create(int d);
void btree_insert(btree* t, int d);
int btree_size(btree* t);
int btree_max(btree* t);
int btree_min(btree* t);
bool btree_contains(btree* t, int d);
void btree_print(btree* t);
//----> Declarations end
struct btree
{
int size;
int max;
int min;
btree_node* root;
};
struct btree_node
{
int value;
btree_node* left;
btree_node* right;
};
btree* btree_create()
{
btree* myBtree = (btree*)malloc(sizeof(btree));
myBtree->root = NULL;
myBtree->size = 0;
return myBtree;
}
btree_node* node_create(int d)
{
btree_node* myNode = (btree_node*)malloc(sizeof(btree_node));
myNode->left = NULL;
myNode->right = NULL;
myNode->value = d;
return myNode;
}
void btree_insertRec(btree_node* n, int d)
{
if (n->value > d)
{
if (n->left == NULL)
n->left = node_create(d);
else
btree_insertRec(n->left, d);
}
else
{
if (n->right == NULL)
n->right = node_create(d);
else
btree_insertRec(n->right, d);
}
}
void btree_insert(btree* t, int d)
{
if (t == NULL)
return;
if (btree_contains(t, d))
return;
if (btree_size(t) == 0)
{
t->max = d;
t->min = d;
t->root = node_create(d);
}
else
{
btree_insertRec(t->root, d);
}
t->size += 1;
}
bool btree_containsRec(btree_node* n, int d)
{
if (n == NULL)
return false;
if (n->value == d)
return true;
else
{
if (n->value > d)
return btree_containsRec(n->left, d);
else
return btree_containsRec(n->right, d);
}
}
bool btree_contains(btree* t, int d)
{
if (t == NULL)
return false;
if (btree_containsRec(t->root, d))
return true;
return false;
}
int btree_size(btree* t)
{
return t->size;
}
int btree_max(btree* t)
{
return t->max;
}
int btree_min(btree* t)
{
return t->min;
}
void btree_printRec(btree_node* n)
{
if (n == NULL)
return;
printf("(");
btree_printRec(n->left);
printf("%d", n->value);
btree_printRec(n->right);
printf(")");
}
void btree_print(btree* t)
{
if (t == NULL)
return ;
if (btree_size(t) == 0)
printf("Your binary tree is empty!\n");
else
{
btree_printRec(t->root);
}
}
int main(void) {
btree* myBtree = btree_create();
btree_insert(myBtree, 5);
btree_insert(myBtree, 3);
btree_insert(myBtree, 2);
btree_insert(myBtree, 1);
btree_insert(myBtree, 4);
btree_insert(myBtree, 7);
btree_insert(myBtree, 6);
btree_insert(myBtree, 8);
btree_insert(myBtree, 9);
btree_print(myBtree);
return EXIT_SUCCESS;
}