Download as pdf or txt
Download as pdf or txt
You are on page 1of 4

Let's go through the process of deleting a node in a Binary Search Tree (BST) using

C++ code, and discuss the structure of a BST class. We will also use tree diagrams
to illustrate the deletion process.

### Deleting a Node in BST

When deleting a node in a BST, we consider three main cases:


1. **Node is a leaf** (no children): Simply remove the node.
2. **Node has one child**: Remove the node and replace it with its child.
3. **Node has two children**: Find the inorder successor (smallest node in the right
subtree), replace the node's value with the successor's value, and delete the
successor.

**Example BST**:
```
50
/ \
30 70
/ \ / \
20 40 60 80
```

**Deleting a Node (50)**:


- Inorder successor of 50 is 60.

**Steps**:
1. Replace 50 with 60.
2. Delete node 60.

**Resulting BST**:
```
60
/ \
30 70
/ \ \
20 40 80
```

### C++ Code for Remove

Here is a C++ implementation of the remove function and the BST class.

```cpp
#include <iostream>
using namespace std;

struct TreeNode {
int value;
TreeNode* left;
TreeNode* right;
TreeNode(int x) : value(x), left(nullptr), right(nullptr) {}
};

class BST {
public:
TreeNode* root;

BST() : root(nullptr) {}

void insert(int key) {


root = insertRec(root, key);
}

void remove(int key) {


root = removeRec(root, key);
}

void inorder() {
inorderRec(root);
}

private:
TreeNode* insertRec(TreeNode* node, int key) {
if (node == nullptr)
return new TreeNode(key);

if (key < node->value)


node->left = insertRec(node->left, key);
else if (key > node->value)
node->right = insertRec(node->right, key);

return node;
}

TreeNode* minValueNode(TreeNode* node) {


TreeNode* current = node;
while (current && current->left != nullptr)
current = current->left;
return current;
}

TreeNode* removeRec(TreeNode* root, int key) {


if (root == nullptr) return root;

if (key < root->value)


root->left = removeRec(root->left, key);
else if (key > root->value)
root->right = removeRec(root->right, key);
else {
if (root->left == nullptr) {
TreeNode* temp = root->right;
delete root;
return temp;
} else if (root->right == nullptr) {
TreeNode* temp = root->left;
delete root;
return temp;
}

TreeNode* temp = minValueNode(root->right);


root->value = temp->value;
root->right = removeRec(root->right, temp->value);
}
return root;
}

void inorderRec(TreeNode* root) {


if (root != nullptr) {
inorderRec(root->left);
cout << root->value << " ";
inorderRec(root->right);
}
}
};

int main() {
BST bst;
bst.insert(50);
bst.insert(30);
bst.insert(20);
bst.insert(40);
bst.insert(70);
bst.insert(60);
bst.insert(80);

cout << "Inorder traversal of the given tree \n";


bst.inorder();
cout << endl;

cout << "\nDelete 20\n";


bst.remove(20);
bst.inorder();
cout << endl;

cout << "\nDelete 30\n";


bst.remove(30);
bst.inorder();
cout << endl;

cout << "\nDelete 50\n";


bst.remove(50);
bst.inorder();
cout << endl;

return 0;
}
```

### Explanation

1. **Insertion**:
- The `insertRec` function inserts a node in the BST while maintaining the BST
property.
- It places the new node in the correct position based on the value.

2. **Deletion**:
- The `removeRec` function handles node deletion.
- It identifies the node to be deleted, handles the three cases (leaf node, one
child, two children), and updates the tree structure accordingly.

3. **Inorder Traversal**:
- The `inorderRec` function prints the values of nodes in ascending order (left,
root, right).

### Summary Table

| Concept | Description
| Example Traversal/Operation
|
|-----------------------|-----------------------------------------------------------
--------------------------------------|---------------------------------------------
-----------|
| Insertion | Adds a node to the BST while maintaining its properties
| Insert 50, 30, 70, 20, 40, 60, 80
|
| Deletion (Node=50) | Removes a node while maintaining BST properties; handles
three main cases | Deletes 50, replaces with 60, resulting in
`60, 30, 70, 20, 40, 80` |
| Inorder Traversal | Visits nodes in ascending order (left, root, right)
| `20, 30, 40, 50, 60, 70, 80` before deletion
|
| BST Class Structure | Defines the BST class with insertion, deletion, and
traversal functions | `class BST { public: TreeNode* root;
... }` |

Feel free to ask for more details or any specific parts you'd like to delve deeper
into!

You might also like