0% found this document useful (0 votes)
6 views7 pages

Deleting a node from a Binary Search Tree

The document explains the insertion and deletion processes for a Binary Search Tree (BST) in C++. It details the steps for inserting a node recursively and outlines three scenarios for deleting a node based on the number of children. Additionally, it provides example C++ code and discusses the time and space complexities associated with these operations.

Uploaded by

tommyassefa95
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views7 pages

Deleting a node from a Binary Search Tree

The document explains the insertion and deletion processes for a Binary Search Tree (BST) in C++. It details the steps for inserting a node recursively and outlines three scenarios for deleting a node based on the number of children. Additionally, it provides example C++ code and discusses the time and space complexities associated with these operations.

Uploaded by

tommyassefa95
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

1.

Binary Search Tree (BST) – Insertion


Insertion of a Node in a Tree in C++
In a Binary Search Tree (BST):

 The left child of a node contains values less than the node.
 The right child of a node contains values than the node.

The insertion process is straightforward, and it is typically done recursively.

Insertion Process in BST

1. Start at the root: If the tree is empty, the new node becomes the root.
2. Recursively search the tree:
o If the value to be inserted is less than the current node, move to the
left child.
o If the value to be inserted is greater than the current node, move to
the right child.
3. Insert the new node: When a NULL (empty) child is found, place the new
node there.

2. Deleting a node from a Binary Search Tree


(BST)
Deleting a node from a Binary Search Tree (BST) involves three
primary scenarios, each requiring specific steps to maintain the BST
properties:

1. Node with No Children (Leaf Node): Simply remove the node


from the tree.
2. Node with One Child: Replace the node with its only child.
3. Node with Two Children: Replace the node's value with its in-
order successor (the smallest value in the right subtree) or in-order
predecessor (the largest value in the left subtree), and then delete
the successor or predecessor node.

Step-by-Step Implementation in C++:

1. Define the Node Structure:


struct Node {
int key;
Node* left;
Node* right;
Node(int x) : key(x), left(nullptr), right(nullptr) {}
};

2. Search for the Node to Delete:


o Start at the root and traverse the tree to find the node
containing the key to be deleted.
o Compare the key with the current node's key:
 If the key is less, move to the left child.
 If the key is greater, move to the right child.
 If the key matches, proceed to the deletion process.
3. Handle the Deletion Based on the Node's Children:
o Node with No Children (Leaf Node):
 Simply remove the node by setting its parent's reference
to it as nullptr.
o Node with One Child:
 Replace the node with its child by adjusting the parent's
reference to point to the child.
o Node with Two Children:
 Find the in-order successor (or predecessor):
 The in-order successor is the node with the
smallest key in the right subtree.
 The in-order predecessor is the node with the
largest key in the left subtree.
 Replace the node's value with the in-order successor's
(or predecessor's) value.
 Recursively delete the in-order successor (or
predecessor) node, which will have at most one child.

Example Code in C++:


#include <iostream>

using namespace std;

struct Node {

int key;

Node* left;

Node* right;

/* we can use member intializer list Node(int


k):key(k),left(nuiiPtr),right(nullPtr){} */

//the below is constructor decleration.

Node(int k){

key = k;

left = right = nullptr;

};

// Note that it is not a generic inorder

// successor function. It mainly works

// when right child is not empty which is

// the case we need in BST delete

Node* getSuccessor(Node* curr){


curr = curr->right;

while (curr != nullptr && curr->left != nullptr)

curr = curr->left;

return curr;

// This function deletes a given key x from

// the give BST and returns modified root of

// the BST (if it is modified)

Node* delNode(Node* root, int x){

// Base case

if (root == nullptr)

return root;

// If key to be searched is in a subtree

if (root->key > x)

root->left = delNode(root->left, x);

else if (root->key < x)

root->right = delNode(root->right, x);

// If root matches with the given key or root->key == x.

else {
// Cases when root has 0 children

// or only right child

if (root->left == nullptr) {

Node* temp = root->right;

delete root;

return temp;

// When root has only left child

if (root->right == nullptr) {

Node* temp = root->left;

delete root;

return temp;

// When both children are present

Node* succ = getSuccessor(root);

root->key = succ->key;

root->right = delNode(root->right, succ->key);

return root;

}
// Utility function to do inorder

// traversal(sorted)

void inorder(Node* root){

if (root != nullptr) {

inorder(root->left);

cout << root->key << " ";

inorder(root->right);

int main(){

Node* root = new Node(10);

root->left = new Node(5);

root->right = new Node(15);

root->right->left = new Node(12);

root->right->right = new Node(18);

int x = 15;

root = delNode(root, x);

inorder(root);

return 0;

}
Output: 5 10 12 18.

Time Complexity:

 In the worst case, the time complexity of deletion is O (h), where h


is the height of the tree.
 In a balanced BST, h is O (log n), making the deletion operation O
(log n).
 In an unbalanced BST, h can be O (n), making the deletion
operation O (n).

Space Complexity:

 The space complexity is O (h) due to the recursion stack used


during the traversal.
 Recursive Approach: O (h), due to the call stack used during recursion.
 Iterative Approach: O (1), as it uses constant space

You might also like