-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcpp-class.cpp
60 lines (54 loc) · 1.37 KB
/
cpp-class.cpp
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
class Node {
public:
int key;
Node *left;
Node *right;
Node() {
left = right = nullptr;
}
Node( int k ) {
key = k;
left = right = nullptr;
}
};
class BST {
private:
Node *deleteNode( Node *T, int k) {
if ( T == nullptr ) {
return T;
}
if (k < T->key) {
T->left = deleteNode(T->left, k);
}
else if (k > T->key) {
T->right = deleteNode(T->right, k);
}
else {
if (T->left == nullptr ) {
Node *tmp = new Node;
tmp = T->right;
delete T;
return tmp;
}
else if (T->right == nullptr ) {
Node *tmp = new Node;
tmp = T->left;
delete T;
return tmp;
}
Node *tmp = new Node;
tmp = getMin(T->right);
T->key = tmp->key;
T->right = deleteNode(T->right, tmp->key);
}
return T;
}
public:
Node *root;
BST() {
root = nullptr;
}
Node *deleteNode( int k) {
return deleteNode( root, k );
}
};