-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.c
More file actions
161 lines (130 loc) · 3.38 KB
/
main.c
File metadata and controls
161 lines (130 loc) · 3.38 KB
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
157
158
159
160
161
// It is a plain C code (not C++)
#include <stddef.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
typedef void *(*method)(void *);
typedef struct Node {
char *key;
method val;
struct Node *left, *right;
char color;
} Node;
int isRed(const Node *const n) {
if (n == NULL) return 0;
return 'R' == n->color;
}
int isEmpty(const Node *const n) {
return NULL == n;
}
// Get function pointer by key
method get(const Node *const root, const char *const key) {
const Node *n = root;
while (NULL != n) {
int cmp = strcmp(key, n->key);
if (cmp < 0) {
n = n->left;
} else if (cmp > 0) {
n = n->right;
} else {
return n->val;
}
}
return NULL;
}
Node *rotateRight(Node *const h) {
Node *x = h->left;
h->left = x->right;
x->right = h;
x->color = x->right->color;
x->right->color = 'R';
return x;
}
Node *rotateLeft(Node *const h) {
Node *x = h->right;
h->right = x->left;
x->left = h;
x->color = x->left->color;
x->left->color = 'R';
return x;
}
void flipColor(Node *const h) {
h->color = 'R' == h->color ? 'B' : 'R';
h->left->color = 'R' == h->left->color ? 'B' : 'R';
h->right->color = 'R' == h->right->color ? 'B' : 'R';
}
Node *_put(Node *h, const char *const key, const method val) {
if (NULL == h) {
// Create new Node
Node *node = (Node *) malloc(sizeof(Node));
node->left = node->right = NULL;
node->key = (char *) malloc(strlen(key) + 1);
strcpy(node->key, key);
node->val = val;
node->color = 'R';
return node;
}
int cmp = strcmp(key, h->key);
if (cmp < 0) {
h->left = _put(h->left, key, val);
} else if (cmp > 0) {
h->right = _put(h->right, key, val);
} else {
h->val = val;
}
// Fix tree structure
if (isRed(h->right) && !isRed(h->left)) h = rotateLeft(h);
if (isRed(h->left) && isRed(h->left->left)) h = rotateRight(h);
if (isRed(h->left) && isRed(h->right)) flipColor(h);
return h;
}
void put(Node **h, const char *const key, const method val) {
Node *root = _put(*h, key, val);
root->color = 'B';
*h = root;
}
void destroyTree(Node **h) {
if (NULL == *h) return;
destroyTree(&(*h)->left);
destroyTree(&(*h)->right);
free((*h)->key);
free(*h);
*h = NULL;
}
// Print ordered keys
void testPrintKeys(const Node *const n) {
if (NULL == n) return;
testPrintKeys(n->left);
printf("%s", n->key);
testPrintKeys(n->right);
}
// Just test Functions
// ------------------------------------------
void *testFuncA(void *s) {
printf("A -> %s\n", (char *) s);
return s;
}
void *testFuncB(void *s) {
printf("B -> %s\n", (char *) s);
return s;
}
void *testFuncC(void *s) {
printf("C -> %s\n", (char *) s);
return s;
}
// ------------------------------------------
int main() {
// Test our realisation RedBlack tree
Node *root = NULL;
char *testMessage = "Test Message";
put(&root, "Test1A", &testFuncA);
put(&root, "Test1C", &testFuncC);
put(&root, "Test1B", &testFuncB);
(*get(root, "Test1A"))(testMessage);
(*get(root, "Test1B"))(testMessage);
(*get(root, "Test1C"))(testMessage);
put(&root, "Test1A", &testFuncC);
(*get(root, "Test1A"))(testMessage);
destroyTree(&root);
return 0;
}