-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create InsertionAndDeletationInBST.cpp
Here Insertion and deletion of a Node in Binary search tree is given
- Loading branch information
1 parent
8884af1
commit 9435b42
Showing
1 changed file
with
91 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
#include <iostream> | ||
using namespace std; | ||
|
||
struct Node { | ||
int data; | ||
Node* left; | ||
Node* right; | ||
Node(int val) : data(val), left(NULL), right(NULL) {} | ||
}; | ||
|
||
// Helper function to find the minimum value node in a subtree | ||
Node* findMin(Node* root) { | ||
while (root->left != NULL) root = root->left; | ||
return root; | ||
} | ||
|
||
Node* deleteValue(Node* root, int key) { | ||
// Base case: if the tree is empty | ||
if (root == NULL) return root; | ||
|
||
// Recursive case: traverse the tree to find the node to delete | ||
if (key < root->data) { | ||
root->left = deleteValue(root->left, key); | ||
} else if (key > root->data) { | ||
root->right = deleteValue(root->right, key); | ||
} else { | ||
// Node with only one child or no child | ||
if (root->left == NULL) { | ||
Node* temp = root->right; | ||
delete root; | ||
return temp; | ||
} else if (root->right == NULL) { | ||
Node* temp = root->left; | ||
delete root; | ||
return temp; | ||
} | ||
|
||
// Node with two children: get the inorder successor (smallest in the right subtree) | ||
Node* temp = findMin(root->right); | ||
|
||
// Copy the inorder successor's content to this node | ||
root->data = temp->data; | ||
|
||
// Delete the inorder successor | ||
root->right = deleteValue(root->right, temp->data); | ||
} | ||
|
||
return root; | ||
} | ||
|
||
// Helper function to perform inorder traversal | ||
void inorder(Node* root) { | ||
if (root == NULL) return; | ||
inorder(root->left); | ||
cout << root->data << " "; | ||
inorder(root->right); | ||
} | ||
|
||
int main() { | ||
Node* root = new Node(50); | ||
root->left = new Node(30); | ||
root->right = new Node(70); | ||
root->left->left = new Node(20); | ||
root->left->right = new Node(40); | ||
root->right->left = new Node(60); | ||
root->right->right = new Node(80); | ||
|
||
cout << "Inorder traversal of the given tree: "; | ||
inorder(root); | ||
cout << endl; | ||
|
||
cout << "\nDelete 20\n"; | ||
root = deleteValue(root, 20); | ||
cout << "Inorder traversal of the modified tree: "; | ||
inorder(root); | ||
cout << endl; | ||
|
||
cout << "\nDelete 30\n"; | ||
root = deleteValue(root, 30); | ||
cout << "Inorder traversal of the modified tree: "; | ||
inorder(root); | ||
cout << endl; | ||
|
||
cout << "\nDelete 50\n"; | ||
root = deleteValue(root, 50); | ||
cout << "Inorder traversal of the modified tree: "; | ||
inorder(root); | ||
cout << endl; | ||
|
||
return 0; | ||
} |