-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbinarytree.c
78 lines (65 loc) · 1.57 KB
/
binarytree.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
76
77
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include "binarytree.h"
static void binarytree_set(binarytree* bt, void* ent, size_t ent_size)
{
assert(bt->ent == NULL);
bt->ent = malloc(ent_size);
assert(bt->ent != NULL);
memcpy(bt->ent, ent, ent_size);
}
binarytree* binarytree_create()
{
binarytree* newtree = malloc(sizeof(binarytree));
memset(newtree, 0, sizeof(binarytree));
assert(newtree != NULL);
return newtree;
}
int binarytree_insert(binarytree** bt, cmp_func cmp, void *ent, size_t ent_size)
{
if (*bt == NULL) {
*bt = binarytree_create();
binarytree_set(*bt, ent, ent_size);
}
else if ((*bt)->ent == NULL) {
binarytree_set(*bt, ent, ent_size);
}
else if (0 < (*cmp)((*bt)->ent, ent)) {
binarytree_insert(&(*bt)->left, cmp, ent, ent_size);
}
else {
binarytree_insert(&(*bt)->right, cmp, ent, ent_size);
}
return 1; // always successful for now
}
void* binarytree_find(binarytree *bt, cmp_func cmp, void *ent)
{
if (bt == NULL || bt->ent == NULL) {
return NULL;
}
int res = (*cmp)(bt->ent, ent);
if (res == 0) {
return bt->ent;
}
else if (0 < res) {
return binarytree_find(bt->left, cmp, ent);
}
else {
return binarytree_find(bt->right, cmp, ent);
}
}
dict_ent* dict_create(const char* key, int value)
{
dict_ent* dict = malloc(sizeof(dict_ent));
assert(dict != NULL);
memset(dict, 0, sizeof(dict_ent));
dict->value = value;
strncpy(dict->key, key, MAX_WORDSIZE);
return dict;
}
int dict_cmp(void *d1, void *d2)
{
return strncmp(((dict_ent*)d1)->key, ((dict_ent*)d2)->key, MAX_WORDSIZE);
}