CbinarySearchTree

  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
#include <iostream>
class CbinarySearchTree {
struct node {
int data;
node* left;
node* right;
};
/**
* Stores root of tree
*
* @type node*
*/
node* root;
public:
/**
* Constructor of class CbinarySearchTree
*/
CbinarySearchTree();
/**
* Destructor of class CbinarySearchTree
*/
~CbinarySearchTree();
/**
* Print all node of tree
*
* @return void
*/
const void print();
/**
* Insert to tree a node
*
* @param int
* @return void
*/
void insert(const int);
/**
* Search for match value key in tree
*
* @param int
* @return bool
*/
bool search(const int);
/**
* Delete node from tree
*
* @param int
* @return void
*/
void deleteNode(const int);
/**
* Free all memory, which is allocated
*
* @return void
*/
void clear();
};
CbinarySearchTree::CbinarySearchTree() {
root = NULL;
}
CbinarySearchTree::~CbinarySearchTree() {
this->clear();
}
void CbinarySearchTree::clear() {
if (root == NULL) {
delete root;
return;
}
node* tempLeft = root->left;
node* tempRight = root->right;
delete root;
root = tempLeft;
clear();
root = tempRight;
clear();
}
const void CbinarySearchTree::print() {
node* saved = root;
if (root == NULL) {
std::cout << "NIL" << std::endl;
} else {
std::cout<<root->data<<std::endl;
root = saved->left;
print();
root = saved->right;
print();
}
}
void CbinarySearchTree::insert(const int key) {
node* newNode = new node;
newNode->right = NULL;
newNode->left = NULL;
newNode->data = key;
if (root == NULL) {
root = newNode;
} else {
node* temp = root;
node* last = NULL;
while (NULL != temp) {
if (temp->data > key) {
last = temp;
temp = temp->left;
} else if (temp->data < key) {
last = temp;
temp = temp->right;
} else if (temp->data == key) {
std::cout << "This key exists" << std::endl;
return;
}
}
if (newNode->data < last->data) {
last->left = newNode;
} else {
last->right = newNode;
}
}
}
bool CbinarySearchTree::search(const int key) {
node* temp = root;
while (NULL != temp) {
if (key > temp->data) {
temp = temp->right;
} else if (key < temp->data) {
temp = temp->left;
} else if (key == temp->data) {
return true;
}
}
return false;
}
void CbinarySearchTree::deleteNode(const int key) {
if (false == search(key)) {
std::cout<<"Not match key"<<std::endl;
return;
}
node* parent;
node* matchNode = root;
while (key != matchNode->data) {
parent = matchNode;
if (matchNode->data < key) {
matchNode = matchNode->right;
} else if (matchNode->data > key) {
matchNode = matchNode->left;
}
}
if (NULL == matchNode->left && NULL == matchNode->right) {
if (parent->data > key) {
parent->left = NULL;
} else {
parent->right = NULL;
}
delete matchNode;
} else if (NULL == matchNode->left && NULL != matchNode->right) {
if (parent->data > matchNode->data) {
parent->left = matchNode->right;
} else {
parent->right = matchNode->right;
}
} else if (NULL != matchNode->left && NULL == matchNode->right) {
if (parent->data > matchNode->data) {
parent->left = matchNode->left;
} else {
parent->right = matchNode->left;
}
} else if (NULL != matchNode->left && NULL != matchNode->right) {
node* temp = matchNode->left;
node* parentTemp;
while (NULL != temp) {
parentTemp = temp;
temp = temp->right;
}
int saved = parentTemp->data;
deleteNode(parentTemp->data);
matchNode->data = saved;
}
}
int main() {
CbinarySearchTree root;
root.insert(4);
root.insert(6);
root.insert(3);
root.insert(9);
root.insert(5);
root.deleteNode(6);
root.print();
return 0;
}