-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
156 lines (134 loc) · 2.83 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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#define DATALEN 1
enum color {
RED = 1,
BLACK
};
struct node {
struct node *parent, *left, *right;
enum color color;
char data[DATALEN];
};
const struct node NIL_NODE = {NULL, NULL, NULL, BLACK};
#define NIL (struct node*)&NIL_NODE
int is_leaf(struct node *n)
{
return n == NIL;
}
struct node *create_node()
{
struct node *p = (struct node *)malloc(sizeof(struct node));
if (p != NULL) {
p->parent = NULL;
p->left = p->right = NIL;
p->color = BLACK;
memset(p->data, 0, DATALEN);
}
return p;
}
void assign_value(struct node *node, const char data[])
{
memcpy(node->data, data, DATALEN);
}
void destroy_node(struct node *n)
{
if (!is_leaf(n))
free(n);
}
void set_data(struct node *n, const char data[])
{
memcpy(n->data, data, DATALEN);
}
int cmp(const char data1[], const char data2[])
{
for (int i = 0; i < DATALEN; i++) {
if (data1[i] != data2[i])
return data1[i] - data2[i];
}
return 0;
}
void replace_node(struct node *n, struct node *child)
{
if (is_leaf(child)) {
if (n == n->parent->left)
n->parent->left = child;
else
n->parent->right = child;
}
struct node *node = create_node();
node->parent = n->parent;
node->left = n->left;
node->right = n->right;
memcpy(node->data, child->data, DATALEN);
node->color = child->color;
}
void delete_one_child(struct node *root, struct node *n)
{
struct node *child = is_leaf(n->right) ? n->left : n->right;
}
struct node *create_tree(const char data[])
{
//stub
struct node *root = create_node();
root->parent = NULL;
root->left = NIL;
root->right = NIL;
root->color = BLACK;
set_data(root, data);
return root;
}
// simpler search, quicker
struct node *rbtree_search(struct node *root, const char data[])
{
struct node *p = root;
while (p != NIL) {
int result = cmp(p->data, data);
if (result == 0)
return p;
if (result > 0)
p = p->left;
else
p = p->right;
}
return NIL;
}
struct node *rbtree_insert(struct node *root, const char data[])
{
struct node *p = root;
if (is_leaf(p))
return create_tree(data);
int result;
while (1) {
result = cmp(p->data, data);
if (result == 0)
return p;
else if (result > 0 && !is_leaf(p->left))
p = p->left;
else if (result < 0 && !is_leaf(p->right))
p = p->right;
else
break;
}
struct node *new_node = create_node();
assign_value(new_node, data);
new_node->parent = p;
if (result > 0)
p->left = new_node;
else
p->right = new_node;
// FIX ME
// do jobs to make rb tree rules satisfied
return new_node;
}
struct node *tree = NULL;
const char datas[10][DATALEN] = {"b", "d", "a", "k", "z", "l", "p", "t", "g", "e"};
int main(void)
{
struct node *tree = create_tree((char*)datas[0]);
for (int i = 1; i < 10; i++)
rbtree_insert(tree, datas[i]);
printf("%s\n", rbtree_search(tree, datas[9])->data);
return 0;
}