-
Notifications
You must be signed in to change notification settings - Fork 0
/
bst.c
75 lines (69 loc) · 2.05 KB
/
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
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 <string.h>
#include "bst.h"
struct bst_node_t* bst_node_t_str_create(const char* k, void* v) {
struct bst_node_t* retval = malloc(sizeof(struct bst_node_t));
const int l = strlen(k);
retval->key = malloc(sizeof(char)*l + 1);
strcpy(retval->key, k);
((char*)retval->key)[l] = 0;
retval->value = v;
retval->left = 0;
retval->right = 0;
return retval;
}
struct bst_node_t* bst_node_t_insert(struct bst_node_t* root, struct bst_node_t* ins, int (*pred)(void*, void*)) {
if (ins == 0 || 0 == root) return ins;
int cmp = (*pred)(root->key, ins->key);
if (0 == cmp) {
ins->left = root->left;
ins->right = root->right;
root = ins;
} else if (0 > cmp) {
root->left = bst_node_t_insert(root->left, ins, pred);
} else {
root->right = bst_node_t_insert(root->right, ins, pred);
}
return root;
}
struct bst_node_t* bst_node_t_delete(struct bst_node_t* root, void* key, int (*pred)(void*, void*)) {
if (0 == root) return root;
int cmp = (*pred)(root->key, key);
if (0 == cmp) {
if (root->right != 0)
root->left = bst_node_t_insert(root->left, root->right, pred);
struct bst_node_t* nr = root->left;
root = nr;
} else if (0 > cmp) {
root->left = bst_node_t_delete(root->left, key, pred);
} else {
root->right = bst_node_t_delete(root->right, key, pred);
}
return root;
}
struct bst_node_t* bst_node_t_find(struct bst_node_t* root, void* key, int (*pred)(void*, void*)) {
if (0 == root) return 0;
int cmp = (*pred)(root->key, key);
if (0 == cmp) {
return root;
} else if (0 > cmp) {
return bst_node_t_find(root->left, key, pred);
} else {
return bst_node_t_find(root->right, key, pred);
}
}
void free_bst(struct bst_node_t* root) {
if (0 == root) return;
free_bst(root->left);
free_bst(root->right);
free(root);
}
struct bst_node_t* bst_node_deep_copy(struct bst_node_t* root) {
if (!root)
return 0;
struct bst_node_t* retval = bst_node_t_str_create(root->key, root->value);
retval->left = bst_node_deep_copy(root->left);
retval->right = bst_node_deep_copy(root->right);
return retval;
}