binary tree
binary tree
UNIT-III
TREES INTRODUCTION
The tree is a nonlinear hierarchical data structure and comprises a collection of
entities known as nodes. It connects each node in the tree data structure using
"edges”, both directed and undirected.
The image below represents the tree data structure. The blue-colored circles depict
the nodes of the tree and the black lines connecting each node with another are
called edges.
You will understand the parts of trees better, in the terminologies section.
Tree Terminologies
The following are some of the basic tree terms
1
Venkat
MIST, Sathupally
Root Node
Edge
Parent node
Child nod
Siblings
Leaf nodes or external nodes
Internal nodes
Degree
Level
Height
Depth
Path
Subtree
Root
In a tree data structure, the root is the first node of the tree. The root node is
the initial node of the tree in data structures.
In the tree data structure, there must be only one root node.
Edge
In a tree in data structures, the connecting link of any two nodes is called the
edge of the tree data structure.
2
Venkat
MIST, Sathupally
Parent:
In the tree in data structures, the node that is the predecessor of any node is known
as a parent node, or a node with a branch from itself to any other successive node
is called the parent node.
Child
The node, a descendant of any node, is known as child nodes in data
structures.
In a tree, any number of parent nodes can have any number of child
nodes.
In a tree, every node except the root node is a child node.
3
Venkat
MIST, Sathupally
Siblings:
In trees in the data structure, nodes that belong to the same parent are called
siblings.
Leaf :
• Trees in the data structure, the node with no child, is known as a leaf node.
• In trees, leaf nodes are also called external nodes or terminal nodes.
Internal nodes:
• Trees in the data structure have at least one child node known as internal nodes.
• In trees, nodes other than leaf nodes are internal nodes.
4
Venkat
MIST, Sathupally
• Sometimes root nodes are also called internal nodes if the tree has more than one
node.
Degree
• In the tree data structure, the total number of children of a node is called the
degree of the node.
• The highest degree of the node among all the nodes in a tree is called the Degree
of Tree.
Level
In tree data structures, the root node is said to be at level 0, and the root node's
children are at level 1, and the children of that node at level 1 will be level 2, and
so on.
Height
5
Venkat
MIST, Sathupally
o In a tree data structure, the number of edges from the leaf node to the
particular node in the longest path is known as the height of that node.
o In the tree, the height of the root node is called "Height of Tree".
o The tree height of all leaf nodes is 0.
Depth
In a tree, many edges from the root node to the particular node are called the
depth of the tree.
In the tree, the total number of edges from the root node to the leaf node in
the longest path is known as "Depth of Tree".
In the tree data structures, the depth of the root node is 0
Path
• In the tree in data structures, the sequence of nodes and edges from
one node to another node is called the path between those two nodes.
• The length of a path is the total number of nodes in a path.zx
6
Venkat
MIST, Sathupally
Subtree
In the tree in data structures, each child from a node shapes a sub-tree recursively
and every child in the tree will form a sub-tree on its parent node.
General Tree
The general tree is the type of tree where there are no constraints on the
hierarchical structure. Properties
• The general tree follows all properties of the tree data structure.
• A node can have any number of nodes.
7
Venkat
MIST, Sathupally
BINARY TREES
The Binary tree means that the node can have maximum two children. Here, binary
name itself suggests that 'two'; therefore, each node can have either 0, 1 or 2
children.
Let's understand the binary tree through an example.
The above tree is a binary tree because each node contains the utmost two children.
The logical representation of the above tree is given below:
In the above tree, node 1 contains two pointers, i.e., left and a right pointer
pointing to the left and right node respectively. The node 2 contains both the nodes
(left and right node); therefore, it has two pointers (left and right). The nodes 3, 5
and 6 are the leaf nodes, so all these nodes contain NULL pointer on both left and
right parts.
Properties of Binary Tree
At each level of i, the maximum number of nodes is 2i.
8
Venkat
MIST, Sathupally
The height of the tree is defined as the longest path from the root node to the
leaf node. The tree which is shown above has a height equal to 3.
Therefore, the maximum number of nodes at height 3 is equal to (1+2+4+8)
= 15.
In general, the maximum number of nodes possible at height h is (20 + 21 +
22+….2h) = 2h+1 -1.
If the number of nodes is minimum, then the height of the tree would be
maximum.
Conversely, if the number of nodes is maximum, then the height of the tree
would be minimum. If there are 'n' number of nodes in the binary tree.
The minimum height can be computed as:
As we know that,
n = 2h+1 -1
n+1 = 2h+1
Taking log on both the sides,
log2(n+1) = log2(2h+1)
log2(n+1) = h+1
h = log2(n+1) – 1
The maximum height can be computed as:
As we know that,
n = h+1
h= n-1
Types of Binary Tree
There are four types of Binary tree:
Full/ proper/ strict Binary tree
1. Complete Binary tree
2. Perfect Binary tree
3. Degenerate Binary tree
4. Balanced Binary tree
1. Full/ proper/ strict Binary tree
9
Venkat
MIST, Sathupally
The full binary tree is also known as a strict binary tree. The tree can only be
considered as the full binary tree if each node must contain either 0 or 2 children.
The full binary tree can also be defined as the tree in which each node must contain
2 children except the leaf nodes.
Let's look at the simple example of the Full Binary tree.
In the above tree, we can observe that each node is either containing zero or two
children; therefore, it is a Full Binary tree.
Properties of Full Binary Tree
The number of leaf nodes is equal to the number of internal nodes
plus 1. In the above example, the number of internal nodes is 5;
therefore, the number of leaf nodes is equal to 6.
The maximum number of nodes is the same as the number of nodes in
the binary tree, i.e., 2h+1 -1.
The minimum number of nodes in the full binary tree is 2*h-1.
The minimum height of the full binary tree is log2(n+1) - 1.
The maximum height of the full binary tree can be computed as
n= 2*h - 1
n+1 = 2*h
h = n+1/2
1) Complete Binary Tree
10
Venkat
MIST, Sathupally
The complete binary tree is a tree in which all the nodes are completely filled
except the last level. In the last level, all the nodes must be as left as possible. In a
complete binary tree, the nodes should be added from the left.
Let's create a complete binary tree.
The above tree is a complete binary tree because all the nodes are completely
filled, and all the nodes in the last level are added at the left first.
Properties of Complete Binary Tree
The maximum number of nodes in complete binary tree is 2h+1 - 1.
The minimum number of nodes in complete binary tree is 2h.
The minimum height of a complete binary tree is log2(n+1) - 1.
The maximum height of a complete binary tree is
2) Perfect Binary Tree :
A tree is a perfect binary tree if all the internal nodes have 2 children, and all the
leaf nodes are at the same level
11
Venkat
MIST, Sathupally
The below tree is not a perfect binary tree because all the leaf nodes are not at the
same level.
The above tree is a degenerate binary tree because all the nodes have only one
child. It is also known as a right-skewed tree as all the nodes have a right child
only.
12
Venkat
MIST, Sathupally
The above tree is also a degenerate binary tree because all the nodes have only one
child. It is also known as a left-skewed tree as all the nodes have a left child only.
4) Balanced Binary Tree
The balanced binary tree is a tree in which both the left and right trees height
differs by almost 1.
For example, AVL and Red-Black trees are balanced binary tree.
Let's understand the balanced binary tree through examples.
The above tree is a balanced binary tree because the difference between the height
of left subtree and right subtree is zero.
13
Venkat
MIST, Sathupally
The above tree is not a balanced binary tree because the difference between the
height of left subtree and the right subtree is greater than 1.
Tree Traversals:
Unlike linear data structures (Array, Linked List, Queues, Stacks, etc) which have
only one logical way to traverse them, trees can be traversed in different ways.
Following are the generally used ways for traversing trees.
Depth First Traversals:
(a) Inorder (Left, Root, Right)
(b) Preorder (Root, Left, Right)
(c) Postorder (Left, Right, Root)
Breadth-First or Level Order Traversal
Let see each traversal method with example.
a) Inorder Traversal
Algorithm Inorder(tree)
1. Traverse the left subtree, i.e., call Inorder(left-subtree)
2. Visit the root.
3. Traverse the right subtree, i.e., call Inorder(right-subtree)
14
Venkat
MIST, Sathupally
Uses of Inorder
In the case of binary search trees (BST), Inorder traversal gives nodes in non-
decreasing order.
To get nodes of BST in non-increasing order, a variation of Inorder traversal where
Inorder traversal s reversed can be used.
Example:
Example:
Preorder traversal for the above-given figure is 1 2 4 5 3.
c) Postorder Traversal
15
Venkat
MIST, Sathupally
Algorithm Postorder(tree)
1. Traverse the left subtree, i.e., call Postorder(left-subtree)
2. Traverse the right subtree, i.e., call Postorder(right-subtree)
3. Visit the root.
EXAMPLE
Uses of Postorder
Postorder traversal is also useful to get the postfix expression of an expression tree.
Level order traversal of a tree is breadth first traversal for the tree.
16
Venkat
MIST, Sathupally
subtree. Repeat this recursively for all nodes in the tree and construct the tree in the
process.
To illustrate, consider the following inorder and postorder sequence:
Inorder : { 4, 2, 1, 7, 5, 8, 3, 6 }
Postorder : { 4, 2, 7, 8, 5, 6, 3, 1 }
Root would be the last element in the postorder sequence, i.e., 1. Next, locate the
index of the root node in the inorder sequence. Now since 1 is the root node, all
nodes before 1 in the inorder sequence must be included in the left subtree of the
root node, i.e., {4, 2} and all the nodes after 1 must be included in the right subtree,
i.e., {7, 5, 8, 3, 6}. Now the problem is reduced to building the left and right
subtrees and linking them to the root node.
Left subtree:
Inorder : {4, 2}
Postorder : {4, 2}
Right subtree:
Inorder : {7, 5, 8, 3, 6}
Postorder : {7, 8, 5, 6, 3}
The idea is to recursively follow the above approach until the complete tree is
constructed.
The final tree will be
17
Venkat
MIST, Sathupally
19
Venkat
MIST, Sathupally
20
Venkat
MIST, Sathupally
In the above tree, the value of root node is 40, which is greater than its left child 30
but smaller than right child of 30, i.e., 55. So, the above tree does not satisfy the
property of Binary search tree. Therefore, the above tree is not a binary search tree.
Now, let's see the creation of binary search tree using an example.
Suppose the data elements are - 45, 15, 79, 90, 10, 55, 12, 20, 50
First, we have to insert 45 into the tree as the root of the tree.
Then, read the next element; if it is smaller than the root node, insert it
as the root of the left subtree, and move to the next element.
21
Venkat
MIST, Sathupally
Otherwise, if the element is larger than the root node, then insert it as
the root of the right subtree.
Now, let's see the process of creating the Binary search tree using the given data
element. The process of creating the BST is shown below –
Step 1 - Insert 45
As 15 is smaller than 45, so insert it as the root node of the left subtree.
22
Venkat
MIST, Sathupally
As 79 is greater than 45, so insert it as the root node of the right subtree
90 is greater than 45 and 79, so it will be inserted as the right subtree of 79.
23
Venkat
MIST, Sathupally
55 is larger than 45 and smaller than 79, so it will be inserted as the left subtree of
79.
Step 7 - Insert 12
12 is smaller than 45 and 15 but greater than 10, so it will be inserted as the right
subtree of 10.
24
Venkat
MIST, Sathupally
20 is smaller than 45 but greater than 15, so it will be inserted as the right subtree
of 15.
50 is greater than 45 but smaller than 79 and 55. So, it will be inserted as a left
subtree of 55.
Now, the creation of binary search tree is completed. After that, let's move
towards the operations that can be performed on Binary search tree.
We can perform insert, delete and search operations on the binary search tree.
25
Venkat
MIST, Sathupally
a specific order. The steps of searching a node in Binary Search tree are listed as
follows –
1. First, compare the element to be searched with the root element of the tree.
2. If root is matched with the target element, then return the node's location.
3. If it is not matched, then check whether the item is less than the root
element, if it is smaller than the root element, then move to the left subtree.
4. If it is larger than the root element, then move to the right subtree.
6. If the element is not found or not present in the tree, then return NULL.
We are taking the binary search tree formed above. Suppose we have to find node
20 from the below tree.
Step 1:
26
Venkat
MIST, Sathupally
Step 2:
Step 3:
Now, let's see the algorithm to search an element in the Binary search tree.
Now let's understand how the deletion is performed on a binary search tree. We
will also see an example to delete an element from the given tree.
In a binary search tree, we must delete a node from the tree by keeping in mind
that the property of BST is not violated. To delete a node from BST, there are three
possible situations occur –
27
Venkat
MIST, Sathupally
It is the simplest case to delete a node in BST. Here, we have to replace the leaf
node with NULL and simply free the allocated space.
We can see the process to delete a leaf node from BST in the below
image. In below image, suppose we have to delete node 90, as the node to be
deleted is a leaf node, so it will be replaced with NULL, and the allocated space
will free.
In this case, we have to replace the target node (Deleting node) with its child, and
then delete the child node. It means that after replacing the target node with its
child node, the child node will now contain the value to be deleted. So, we simply
have to replace the child node with NULL and free up the allocated space.
28
Venkat
MIST, Sathupally
We can see the process of deleting a node with one child from BST in the below
image.
In the below image, suppose we have to delete the node 79, as the node to be
deleted has only one child, so it will be replaced with its child 55.
So, the replaced node 79 will now be a leaf node that can be easily deleted.
This case of deleting a node in BST is a bit complex among other two cases. In
such a case, the steps to be followed are listed as follows –
The inorder successor is required when the right child of the node is not empty.
We can obtain the inorder successor by finding the minimum element in the
right child of the node.
We can see the process of deleting a node with two children from BST in the
below image. In the below image, suppose we have to delete node 45 that is the
root node, as the node to be deleted has two children, so it will be replaced with its
29
Venkat
MIST, Sathupally
inorder successor. Now, node 45 will be at the leaf of the tree so that it can be
deleted easily.
A new key in BST is always inserted at the leaf. To insert an element in BST, we
have to start searching from the root node; if the node to be inserted is less than the
root node, then search for an empty location in the left subtree. Else, search for the
empty location in the right subtree and insert the data. Insert in BST is similar to
searching, as we always have to maintain the rule that the left subtree is smaller
than the root, and right subtree is larger than the root.
Now, let's see the process of inserting a node into BST using an example.
30
Venkat
MIST, Sathupally
31
Venkat