AfterAcademy Tech
•
11 Feb 2020

We have studied what are trees and how to traverse them. But is that enough? Since it is such a strong data structure, it must have a good utility which means various operations could be performed on it.
But what kind of operations? Insertion, deletion and searching of elements are among the most basic operations that most data structures support.
But there are several other operations that can be performed on trees like →
Let us now dive deep in and see how each of these is implemented in trees. For the purposes of this blog, we will just consider binary trees during implementation for simplicity. Also, our primary mode of implementation for the tree here will be using linked representation.
Insertion in a tree can be performed in various ways depending on where you need to insert the new element. We can →
The possibilities are endless…
Among these three, the first method is relatively most difficult to implement so let's try to implement that.
Solution Steps
We first need to select the type of traversal that we shall perform on the tree to determine the vacant place. Let us do this with inorder traversal. Parameters: root, new_node
4. This way, whenever we get an empty spot, we insert the new_node and break the process that instant.
Pseudo-Code
TreeNode insert_node_inorder(TreeNode root, TreeNode new_node)
{
if ( root == NULL )
return new_node
if ( root.left == NULL )
root.left = new_node
else
root.left = insert_node_inorder(root.left, new_node)
if ( root.right == NULL )
root.right = new_node
else
root.right = insert_node_inorder(root.right, new_node)
return root
}
Searching an element in a simple binary tree is quite simple actually. We just check if the current node’s value matches with the given value or not and then recurse to the left and right subtrees repeating the same process.
Pseudo-Code
bool search(TreeNode root, int item)
{
if ( root == NULL )
return 0
if ( root.val == item )
return 1
if ( search(root.left, item) == 1 )
return 1
else if ( search(root.right, item) == 1 )
return 1
else
return 0
}
Deletion of a node in a tree may get a little tricky when you think about what to do with the left child and the right child of that node. We need to follow a certain convention in this process, whatever the programmer sees fit.
To keep it simple, we shall just replace the deleted node with a leaf node. But which leaf node? → Well, you can choose this as you wish too. A simple choice is to find the leftmost or rightmost node of the subtree with the node to be deleted as root.
Although, if the node to be deleted is itself a leaf node, we won’t need to replace it with any node, or if it has only one child, we can use replace the node with its child.
Solution Steps
Our purpose is to accept the root of tree and value item and return the root of the modified tree after deleting the node with value item.
8. Return the root.This is a basic approach and hence searches node by value. It is assumed that all values in the node are unique. Also, keep in mind that a basic binary tree has no rules like Binary Search Tree for the ordering of elements so you can change the convention as per your need.
Pseudo-Code
TreeNode delete_element(TreeNode root, int item)
{
if ( root == NULL )
return root
if ( search(root.left, item) == True )
root.left = delete_element(root.left, item)
else if ( search(root.right, item) == True )
root.right = delete_element(root.right, item)
else if ( root.val == item )
{
// No child exists
if ( root.left == NULL and root.right == NULL )
delete root
// Only one child exists
else if ( root.left == NULL or root.right == NULL )
{
if ( root.left == NULL )
return root.right
else
return root.left
}
// Both left and right child exists
else
{
TreeNode selected_node = root
while ( selected_node.left != NULL )
selected_node = selected_node.left
root.val = selected_node.val
root.left = delete_element(root.left, selected_node.val)
}
}
return root
}
Finding minimum and maximum in a tree can be done via a traversal too. For example, we can just do an inorder traversal of the tree and update the values of min_val and max_val as we traverse the tree.
Let us understand this by a code
void getMinMax(TreeNode root, int& min_val, int& max_val)
{
if ( root == NULL )
return
getMinMax(root.left, min_val, max_val)
min_val = min(min_val, root.val)
max_val = max(max_val, root.val)
getMinMax(root.right, min_val, max_val)
}
→ Here, min_val and max_val are passed by reference and not by value.
This is another operation that can be performed based on traversal only. Can you try this now based on your understanding from the above codes?
Maybe you will write the code something like this →
void getSum(TreeNode root, int& sum)
{
if ( root == NULL )
return
getSum(root.left, sum)
sum += root.val
getSum(root.right, sum)
}
Although, in this case, the code can also be written in this manner
int getSum(TreeNode root)
{
if ( root == NULL )
return NULL
int left_sum = getSum(root.left)
int right_sum = getSum(root.right)
return left_sum + root.val + right_sum
}
Both codes are correct and equally efficient. It depends on the programmer to right either one.
A Tree is a very useful data structure with a lot of applications and is used internally in many technologies around you →
And these are just a few examples of how much tree data structure is used in our daily lives.
Let us list out a few special types of trees with their specific purposes →
AfterAcademy Tech
Binary Search Trees is one of the most important variation of binary tree and is extremely useful in practical applications. The blog discusses the operations and applications of this powerful data structure

AfterAcademy Tech
In this blog, we will learn what an Operating System is and what are the goals of an Operating System. We will also learn the functionalities of an Operating System that helps in achieving the goal of the OS.

AfterAcademy Tech
It is important to know traversal techniques in a tree before solving tree problems. We have discussed the 3 primary traversals in a tree at length in this blog with both recursive and iterative implementations.

AfterAcademy Tech
Stack is a very useful concept that a good programmer could use for their benefit. We will be discussing the basic design of stack and the operations that can be performed on it. We shall be also discussing the implementation of stack.
