-
Notifications
You must be signed in to change notification settings - Fork 0
/
deletion_in_binary_tree.cpp
82 lines (68 loc) · 2.08 KB
/
deletion_in_binary_tree.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#include <iostream>
// Define a structure for a binary tree node
struct TreeNode {
int data;
TreeNode* left;
TreeNode* right;
TreeNode(int value) : data(value), left(nullptr), right(nullptr) {}
};
// Helper function to find the minimum value node in a tree
TreeNode* findMin(TreeNode* node) {
while (node->left != nullptr) {
node = node->left;
}
return node;
}
// Function to delete a node with the given key from the binary tree
TreeNode* deleteNode(TreeNode* root, int key) {
if (root == nullptr) {
return root;
}
if (key < root->data) {
root->left = deleteNode(root->left, key);
} else if (key > root->data) {
root->right = deleteNode(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 = findMin(root->right);
root->data = temp->data;
root->right = deleteNode(root->right, temp->data);
}
return root;
}
// Inorder traversal to print the elements of the binary tree
void inorderTraversal(TreeNode* root) {
if (root) {
inorderTraversal(root->left);
std::cout << root->data << " ";
inorderTraversal(root->right);
}
}
int main() {
TreeNode* root = nullptr;
// Insert elements into the binary tree
root = insert(root, 50);
root = insert(root, 30);
root = insert(root, 70);
root = insert(root, 20);
root = insert(root, 40);
root = insert(root, 60);
root = insert(root, 80);
std::cout << "Binary tree before deletion: ";
inorderTraversal(root);
std::cout << std::endl;
int keyToDelete = 30;
root = deleteNode(root, keyToDelete);
std::cout << "Binary tree after deleting " << keyToDelete << ": ";
inorderTraversal(root);
std::cout << std::endl;
return 0;
}