Basic Operations on Binary Trees
Introduction
Binary trees are a versatile data structure that allows for efficient operations like searching, insertion, and deletion. In this post, we’ll explore the core operations used to manipulate binary trees, along with traversal methods that are key to utilizing binary trees effectively.
Basic Operations on Binary Trees
1. Insertion
Inserting a new node into a binary tree involves placing the node in its correct position, maintaining the structure of the binary tree.
Video Explanation

Example in C++:
// Insert function
Node* insert(Node* root, int val) {
if (root == nullptr) {
return new Node(val); // Inserting at an empty spot
}
if (val < root->data) {
root->left = insert(root->left, val);//Traversing to left sub-tree
} else {
root->right = insert(root->right, val);//Traversing to right sub-tree
}
return root;
}
Inserting E at the right of B
A A
/ \ / \
B C ------> B C
/ / \ / \ / \
D F G D E F G
2. Deletion
In a binary tree, when deleting a node, the node to be deleted is replaced by the deepest node in the tree. This approach ensures that the tree remains complete. The deletion process involves the following steps:
1. Identify the Deepest Node:
Traverse the binary tree to find the deepest node (the node that is the last in the level order traversal). This node will be used to replace the node being deleted.
2. Replace the Node:
Replace the value of the node to be deleted with the value of the deepest node.
3. Delete the Deepest Node:
Remove the deepest node from the tree. Since it is a leaf node, you can simply delete it.