-
Notifications
You must be signed in to change notification settings - Fork 0
/
BSTMap.h
239 lines (206 loc) · 4.12 KB
/
BSTMap.h
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
#include<iostream>
#include"BST.h"
using namespace std;
template<typename K, typename V>
class BSTMap {
struct Node {
Node(K k,V v) {
this->key = k;
this->value = v;
this->left = nullptr;
this->right = nullptr;
}
K key;
V value;
Node* left;
Node* right;
};
private:
Node* root;
int size;
public:
BSTMap() {
root = nullptr;
size = 0;
}
int getSize() {
return size;
}
bool isEmpty() {
return size == 0;
}
void add(K key,V value) {
root = _add(root, key, value);
}
Node* _add(Node* node, K key, V value) {
if (node == nullptr) {
size++;
return new Node(key, value);
}
if (node->key == key) {
node->value = value;
}
else if (node->key > key) {
node->left = _add(node->left, key, value);
}
else {
node->right = _add(node->right, key, value);
}
return node;
}
Node* getNode(Node* node, K key) {
if (node == nullptr) {
return nullptr;
}
if (node->key == key) {
return node;
}
else if (node->key > key) {
return getNode(node->left, key);
}
else {
return getNode(node->right, key);
}
}
bool contains(K key) {
return getNode(root, key) != nullptr;
}
V get(K key) {
Node* node = getNode(root, key);
return node == nullptr ? nullptr : node->value;
}
void set(K key, V value) {
Node* node = getNode(root, key);
if (node == nullptr) {
cout << "本树没有这个key" << endl;
return;
}
node->value = value;
}
V minimum() {
if (size == 0) {
cout << "本树为空" << endl;
assert(false);
}
return _minimum(root)->value;
}
Node* _minimum(Node* node) {
if (node->left == nullptr) {
return node;
}
return _minimum(node->left);
}
V maximum() {
if (size == 0) {
cout << "本树为空" << endl;
assert(false);
}
return _maximum(root)->value;
}
Node* _maximum(Node* node) {
if (node->right == nullptr) {
return node;
}
return _maximum(node->right);
}
V removeMin() {
V ret = minimum();
root = _removeMin(root);
return ret;
}
Node* _removeMin(Node* node) {
if (node->left == nullptr) {
Node* rightnode = node->right;
node->right = nullptr;
size--;
return rightnode;
}
node->left = _removeMin(node->left);
return node;
}
V removeMax() {
V ret = maximum();
root = _removeMax(root);
return ret;
}
Node* _removeMax(Node* node) {
if (node->right == nullptr) {
Node* leftnode = node->left;
node->left = nullptr;
size--;
return leftnode;
}
node->right = _removeMax(node->right);
return node;
}
V remove(K key) {
Node* node = getNode(root, key);
if (node != nullptr) {
root = _remove(root, key);
return node->value;
}
}
Node* _remove(Node* node, K key) {
if (node == nullptr) {
return nullptr;
}
if (key > node->key) {
node->right = _remove(node->right, key);
return node;
}
else if (key < node->key) {
node->left = _remove(node->left, key);
return node;
}
else {
if (node->left == nullptr) {
Node* rightnode = node->right;
node->right = nullptr;
size--;
return rightnode;
}
if (node->right == nullptr) {
Node* leftnode = node->left;
node->left = nullptr;
size--;
return leftnode;
}
Node* successor = _minimum(node->right);
successor->right = _removeMin(node->right);
successor->left = node->left;
node->left = nullptr;
node->right = nullptr;
return successor;
}
}
void inOrder() {
_inOrder(root);
}
void _inOrder(Node* node) {
if (node == nullptr) {
return;
}
_inOrder(node->left);
cout << node->key <<":"<<node->value<< endl;
_inOrder(node->right);
}
//基于中序遍历实现key值获取
K* keyset() {
LoopQueue<Node*> q;
q.enqueue(root);
int j = 0;
K* temp = new K[size];
while (!q.isEmpty()) {
Node* cur = q.dequeue();
temp[j] = cur->key;
if (cur->left != nullptr) {
q.enqueue(cur->left);
}
if (cur->right != nullptr) {
q.enqueue(cur->right);
}
j++;
}
return temp;
}
};