Key Words : Binary Search Tree in C++, deleting nodes in Binary Search Tree.
Topics at a glance:
- The simplicity of linear data structures – revisiting our custom Array, Queue
- The algorithmic efficiency of non-linear data structures
- Understanding Binary Search Tree (BST) and how to implement one in C++
- What all things to consider in deleting a node in a BST
I am starting chapters on data structures with Binary Search Tree (BST). In fact, I have already discussed some other data structures in previous chapters. For instance, thread-safe queues with our Queue class discussed in chapter 11 on threads and again in our logger implementation discussed in chapter 18. Also, our own implementation of a safe Array class in chapter 8. If we add a custom allocator to our Array class, it will become a custom Vector. As our Array class already has (random access) iterator support, adding an allocator will make it a considerably basic version of std::vector. The main difference between our custom Array class and std::vector is that Array once allocated cannot dynamically shrink or expand its capacity, as it doesn’t have memory allocator functionality, that a std:vector has.
Queues, arrays, and vectors are examples for linear data structures. i.e. the traversal always happens in a linear fashion from one index location to another index location. The generalization of all such data structures can be done on the basis that they all store data in consecutive memory locations. We also have data structures, that stores data in locations that are not necessarily consecutive always. These data structures fall under the category of non-linear data structures. One example is Binary Search Tree (BST). The question is why we need non-linear data structures? Simply because the complexity in sorting, searching, or finding any data in a linear data structure is always O(n), where ‘n’ is the number of data it has stored, or worse. Whereas in non-linear data structures, it is considerably less. Say in case of BST, for sorting, the algorithm complexity is just O(log n).
Binary Search Tree in C++
Binary search tree stores data in nodes. Each node can have a maximum of two children; One left child and one right child. Any BST starts with just one node called the root node. If the data to be entered is less than the root node data, a new node will get created and will be assigned as the root node’s left child. This data will be added to that new node. Similarly, any data that is higher than the root node will be added to a new node created on the right side of the root node. Each of these child nodes, left and right, are themselves a sub-BST and should comply with the same rules for adding new data to their child nodes. This is the philosophy behind BST. There is one more thing. You cannot have duplicate data stored into a BST. By ‘data’, I refer to the unique keys used for uniquely identifying a node in BST.
Now, let us implement one BST and see.
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 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 | #include <iostream> #include <vector> using namespace std; class bst { private: struct node { int key; int data; // optional. Not used in this example. node* left_node; node* right_node; ~node() { cout << "node is deleted" << endl; } }; node *root; node* create_new_node(node** this_node, int key) { if(*this_node == nullptr) { // found a free node *this_node = new node; (*this_node)->left_node = nullptr; (*this_node)->right_node = nullptr; return *this_node; } // start traversing if(key == (*this_node)->key) { return nullptr; // cannot create duplicate key } else if(key < (*this_node)->key) { // traverse left return create_new_node(&((*this_node)->left_node), key); } else if(key > (*this_node)->key) { // traverse right return create_new_node(&((*this_node)->right_node), key); } } void print_keys(node* this_node) { if(this_node != nullptr) { print_keys(this_node->left_node); // start traversing to the left to print // in ascending order cout << "key : " << this_node->key << endl; // print this node key print_keys(this_node->right_node); // now, traverse in the right to print // the higher keys in order } return; } node* find_min_value_node(node *this_node) { node *temp_node = this_node; if( this_node->left_node != nullptr ) { temp_node = find_min_value_node(this_node->left_node); } return temp_node; } void delete_this_key(node **this_node, int key, int& status) { // step 1 : find the node if(*this_node == nullptr) { // reached a null-node cout << "Key not found. Cannot be deleted" << endl; status = -1; return; } if(key == (*this_node)->key) { // found our node status = 0; // now, check the children // case 1 - There are no children. Then simply delete this node if( ( (*this_node)->left_node == nullptr ) && ( (*this_node)->right_node == nullptr ) ) { delete *this_node; *this_node = nullptr; } // case 2 - this_node has one right child else if( (*this_node)->left_node == nullptr ) { // copy the right node byte by byte to this node // and delete the right node // i.e. move the right node to this node node *temp = (*this_node)->right_node; (*this_node)->key = (*this_node)->right_node->key; (*this_node)->left_node = (*this_node)->right_node->left_node; (*this_node)->right_node = (*this_node)->right_node->right_node; // Now, delete the original right node, as it is moved to this node delete temp; } // case 3 - this_node has one left child else if( (*this_node)->right_node == nullptr ) { // copy the left node byte by byte to this node // and delete the left_node node // i.e. move the left_node node to this node node *temp = (*this_node)->left_node; (*this_node)->key = (*this_node)->left_node->key; (*this_node)->left_node = (*this_node)->left_node->left_node; (*this_node)->right_node = (*this_node)->left_node->right_node; // Now, delete the original left node, as it is moved to this node delete temp; } // case 4 - this node has two children both left and right else { node *temp = find_min_value_node( (*this_node)->right_node ); // found a node with minimum value and with no left nodes // copy the key of minimum node to this node and delete that temp node (*this_node)->key = temp->key; // clean the right node link properly // check whether the temp node is actually this_node's right node // in that case this_node's right_node should be set to nullptr if((*this_node)->key == (*this_node)->right_node->key) { // check if this temp node is a leaf node if( (temp->left_node == nullptr ) &&(temp->right_node == nullptr) ) { (*this_node)->right_node = nullptr; } } int temp_status = 0; delete_this_key(&temp, temp->key, temp_status); } } else if(key < (*this_node)->key) { // traverse left delete_this_key(&((*this_node)->left_node), key, status); } else if(key > (*this_node)->key) { // traverse right delete_this_key(&((*this_node)->right_node), key, status); } return; } void get_all_nodes(vector<node*>& nodes, node* this_node) { if(this_node != nullptr) { get_all_nodes(nodes, this_node->left_node); // start traversing to the left to print // in ascending order nodes.push_back(this_node); get_all_nodes(nodes, this_node->right_node); // now, traverse in the right to print // the higher keys in order } return; } public: bst():root{nullptr}{} ~bst() { // get the nodes one by one in to a vector and delete them vector<node*> nodes; get_all_nodes(nodes, root); for (auto this_node : nodes) { delete this_node; } cout << "\nbst is destroyed" << endl; } int insert(int key) { int status = -1; node* new_node = create_new_node(&root, key); if(new_node != nullptr) { // a new node is created new_node->key = key; status = 0; } return status; } void print() { cout << "\nprinting keys..." << endl; print_keys(root); cout << "printing complete\n" << endl; } int delete_key(int key) { int status = 0; delete_this_key(&root, key, status); return status; } }; int main() { bst bst_1; // '1', '5' and '2' are duplicates int unsorted_array[] = {5, 2, 7, 9, 10, 1, 6, 12, 3, 1, 5, 2}; int status = 0; for (auto val : unsorted_array) { status = bst_1.insert(val); if(status == 0) { cout << "val : " << val << " inserted" << endl; } else { cout << "val : " << val << " not inserted" << endl; } } bst_1.print(); // delete 1 status = bst_1.delete_key(1); if(status == 0) { cout << "Val : 1 deleted" << endl; } else { cout << "Val : 1 not deleted" << endl; } bst_1.print(); // delete 9 status = bst_1.delete_key(9); if(status == 0) { cout << "Val : 9 deleted" << endl; } else { cout << "Val : 9 not deleted" << endl; } bst_1.print(); // delete 7 status = bst_1.delete_key(7); if(status == 0) { cout << "Val : 7 deleted" << endl; } else { cout << "Val : 7 not deleted" << endl; } bst_1.print(); // delete 10 status = bst_1.delete_key(10); if(status == 0) { cout << "Val : 10 deleted" << endl; } else { cout << "Val : 10 not deleted" << endl; } bst_1.print(); // try to delete 1 again status = bst_1.delete_key(1); if(status == 0) { cout << "Val : 1 deleted" << endl; } else { cout << "Val : 1 not deleted" << endl; } bst_1.print(); // try to delete 7 again status = bst_1.delete_key(7); if(status == 0) { cout << "Val : 7 deleted" << endl; } else { cout << "Val : 7 not deleted" << endl; } bst_1.print(); cout << "Main ends" << endl; return 0; } |
Let us visualize how the unsorted array data are getting stored in our BST bst_1 as sorted keys:

Some points to consider for understanding how I have tested our BST:
- To show that duplicate keys cannot be created I have used values 1, 5 and 2.
- For deletion purposes I have used the following keys:
- key -1 that has no child,
- key-9 that has one right child
- key 7 that has two children
- key 10 that has one right child
- Also, tried to delete key-1 and key-7 again.
Let us see the result now.
val : 5 inserted
val : 2 inserted
val : 7 inserted
val : 9 inserted
val : 10 inserted
val : 1 inserted
val : 6 inserted
val : 12 inserted
val : 3 inserted
val : 1 not inserted
val : 5 not inserted
val : 2 not inserted
printing keys...
key : 1
key : 2
key : 3
key : 5
key : 6
key : 7
key : 9
key : 10
key : 12
printing complete
node is deleted
Val : 1 deleted
printing keys...
key : 2
key : 3
key : 5
key : 6
key : 7
key : 9
key : 10
key : 12
printing complete
node is deleted
Val : 9 deleted
printing keys...
key : 2
key : 3
key : 5
key : 6
key : 7
key : 10
key : 12
printing complete
node is deleted
Val : 7 deleted
printing keys...
key : 2
key : 3
key : 5
key : 6
key : 10
key : 12
printing complete
node is deleted
Val : 10 deleted
printing keys...
key : 2
key : 3
key : 5
key : 6
key : 12
printing complete
Key not found. Cannot be deleted
Val : 1 not deleted
printing keys...
key : 2
key : 3
key : 5
key : 6
key : 12
printing complete
Key not found. Cannot be deleted
Val : 7 not deleted
printing keys...
key : 2
key : 3
key : 5
key : 6
key : 12
printing complete
Main ends
node is deleted
node is deleted
node is deleted
node is deleted
node is deleted
bst is destroyed
Deleting nodes in Binary Search Tree
BST is always tricky when it comes to deleting a node as you cannot simply go and find the node and delete it, as it breaks the links in between and the BST will become useless.
We must consider three cases during a node deletion:
- Node has no child. In that case simply delete that node
- Node has one child. Either a left child or a right child. In this case you can move that one child to the ‘node’ including the child’s key, left and right child node pointers if any, and then delete that child node. In effect, the node to be deleted has become its child node and the original child node got deleted. Remember the move assignment operation we have done in chapter 6? This has a resemblance with that. Hasn’t it?
- Node has two children. In this case, we must first find the minimum value node that has no children of its own, or at least, there should not be a left child. We must copy that minimum value node’s key to this node.
- There is a special case here, i.e. if that minimum value node and our current node ‘s right child node are the same. In this special case we need to check one extra condition. i.e. whether this minimum value node is a leaf node. A leaf node in a BST is a node with no left and right children. In this case, our current node’s right child should be made as nullptr, otherwise this link will be broken and will lead to errors in course of execution.
Enjoyed the chapter? Let me know in the comments below. Thanks!😊