Binary Tree and BST
Binary Tree and BST
Binary tree is a special tree data structure in which each node can have at most 2
children.
Thus, in a binary tree,
Each node has either 0 child or 1 child or 2 children.
Example-
Similarly,
Every other unlabeled structure gives rise to 6 different labeled structures.
Thus, in total 30 different labeled binary trees are possible.
Types of Binary Trees-
Binary trees can be of the following types-
Here,
First binary tree is not a full binary tree.
This is because node C has only 1 child.
Example-
Here,
First binary tree is not a complete binary tree.
This is because all the leaf nodes are not at the same level.
Here,
First binary tree is not an almost complete binary tree.
This is because the last level is not filled from left to right.
Example-
Property-01:
Minimum number of nodes in a binary tree of height H
=H+1
Example-
To construct a binary tree of height = 4, we need at least 4 + 1 = 5 nodes.
Property-02:
Example-
Maximum number of nodes in a binary tree of height 3
= 23+1 – 1
= 16 – 1
= 15 nodes
Thus, in a binary tree of height = 3, maximum number of nodes that can be inserted = 15.
Example-
Consider the following binary tree-
Here,
Number of leaf nodes = 3
Number of nodes with 2 children = 2
Clearly, number of leaf nodes is one greater than number of nodes with 2 children.
This verifies the above relation.
NOTE
It is interesting to note that-
Number of leaf nodes in any binary tree depends only on the number of nodes with 2
children.
Property-04:
Maximum number of nodes at any level ‘L’ in a binary tree
= 2L
Example-
Maximum number of nodes at level-2 in a binary tree
= 22
=4
Thus, in a binary tree, maximum number of nodes that can be present at level-2 = 4.
Tree Traversal-
Tree Traversal refers to the process of visiting each node in a tree data structure exactly
once.
Applications-
Preorder traversal is used to get prefix expression of an expression tree.
Preorder traversal is used to create a copy of the tree.
2. Inorder Traversal-
Algorithm-
Traverse the left sub tree i.e. call Inorder (left sub tree)
1. Visit the root
2. Traverse the right sub tree i.e. call Inorder (right sub tree)
Example-
Consider the following example-
Application-
Inorder traversal is used to get infix expression of an expression tree.
3. Postorder Traversal-
Algorithm-
1. Traverse the left sub tree i.e. call Postorder (left sub tree)
2. Traverse the right sub tree i.e. call Postorder (right sub tree)
3. Visit the root
Left → Right → Root
Example-
Consider the following example-
Applications-
Postorder traversal is used to get postfix expression of an expression tree.
Postorder traversal is used to delete the tree.
This is because it deletes the children first and then it deletes the parent.
Breadth First Traversal of a tree prints all the nodes of a tree level by level.
Breadth First Traversal is also called as Level Order Traversal.
Example-
Application-
Level order traversal is used to print the data in the same order as stored in the array
representation of a complete binary tree.
If the binary tree in figure is traversed in inorder, then the order in which the nodes will be
visited is ____?
Solution-
The inorder traversal will be performed as-
Problem-02:
Which of the following sequences denotes the postorder traversal sequence of the tree
shown in figure?
1. FEGCBDBA
2. GCBDAFE
3. GCDBFEA
4. FDEGCBA
Problem-03:
Let LASTPOST, LASTIN, LASTPRE denote the last vertex visited in a postorder, inorder
and preorder traversal respectively of a complete binary tree. Which of the following is
always true?
1. LASTIN = LASTPOST
2. LASTIN = LASTPRE
3. LASTPRE = LASTPOST
4. None of these
Solution-
Consider the following complete binary tree-
Preorder Traversal : B , A , E
Inorder Traversal :B,A,E
Postorder Traversal : B , E , A
Problem-04:
Which of the following binary trees has its inorder and preorder traversals as BCAD and
ABCD respectively-
Solution-
Option (D) is correct
6/17/2021 Binary Tree and its Types | Data Structure Tutorial | Studytonight
A binary tree is a hierarchical data structure in which each node has at most two children generally referred as left child and right child.
The topmost node in the tree is called the root. An empty tree is represented by NULL pointer.
https://www.studytonight.com/data-structures/introduction-to-binary-trees 1/4
6/17/2021 Binary Tree and its Types | Data Structure Tutorial | Studytonight
In the above binary tree we see that root node is A. The tree has 10 nodes with 5 internal nodes, i.e, A,B,C,E,G and 5 external nodes,
i.e, D,F,H,I,J. The height of the tree is 3. B is the parent of D and E while D and E are children of B.
Advantages of Trees
Trees are so useful and frequently used, because they have some very serious advantages:
Rooted binary tree: It has a root node and every node has atmost two children.
Full binary tree: It is a tree in which every node in the tree has either 0 or 2 children.
The number of nodes, n, in a full binary tree is atleast n = 2h – 1, and atmost n = 2h+1 – 1, where h is the height of the tree.
The number of leaf nodes l, in a full binary tree is number, L of internal nodes + 1, i.e, l = L+1.
Perfect binary tree: It is a binary tree in which all interior nodes have two children and all leaves have the same depth or same level.
https://www.studytonight.com/data-structures/introduction-to-binary-trees 2/4
6/17/2021 Binary Tree and its Types | Data Structure Tutorial | Studytonight
https://www.studytonight.com/data-structures/introduction-to-binary-trees 3/4
6/17/2021 Binary Tree and its Types | Data Structure Tutorial | Studytonight
https://www.studytonight.com/data-structures/introduction-to-binary-trees 4/4
6/28/2021 Binary Tree Traversal | Inorder, Preorder and Postorder
Inorder Traversal: 7 9 4 2 5 1 3 6 8
Preorder Traversal: 1 2 4 7 9 5 3 6 8
Postorder Traversal: 9 7 4 5 2 8 6 3 1
Inorder Traversal: For binary search trees (BST), Inorder Traversal specifies the nodes in non-
descending order. In order to obtain nodes from BST in non-increasing order, a variation of inorder
traversal may be used where inorder traversal is reversed.
https://www.faceprep.in/data-structures/tree-traversals-inorder-preorder-and-postorder/ 1/5
6/28/2021 Binary Tree Traversal | Inorder, Preorder and Postorder
Preorder Traversal: Preorder traversal will create a copy of the tree. Preorder Traversal is also used
to get the prefix expression of an expression.
Postorder Traversal: Postorder traversal is used to get the postfix expression of an expression given
Preorder(root)
Visit and print the root node.
Traverse the left sub-tree, (recursively call inorder(root -> left).
Traverse the right sub-tree, (recursively call inorder(root -> right).
Postorder(root)
Traverse the left sub-tree, (recursively call inorder(root -> left).
Traverse the right sub-tree, (recursively call inorder(root -> right).
Visit and print the root node.
Program for binary tree traversals in inorder, preorder, and postorder traversals is given below.
https://www.faceprep.in/data-structures/tree-traversals-inorder-preorder-and-postorder/ 2/5
6/28/2021 Binary Tree Traversal | Inorder, Preorder and Postorder
#include <iostream>
#include <stdlib.h>
struct node
int data;
};
return temp;
};
void insert_node(struct node *root, int n1, int n2, char lr)
if(root == NULL)
return;
switch(lr)
break;
break;
else
if(root == NULL)
return;
https://www.faceprep.in/data-structures/tree-traversals-inorder-preorder-and-postorder/ 3/5
6/28/2021 Binary Tree Traversal | Inorder, Preorder and Postorder
if(root == NULL)
return;
if(root == NULL)
return;
/* Main Function */
int main()
int n;
cin >> n;
cout << "\nInput the nodes of the binary tree in order \n\nparent-child-left(or)right-\n\n";
while(n--)
char lr;
int n1,n2;
cin >>lr;
if(root == NULL)
root = newNode(n1);
switch(lr)
break;
break;
else
insert_node(root,n1,n2,lr);
inorder(root);
https://www.faceprep.in/data-structures/tree-traversals-inorder-preorder-and-postorder/ 4/5
6/28/2021 Binary Tree Traversal | Inorder, Preorder and Postorder
preorder(root);
postorder(root);
return 0;
https://www.faceprep.in/data-structures/tree-traversals-inorder-preorder-and-postorder/ 5/5
Binary Search Tree-
Binary Search Tree is a special kind of binary tree in which nodes are arranged in a
specific order.
Example-
If three distinct keys are A, B and C, then 5 distinct binary search trees are-
Let us understand the construction of a binary search tree using the following example-
Example-
Construct a Binary Search Tree (BST) for the following sequence of numbers-
50, 70, 60, 20, 90, 10, 40, 100
When elements are given in a sequence,
Always consider the first element as the root node.
Consider the given elements and insert them in the BST one by one.
Insert 70-
As 70 > 50, so insert 70 to the right of 50.
Insert 60-
As 60 > 50, so insert 60 to the right of 50.
As 60 < 70, so insert 60 to the left of 70.
Insert 20-
As 20 < 50, so insert 20 to the left of 50.
Insert 90-
As 90 > 50, so insert 90 to the right of 50.
As 90 > 70, so insert 90 to the right of 70.
Insert 10-
As 10 < 50, so insert 10 to the left of 50.
As 10 < 20, so insert 10 to the left of 20.
Insert 40-
As 40 < 50, so insert 40 to the left of 50.
As 40 > 20, so insert 40 to the right of 20.
Insert 100-
As 100 > 50, so insert 100 to the right of 50.
As 100 > 70, so insert 100 to the right of 70.
As 100 > 90, so insert 100 to the right of 90.
This is the required Binary Search Tree.
Problem-02:
How many distinct binary search trees can be constructed out of 4 distinct keys?
1. 5
2. 14
3. 24
4. 35
Solution-
Number of distinct binary search trees possible with 4 distinct keys
= 2nCn / n+1
= 2×4C4 / 4+1
= 8C4 / 5
= 14
Thus, Option (B) is correct.
Binary Search Tree Operations-
Commonly performed operations on binary search tree are-
1. Search Operation
2. Insertion Operation
3. Deletion Operation
1. Search Operation-
Search Operation is performed to search a particular element in the Binary Search Tree.
Rules-
For searching a given key in the BST,
Compare the key with the value of root node.
If the key is present at the root node, then return the root node.
If the key is greater than the root node value, then recur for the root node’s right subtree.
If the key is smaller than the root node value, then recur for the root node’s left subtree.
Example-
Consider key = 45 has to be searched in the given BST-
We start our search from the root node 25.
As 45 > 25, so we search in 25’s right subtree.
As 45 < 50, so we search in 50’s left subtree.
As 45 > 35, so we search in 35’s right subtree.
As 45 > 44, so we search in 44’s right subtree but 44 has no subtrees.
So, we conclude that 45 is not present in the above BST.
2. Insertion Operation-
Insertion Operation is performed to insert an element in the Binary Search Tree.
Rules-
The insertion of a new key always takes place as the child of some leaf node.
For finding out the suitable leaf node,
Search the key to be inserted from the root node till some leaf node is reached.
Once a leaf node is reached, insert the key as child of that leaf node.
Example-
Consider the following example where key = 40 is inserted in the given BST-
When it comes to deleting a node from the binary search tree, following three cases are
possible-
Example-
Consider the following example where node with value = 20 is deleted from the BST-
Example-
Consider the following example where node with value = 30 is deleted from the BST-
Method-01:
Visit to the right subtree of the deleting node.
Pluck the least value element called as inorder successor.
Replace the deleting element with its inorder successor.
Example-
Consider the following example where node with value = 15 is deleted from the BST-
Method-02:
Visit to the left subtree of the deleting node.
Pluck the greatest value element called as inorder predecessor.
Replace the deleting element with its inorder predecessor.
Example-
Consider the following example where node with value = 15 is deleted from the BST-
Time Complexity-
Time complexity of all BST Operations = O(h).
Here, h = Height of binary search tree
Now, let us discuss the worst case and best case.
Worst Case-
In worst case,
The binary search tree is a skewed binary search tree.
Height of the binary search tree becomes n.
So, Time complexity of BST Operations = O(n).
In this case, binary search tree is as good as unordered list with no benefits.
Best Case-
In best case,
The binary search tree is a balanced binary search tree.
Height of the binary search tree becomes log(n).
So, Time complexity of BST Operations = O(logn).
Example-
Consider the following binary search tree-
Now, let us write the traversal sequences for this binary search tree-
Preorder Traversal-
100, 20 , 10 , 30 , 200 , 150 , 300
Inorder Traversal-
10, 20 , 30 , 100 , 150 , 200 , 300
Postorder Traversal-
10, 30, 20, 150, 300 , 200 , 100
2. In a binary search tree, the value of all the nodes in the left sub-tree is less than the value of
the root.
3. Similarly, value of all the nodes in the right sub-tree is greater than or equal to the value of the
root.
4. This rule will be recursively applied to all the left and right sub-trees of the root.
A Binary search tree is shown in the above figure. As the constraint applied on the BST, we can see
that the root node 30 doesn't contain any value greater than or equal to 30 in its left sub-tree and it
also doesn't contain any value less than 30 in its right sub-tree.
https://www.javatpoint.com/binary-search-tree 1/8
8/16/2021 Binary Search Tree - javatpoint
1. Searching become very efficient in a binary search tree since, we get a hint at each step, about
which sub-tree contains the desired element.
2. The binary search tree is considered as efficient data structure in compare to arrays and linked
lists. In searching process, it removes half sub-tree at every step. Searching for an element in a
binary search tree takes o(log2n) time. In worst case, the time it takes to search an element is
0(n).
3. It also speed up the insertion and deletion operations as compare to that in array and linked
list.
Q. Create the binary search tree using the following data elements.
2. Read the next element, if it is lesser than the root node element, insert it as the root of the left
sub-tree.
The process of creating BST by using the given elements, is shown in the image below.
https://www.javatpoint.com/binary-search-tree 2/8
8/16/2021 Binary Search Tree - javatpoint
https://www.javatpoint.com/binary-search-tree 3/8
8/16/2021 Binary Search Tree - javatpoint
SN Operation Description
1 Searching Finding the location of some specific element in a binary search tree.
in BST
2 Insertion in Adding a new element to the binary search tree at the appropriate location so
BST that the property of BST do not violate.
3 Deletion in Deleting some specific node from a binary search tree. However, there can be
BST various cases in deletion depending upon the number of children, the node
have.
#include <iostream>
#include <stdlib.h>
using namespace std;
struct Node {
int data;
Node *left;
Node *right;
};
Node* create(int item)
{
Node* node = new Node;
node->data = item;
node->left = node->right = NULL;
return node;
}
void inorder(Node *root)
{
if (root == NULL)
return;
https://www.javatpoint.com/binary-search-tree 4/8
8/16/2021 Binary Search Tree - javatpoint
inorder(root->left);
cout<< root->data << " ";
inorder(root->right);
}
Node* findMinimum(Node* cur)
{
while(cur->left != NULL) {
cur = cur->left;
}
return cur;
}
Node* insertion(Node* root, int item)
{
if (root == NULL)
return create(item);
if (item < root->data)
root->left = insertion(root->left, item);
else
root->right = insertion(root->right, item);
return root;
}
void search(Node* &cur, int item, Node* &parent)
{
while (cur != NULL && cur->data != item)
{
parent = cur;
if (item < cur->data)
cur = cur->left;
else
cur = cur->right;
}
}
https://www.javatpoint.com/binary-search-tree 5/8
8/16/2021 Binary Search Tree - javatpoint
void deletion(Node*& root, int item)
{
Node* parent = NULL;
Node* cur = root;
search(cur, item, parent);
if (cur == NULL)
return;
if (cur->left == NULL && cur->right == NULL)
{
if (cur != root)
{
if (parent->left == cur)
parent->left = NULL;
else
parent->right = NULL;
}
else
root = NULL;
free(cur);
}
else if (cur->left && cur->right)
{
Node* succ = findMinimum(cur- >right);
int val = succ->data;
deletion(root, succ->data);
cur->data = val;
}
else
https://www.javatpoint.com/binary-search-tree 6/8
8/16/2021 Binary Search Tree - javatpoint
{
Node* child = (cur->left)? Cur- >left: cur->right;
if (cur != root)
{
if (cur == parent->left)
parent->left = child;
else
parent->right = child;
}
else
root = child;
free(cur);
}
}
int main()
{
Node* root = NULL;
int keys[8];
for(int i=0;i<8;i++)
{
cout << "Enter value to be inserted";
cin>>keys[i];
root = insertion(root, keys[i]);
}
inorder(root);
cout<<"\n";
deletion(root, 10);
inorder(root);
return 0;
}
Output:
https://www.javatpoint.com/binary-search-tree 7/8
8/16/2021 Binary Search Tree - javatpoint
5 5 10 15 20 25 30 40
5 5 15 20 25 30 40
https://www.javatpoint.com/binary-search-tree 8/8
8/17/2021 Deletion in Binary Search Tree - javatpoint
Deletion
Delete function is used to delete the specified node from a binary search tree. However, we must
delete a node from a binary search tree in such a way, that the property of binary search tree doesn't
violate. There are three situations of deleting a node from binary search tree.
In the following image, we are deleting the node 85, since the node is a leaf node, therefore the
node will be replaced with NULL and allocated space will be freed.
In the following image, the node 12 is to be deleted. It has only one child. The node will be replaced
with its child node and the replaced node 12 (which is now leaf node) will simply be deleted.
https://www.javatpoint.com/deletion-in-binary-search-tree 1/2
8/17/2021 Deletion in Binary Search Tree - javatpoint
In the following image, the node 50 is to be deleted which is the root node of the tree. The in-order
traversal of the tree given below.
replace 50 with its in-order successor 52. Now, 50 will be moved to the leaf of the tree, which will
simply be deleted.
https://www.javatpoint.com/deletion-in-binary-search-tree 2/2