Tree Types
Tree Types
on
DATA STRUCTURES
2021 – 2022
Sitapur Road,Lucknow
Uttar Pradesh
India
Pin Code: 226021
INSTITUTE OF ENGINEERING
&TECHNOLOGY
Disclaimer: The e-content is exclusively meant for academic purposes and for enhancing teaching
and learning. Any other use for economic/commercial purpose is strictly prohibited. The users of the
content shall not distribute, disseminate or share it with anyone else and its use is restricted to
advancement of individual knowledge.
The information provided in this e-content is developed from authentic references, to the best of my
knowledge
Binary Tree
As you can see in the picture given above, a node can have less than 2 children but not more
than that
A complete binary tree also holds some important properties. So, let's look at them.
The parent of node i is ⌊i2⌋⌊i2⌋. For example, the parent of node 4 is 2 and the parent of node
5 is also 2.
The left child of node i is 2i2i.
The right child of node i is 2i+12i+1
Perfect Binary Tree → In a perfect binary tree, each leaf is at the same level and all the
interior nodes have two children.
In other words, A perfect binary tree is a type of binary tree in which every internal node has
exactly two child nodes and all the leaf nodes are at the same level.
Thus, a perfect binary tree will have the maximum number of nodes for all alternative binary
trees of the same height and it will be 2h+1−12h+1−1 which we are going to prove next.
We can also get the parent, the right child and the left child using the properties of a complete
binary tree we have discussed above i.e., for a node i, the parent is ⌊i2⌋⌊i2⌋, the left child
is 2i2i and the right child is 2i+12i+1.
So, we represented a complete binary tree using an array and saw how to get the parent and
children of any node. Let's discuss about doing the same for an incomplete binary tree.
Array Representation of Incomplete Binary Tree
To represent an incomplete binary tree with an array, we first assume that all the nodes are
present to make it a complete binary tree and then number the nodes as shown in the picture
given below.
Array index is a value in tree nodes and array value gives to the parent node of that particular
index or node. Value of the root node index is always -1 as there is no parent for root. When
the data item of the tree is sorted in an array, the number appearing against the node will
work as indexes of the node in an array.
The above figure shows how a binary tree is represented as an array. Value '7' is the total
number of nodes. If any node does not have any of its child, null value is stored at the
corresponding index of the array.