|
| 1 | +// C++ program to delete element in binary tree |
| 2 | +#include <bits/stdc++.h> |
| 3 | +using namespace std; |
| 4 | + |
| 5 | +/* A binary tree node has key, pointer to left |
| 6 | +child and a pointer to right child */ |
| 7 | +struct Node { |
| 8 | + int key; |
| 9 | + struct Node *left, *right; |
| 10 | +}; |
| 11 | + |
| 12 | +/* function to create a new node of tree and |
| 13 | +return pointer */ |
| 14 | +struct Node* newNode(int key) |
| 15 | +{ |
| 16 | + struct Node* temp = new Node; |
| 17 | + temp->key = key; |
| 18 | + temp->left = temp->right = NULL; |
| 19 | + return temp; |
| 20 | +}; |
| 21 | + |
| 22 | +/* Inorder traversal of a binary tree*/ |
| 23 | +void inorder(struct Node* temp) |
| 24 | +{ |
| 25 | + if (!temp) |
| 26 | + return; |
| 27 | + inorder(temp->left); |
| 28 | + cout << temp->key << " "; |
| 29 | + inorder(temp->right); |
| 30 | +} |
| 31 | + |
| 32 | +/* function to delete the given deepest node |
| 33 | +(d_node) in binary tree */ |
| 34 | +void deletDeepest(struct Node* root, |
| 35 | + struct Node* d_node) |
| 36 | +{ |
| 37 | + queue<struct Node*> q; |
| 38 | + q.push(root); |
| 39 | + |
| 40 | + // Do level order traversal until last node |
| 41 | + struct Node* temp; |
| 42 | + while (!q.empty()) { |
| 43 | + temp = q.front(); |
| 44 | + q.pop(); |
| 45 | + if (temp == d_node) { |
| 46 | + temp = NULL; |
| 47 | + delete (d_node); |
| 48 | + return; |
| 49 | + } |
| 50 | + if (temp->right) { |
| 51 | + if (temp->right == d_node) { |
| 52 | + temp->right = NULL; |
| 53 | + delete (d_node); |
| 54 | + return; |
| 55 | + } |
| 56 | + else |
| 57 | + q.push(temp->right); |
| 58 | + } |
| 59 | + |
| 60 | + if (temp->left) { |
| 61 | + if (temp->left == d_node) { |
| 62 | + temp->left = NULL; |
| 63 | + delete (d_node); |
| 64 | + return; |
| 65 | + } |
| 66 | + else |
| 67 | + q.push(temp->left); |
| 68 | + } |
| 69 | + } |
| 70 | +} |
| 71 | + |
| 72 | +/* function to delete element in binary tree */ |
| 73 | +Node* deletion(struct Node* root, int key) |
| 74 | +{ |
| 75 | + if (root == NULL) |
| 76 | + return NULL; |
| 77 | + |
| 78 | + if (root->left == NULL && root->right == NULL) { |
| 79 | + if (root->key == key) |
| 80 | + return NULL; |
| 81 | + else |
| 82 | + return root; |
| 83 | + } |
| 84 | + |
| 85 | + queue<struct Node*> q; |
| 86 | + q.push(root); |
| 87 | + |
| 88 | + struct Node* temp; |
| 89 | + struct Node* key_node = NULL; |
| 90 | + |
| 91 | + // Do level order traversal to find deepest |
| 92 | + // node(temp) and node to be deleted (key_node) |
| 93 | + while (!q.empty()) { |
| 94 | + temp = q.front(); |
| 95 | + q.pop(); |
| 96 | + |
| 97 | + if (temp->key == key) |
| 98 | + key_node = temp; |
| 99 | + |
| 100 | + if (temp->left) |
| 101 | + q.push(temp->left); |
| 102 | + |
| 103 | + if (temp->right) |
| 104 | + q.push(temp->right); |
| 105 | + } |
| 106 | + |
| 107 | + if (key_node != NULL) { |
| 108 | + int x = temp->key; |
| 109 | + deletDeepest(root, temp); |
| 110 | + key_node->key = x; |
| 111 | + } |
| 112 | + return root; |
| 113 | +} |
| 114 | + |
| 115 | +// Driver code |
| 116 | +int main() |
| 117 | +{ |
| 118 | + struct Node* root = newNode(10); |
| 119 | + root->left = newNode(11); |
| 120 | + root->left->left = newNode(7); |
| 121 | + root->left->right = newNode(12); |
| 122 | + root->right = newNode(9); |
| 123 | + root->right->left = newNode(15); |
| 124 | + root->right->right = newNode(8); |
| 125 | + |
| 126 | + cout << "Inorder traversal before deletion : "; |
| 127 | + inorder(root); |
| 128 | + |
| 129 | + int key = 11; |
| 130 | + root = deletion(root, key); |
| 131 | + |
| 132 | + cout << endl; |
| 133 | + cout << "Inorder traversal after deletion : "; |
| 134 | + inorder(root); |
| 135 | + |
| 136 | + return 0; |
| 137 | +} |
0 commit comments