-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
75 lines (56 loc) · 1.41 KB
/
main.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
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
typedef struct node {
int value;
struct node* left;
struct node* right;
} node;
typedef struct tree {
node* root;
} tree;
node* create_node(int value) {
node* n = malloc(sizeof(node));
if (!n) {
fprintf(stderr, "failed to allocate memory for node\n");
return NULL;
}
n->value = value;
n->left = NULL;
n->right = NULL;
return n;
}
void free_node(node* n) {
if (!n) return;
free_node(n->left);
free_node(n->right);
free(n);
}
void destroy_tree(tree* t) {
if (!t) return;
free_node(t->root);
t->root = NULL;
}
// check the nodes value
bool check_node(node* n, int value) {
if (n->value == value) return true;
return check_node(n->left, value) || check_node(n->right, value);
}
// this function will find the first occurance of <value>
bool search_tree(tree* t, int value) {
if (!t) return false;
return check_node(t->root, value);
}
int main() {
tree bs = {0};
bs.root = create_node(10);
bs.root->left = create_node(20);
bs.root->left->left = create_node(30);
bs.root->right = create_node(40);
printf("deepest node value: %d\n", bs.root->left->left->value);
printf("searching tree for 30...\n");
bool result = search_tree(&bs, 30);
printf("search result: %s\n", result ? "true" : "false");
destroy_tree(&bs);
return 0;
}