-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbst.c
49 lines (43 loc) · 1019 Bytes
/
bst.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
#include <stdio.h>
#include <stdlib.h>
typedef struct treeNode_t {
int val;
struct treeNode_t *left;
struct treeNode_t *right;
} treeNode;
void tree_insert(treeNode **root, int v) {
if (*root == NULL) {
*root = malloc(sizeof(treeNode));
(*root)->val = v;
(*root)->left = (*root)->right = NULL;
return;
}
if ((*root)->val == v) return;
if (v > (*root)->val) {
tree_insert(&(*root)->right, v);
} else {
tree_insert(&(*root)->left, v);
}
}
int tree_find(const treeNode *root, int v) {
if (!root) return 0;
if (root->val == v) return 1;
if (v > root->val)
return tree_find(root->right, v);
else
return tree_find(root->left, v);
}
void tree_inorder(const treeNode *root) {
if (!root) return;
tree_inorder(root->left);
printf("%d\n", root->val);
tree_inorder(root->right);
}
int main() {
treeNode *root = NULL;
FILE *fp = fopen("/dev/urandom", "r");
int i;
for (i=0; i<64; i++) tree_insert(&root, fgetc(fp));
tree_inorder(root);
return 0;
}