/
binary_tree.cc
294 lines (252 loc) · 5.86 KB
/
binary_tree.cc
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
/* Implementaion of several binary search tree (BST) functions.
*
* See COPYING for copyright information
* inspired by http://eternallyconfuzzled.com/tuts/datastructures
*/
#include <iostream>
#include "binary_tree.h"
using namespace std;
/* SEARCH */
// NOTE: only returns first found element.
/* find_r: Recursive search
*
* Args: pointer to root, data to search for
* Returns: NULL pointer if not found, pointer to node if found
*
*/
node*
find_r(node *root, double data)
{
if (root == NULL) {
return NULL; // not found
} else if (root->data == data) {
return root; // found
} else {
int dir = root->data < data; // which subtree to follow?
return find_r(root->link[dir], data);
}
}
/* find: Convenience wrapper for find_r
*/
node*
find(tree *tree, double data)
{
return find_r(tree->root, data);
}
/* find_nr: Non-recursive search
*
* Args: pointer to tree, data to search for
* Returns: NULL pointer if not found, pointer to node if found
*/
node*
find_nr(tree *tree, double data)
{
node *it = tree->root;
while (it != NULL) {
if (it->data == data)
return it; // found
else {
int dir = it->data < data; // which direction?
it = it->link[dir];
}
}
return NULL; // not found
}
/* INSERTION */
/* insert_r: Recursive insertion
*
* Args: root pointer, data to store
* Returns: pointer to root
*
*/
node*
insert_r(node *root, double data, unsigned int id)
{
if (root == NULL)
root = make_node(data, id);
// else if (root->data == data) // accept dups
// return root;
else {
int dir = root->data < data;
root->link[dir] = insert_r(root->link[dir], data, id);
}
return root;
}
/* insert: Conveneince wrapper for insert_r
*
* Returns: 1 if successful
*/
bool
insert(tree *tree, double data, unsigned int id)
{
tree->root = insert_r(tree->root, data, id);
return true;
}
/* insert_nr: Non-recursive insertion
*
* Args: pointer to tree, data to insert
* Returns: true if successful, false if not
*
*/
bool
insert_nr(tree *tree, double data, unsigned int id)
{
if (tree->root == NULL) // empty tree
tree->root = make_node(data, id);
else {
node *it = tree->root; // help pointer
int dir;
for (;;) {
dir = it->data < data; // which direction?
if (it->link[dir] == NULL) // we've reached a leaf
break;
/*
if (it->data == data) // already in tree, won't insert duplicates
return false;
else if (it->link[dir] == NULL) // we've reached a leaf, don't continue
break;
*/
it = it->link[dir]; // link to the node we want to insert the data
}
it->link[dir] = make_node(data, id);
}
return true; // successfully inserted data
}
/* REMOVAL */
/* remove: Delete a node from the tree
*
* Args: tree pointer, data to delete
* Returns: true if successful, false if not
*
*/
bool
remove(tree *tree, unsigned int id)
{
if (tree->root != NULL) { //non-empty tree
node head = {0}; // dummy root
node *it = &head; // current position
node *p, *f = NULL; // parent node, help pointer if found
unsigned int dir = 1; // traverse to the right
it->link[1] = tree->root;
while (it->link[dir] != NULL) { // traverse to the bottom
p = it; // save parent node
it = it->link[dir]; // proceed the current pointer
dir = it->id <= id; // which direction?
if (it->id == id)
f = it; // found it
}
if (f != NULL) { // if we found the node to delete
f->data = it->data; // copy
f->id = it->id;
p->link[p->link[1] == it] = it->link[it->link[0] == NULL]; // :)
delete it;
}
tree->root = head.link[1];
if (f == NULL) // node not found
return false;
return true;
}
return false;
}
/* destroy_r: Recurse delete entire tree
*
* Args: pointer to root
* Returns: nothing
*/
void
destroy_r(node *root)
{
if (root != NULL) {
destroy_r(root->link[0]);
destroy_r(root->link[1]);
delete root;
}
}
void
destroy(tree *tree)
{
destroy_r(tree->root);
}
/* destroy_nr: Non-recurse delete entire tree
*
* Args: pointer to tree
*/
void
destroy_nr(tree *tree)
{
node *it = tree->root;
node *save;
while (it != NULL) {
if (it->link[0] != NULL) {
// right rotation
save = it->link[0];
it->link[0] = save->link[1];
save->link[1] = it;
} else {
save = it->link[1];
delete it;
}
it = save;
}
}
/* TRAVERSAL */
// Preorder
void
preorder_r(node *root)
{
if (root != NULL) {
cout << "ID: " << root->id << " MW: "<< root->data << endl;
preorder_r(root->link[0]);
preorder_r(root->link[1]);
}
}
void
preorder(tree *tree)
{
preorder_r(tree->root);
}
// Inorder
void
inorder_r(node *root)
{
if (root != NULL) {
inorder_r(root->link[0]);
cout << "ID: " << root->id << " MW: "<< root->data << endl;
inorder_r(root->link[1]);
}
}
void
inorder(tree *tree)
{
inorder_r(tree->root);
}
// Postorder
void
postorder_r(node *root)
{
if (root != NULL) {
postorder_r(root->link[0]);
postorder_r(root->link[1]);
cout << "ID: " << root->id << " MW: "<< root->data << endl;
}
}
void
postorder(tree *tree)
{
postorder_r(tree->root);
}
/* make_node: Create a node
*
* Args: data
* Returns: pointer to new node
*/
node*
make_node(double data, unsigned int id)
{
node *it = new node;
it->data = data;
it->id = id;
it->link[0] = NULL;
it->link[1] = NULL;
return it;
}