二叉查找树

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
struct {
int value;
int cnt;
Node* lson;
Node* rson;
Node* parent;
int pos;
Node() {
value = -1;
lson = rson = parent = nullptr;
cnt = pos = 0;
}
Node(int _value, Node* _parent, int _pos) :value(_value), parent(_parent), pos(_pos) {
lson = rson = nullptr;
cnt = 0;
}
};
class BST {
private:
Node* root;
void _insert(Node *node, int x) {
if (x == node->value) {
node->cnt++;
return;
}
if (x < node->value) {
if (node->lson == nullptr) {
node->lson = new Node(x, node, 1);
}
_insert(node->lson, x);
}
else {
if (node->rson == nullptr) {
node->rson = new Node(x, node, -1);
}
_insert(node->rson, x);
}
}
Node* _find(Node *node, int x) {
if (node == nullptr || x == node->value) {
return node;
}
return _find(x < node->value ? node->lson : node->rson, x);
}
void _delete(Node *node) {
Node* temp;
if (node->lson != nullptr && node->rson != nullptr) {
temp = node->lson;
while (temp->rson != nullptr) {
temp = temp->rson;
}
node->value = temp->value;
node->cnt = temp->cnt;
_delete(temp);
return;
}
if (node->lson == nullptr && node->rson == nullptr) {
if (node->pos == 0) {
root = nullptr;
}
else {
(node->pos == 1 ? node->parent->lson : node->parent->rson) = nullptr;
}
}
else {
temp = (node->lson == nullptr ? node->rson : node->lson);
if (node->pos == 0) {
root = temp;
root->parent = root;
root->pos = 0;
}
else {
temp->pos = node->pos;
temp->parent = node->parent;
(node->pos == 1 ? node->parent->lson : node->parent->rson) = temp;
}
}
delete node;
}
public:
void Insert(int x) {
if (root == nullptr) {
root = new Node(x, root, 0);
root->cnt = 1;
return;
}
_insert(root, x);
}
bool Find(int x) {
return _find(root, x) != nullptr;
}
bool Delete(int x) {
Node* n = _find(root, x);
if (n == nullptr) {
return false;
}
if (--n->cnt == 0) {
_delete(n);
}
return true;
}
};