-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtree.c
189 lines (166 loc) · 4.07 KB
/
tree.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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
/*
* Binary tree implementation for M68K
*/
#include "tree.h"
struct tnode *tnode_rol(struct tnode *p) {
struct tnode *root = p->right;
if (root->left == NULL) {
root->left = p;
p->right = NULL;
} else {
p->right = root->left;
root->left = p;
}
p->nodes = 1 + tnode_count(p->left) + tnode_count(p->right);
root->nodes = 1 + tnode_count(root->left) + tnode_count(root->right);
return root;
}
struct tnode *tnode_ror(struct tnode *p) {
struct tnode *root = p->left;
if (root->right == NULL) {
root->right = p;
p->left = NULL;
} else {
p->left = root->right;
root->right = p;
}
p->nodes = 1 + tnode_count(p->left) + tnode_count(p->right);
root->nodes = 1 + tnode_count(root->left) + tnode_count(root->right);
return root;
}
void tnode_print(struct tnode *p, int num_spaces) {
int i;
if (p == NULL)
return;
for (i = 0; i < num_spaces; i++)
putch(' ');
putstr(p->key);
putstr(" (");
putshort(p->nodes);
putstr(" nodes)\n");
tnode_print(p->left, num_spaces + 1);
tnode_print(p->right, num_spaces + 1);
}
/* floor of log base 2 of x */
unsigned int_log2(unsigned x) {
int index = 30;
while (!(x & (1 << index)) && index) index--;
if (x != 0)
index++;
return index;
}
struct tnode *tnode_balance(struct tnode *p) {
if (p == NULL)
return NULL;
int left = int_log2(tnode_count(p->left));
int right = int_log2(tnode_count(p->right));
if (left - right > 1) { // more on the left side
left = int_log2(tnode_count(p->left->left));
right = int_log2(tnode_count(p->left->right));
if (left - right > 0) {
p = tnode_ror(p);
} else if (right - left > 0) {
p->left = tnode_rol(p->left);
p = tnode_ror(p);
}
} else if (right - left > 1) {
left = tnode_count(p->left);
right = tnode_count(p->right);
if (right - left > 0) {
p = tnode_rol(p);
} else if (left - right > 0) {
p->right = tnode_ror(p->right);
p = tnode_rol(p);
}
}
return p;
}
/* insert a tnode into the binary tree. Overwrites previous contents. */
struct tnode *tnode_insert(struct tnode *p, const char *key, void *value) {
if(p == NULL) {
/* insert [new] tnode as root node */
p = (struct tnode *)kmalloc(sizeof(struct tnode));
p->key = strdup(key);
p->value = value;
p->left = p->right = NULL;
p->nodes = 1;
return p;
}
/* Traverse the tree to get a pointer to the specific tnode */
/* The child of this tnode will be the [new] tnode */
int compare = strcmp(p->key, key);
if (compare == 1)
p->left = tnode_insert(p->left, key, value);
else if (compare == -1)
p->right = tnode_insert(p->right, key, value);
else {
p->value = value; // overwrite...
}
p->nodes = tnode_count(p->left) + tnode_count(p->right) + 1;
p = tnode_balance(p);
return p;
}
/* returns the total number of tree nodes */
int tnode_count(struct tnode *p) {
if(p == NULL)
return 0;
else {
return p->nodes;
}
}
/* locate a value in the btree */
struct tnode *tnode_search(struct tnode *p, const char *key) {
struct tnode *temp;
temp = p;
while(temp != NULL) {
int compare = strcmp(temp->key, key);
if(!compare)
return temp;
else if(compare == 1)
temp = temp->left;
else
temp = temp->right;
}
return NULL;
}
void tnode_startswith(struct tnode *p, struct linked_list **ll, const char *key) {
struct tnode *temp = p;
if (p == NULL)
return;
int compare = strstartswith(p->key, key);
if (!compare) {
tnode_startswith(p->left, ll, key);
linked_list_push(ll, p->key);
tnode_startswith(p->right, ll, key);
} else if (compare == 1)
tnode_startswith(p->left, ll, key);
else
tnode_startswith(p->right, ll, key);
}
/* locate a minimum value in the btree */
struct tnode *tnode_searchmin(struct tnode *p) {
if(p == NULL)
return NULL;
else {
if(p->left == NULL)
return p;
else
return tnode_searchmin(p->left);
}
}
/* locate a maximum value in the btree */
struct tnode *tnode_searchmax(struct tnode *p) {
if(p != NULL)
while(p->right != NULL)
p = p->right;
return p;
}
/* destroy the binary tree */
void tnode_destroy(struct tnode *p) {
if(p != NULL) {
tnode_destroy(p->left);
tnode_destroy(p->right);
kfree(p->key);
kfree(p);
}
}