This repository was archived by the owner on Oct 6, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 70
Expand file tree
/
Copy pathbst.cpp
More file actions
178 lines (156 loc) · 3.31 KB
/
bst.cpp
File metadata and controls
178 lines (156 loc) · 3.31 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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
#include <iostream>
#include <cassert>
using namespace std;
template <typename K, typename V>
struct bstnode {
K k;
V v;
bstnode<K, V> *left;
bstnode<K, V> *right;
};
template <typename K, typename V>
bstnode<K, V> *new_node(K k, V v)
{
auto b = new bstnode<K, V>;
b->k = k;
b->v = v;
b->left = b->right = NULL;
return b;
}
template <typename K, typename V>
struct bst {
bstnode<K, V> *root;
};
template <typename K, typename V>
bst<K, V> empty()
{
bst<K, V> b;
b.root = NULL;
return b;
}
template <typename K, typename V>
struct search_result {
bstnode<K, V> *parent;
bstnode<K, V> *actual;
};
template <typename K, typename V>
static search_result<K, V> search(bstnode<K, V> *p, K key)
{
bstnode<K, V> *q = p;
while (p && p->k != key) {
q = p;
if (p->k < key) {
p = p->right;
} else {
p = p->left;
}
}
return { q, p };
}
template <typename K, typename V>
bool member(const bst<K, V> &d, K key)
{
auto r = search(d.root, key);
return r.actual != NULL;
}
template <typename K, typename V>
V lookup(const bst<K, V> &d, K key)
{
auto r = search(d.root, key);
assert(r.actual);
return (r.actual)->v;
}
template <typename K, typename V>
void insert(bst<K, V> &d, K key, V value)
{
/* insert into the empty tree. */
if (!d.root) {
d.root = new_node(key, value);
return;
}
auto r = search(d.root, key);
if (r.actual) {
(r.actual)->v = value;
return;
}
/* If we are here, r.actual == NULL. */
auto n = new_node(key, value);
if ((r.parent)->k < key) {
(r.parent)->right = n;
} else {
(r.parent)->left = n;
}
}
template <typename K, typename V>
bstnode<K, V> *least(bstnode<K, V>* root)
{
while (root->left) { root = root->left; }
return root;
}
template <typename K, typename V>
void remove(bst<K, V> &d, K key)
{
auto r = search(d.root, key);
auto q = r.actual;
auto p = r.parent;
assert(q);
if (!q->left && !q->right) { // q does not have any child.
if (p->left == q) {
p->left = NULL;
} else if (p->right == q) {
p->right = NULL;
}
return;
}
if (!q->left || !q->right) { // q has exactly one child.
bstnode<K, V> *zs;
if (q->left) { zs = q->left; }
else { zs = q->right; }
if (p->left == q) {
p->left = zs;
} else if (p->right == q) {
p->right = zs;
}
return;
}
// q has both children.
auto t = least(q->right);
auto new_k = t->k;
auto new_v = t->v;
remove(d, new_k);
q->k = new_k;
q->v = new_v;
}
template <typename K, typename V>
void print0(const bstnode<K, V> *p)
{
if (!p) return;
cout << p->k << ": " << p->v << ", ";
print0(p->left);
print0(p->right);
}
template <typename K, typename V>
void print(const bst<K, V>& d)
{
cout << "{";
print0(d.root);
cout << "}\n";
}
int main()
{
auto d = empty<int, int>();
insert(d, 15, 0);
insert(d, 10, 0);
insert(d, 9, 0);
insert(d, 13, 0);
insert(d, 30, 0);
insert(d, 17, 0);
insert(d, 25, 0);
insert(d, 16, 0);
insert(d, 18, 0);
insert(d, 21, 0);
print(d);
remove(d, 17);
print(d);
return 0;
}