-
Notifications
You must be signed in to change notification settings - Fork 0
/
bst.c
38 lines (35 loc) · 888 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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct Bst {
int data;
struct Bst* lchild;
struct Bst* rchild;
};
struct Bst* bst_find(struct Bst* node, int target) {
if (!node) {
return NULL;
}
if (target < node->data)
return bst_find(node->lchild, target);
else if (target > node->data)
return bst_find(node->rchild, target);
else {
return node;
}
}
struct Bst* bst_insert(struct Bst* node, int target) {
struct Bst* ptr;
if (!node) {
ptr = (struct Bst*)malloc(sizeof(struct Bst));
ptr->data = target;
ptr->lchild = NULL;
ptr->rchild = NULL;
return ptr;
}
if (target < node->data)
node->lchild = bst_insert(node->lchild, target);
else if (target > node->data)
node->rchild = bst_insert(node->rchild, target);
return node;
}