Tree
Tree
Tree is a collection of elements called Nodes, where each node can have arbitrary
number of children.
Field Description
Root is a special node in a tree. The entire tree is referenced through
Root
it. It does not have a parent.
Parent Node Parent node is an immediate predecessor of a node.
Child Node All immediate successors of a node are its children.
Siblings Nodes with the same parent are called Siblings.
Path is a number of successive edges from source node to destination
Path
node.
Height of Height of a node represents the number of edges on the longest path
Node between that node and a leaf.
Height of
Height of tree represents the height of its root node.
Tree
Depth of Depth of a node represents the number of edges from the tree's root
Node node to the node.
Degree of
Degree of a node represents a number of children of a node.
Node
Edge is a connection between one node to another. It is a line between
Edge
two nodes or a node and a leaf.
In the above figure, D, F, H, G are leaves. B and C are siblings. Each node
excluding a root is connected by a direct edge from exactly one other node
parent → children.
Levels of a node
Levels of a node represents the number of connections between the node and the
root. It represents generation of a node. If the root node is at level 0, its next node
is at level 1, its grand child is at level 2 and so on. Levels of a node can be shown
as follows:
Note:
- A tree can be empty with no nodes or a tree consists of one node called the Root.
Applications of Trees
Trees and their variants are an extremely useful data structure with lots of practical
applications.
There is one and only one path between every pair of vertices in a tree.
A tree with n vertices has exactly (n-1) edges.
A graph is a tree if and only if it is minimally connected.
Any connected graph with n vertices and (n-1) edges is a tree.
Tree Terminology-
The first node from where the tree originates is called as a root node.
In any tree, there must be only one root node.
We can never have multiple root nodes in a tree data structure.
Example-
Here, node A is the only root node.
2. Edge-
Example-
3. Parent-
The node which has a branch from it to any other node is called as a parent
node.
In other words, the node which has one or more children is called as a parent
node.
In a tree, a parent node can have any number of child nodes.
Example-
Here,
4. Child-
Example-
Here,
5. Siblings-
Example-
Here,
6. Degree-
Here,
Degree of node A = 2
Degree of node B = 3
Degree of node C = 2
Degree of node D = 0
Degree of node E = 2
Degree of node F = 0
Degree of node G = 1
Degree of node H = 0
Degree of node I = 0
Degree of node J = 0
Degree of node K = 0
7. Internal Node-
The node which has at least one child is called as an internal node.
Internal nodes are also called as non-terminal nodes.
Every non-leaf node is an internal node.
Example-
8. Leaf Node-
The node which does not have any child is called as a leaf node.
Leaf nodes are also called as external nodes or terminal nodes.
Example-
Here, nodes D, I, J, F, K and H are leaf nodes.
9. Level-
Example-
10. Height-
Total number of edges that lies on the longest path from any leaf node to a
particular node is called as height of that node.
Height of a tree is the height of root node.
Height of all leaf nodes = 0
Example-
Here,
Height of node A = 3
Height of node B = 2
Height of node C = 2
Height of node D = 0
Height of node E = 1
Height of node F = 0
Height of node G = 1
Height of node H = 0
Height of node I = 0
Height of node J = 0
Height of node K = 0
11. Depth-
Total number of edges from root node to a particular node is called as depth
of that node.
Depth of a tree is the total number of edges from root node to a leaf node in
the longest path.
Depth of the root node = 0
The terms “level” and “depth” are used interchangeably.
Example-
Here,
Depth of node A = 0
Depth of node B = 1
Depth of node C = 1
Depth of node D = 2
Depth of node E = 2
Depth of node F = 2
Depth of node G = 2
Depth of node H = 2
Depth of node I = 3
Depth of node J = 3
Depth of node K = 3
12. Subtree-
In a tree, each child from a node forms a subtree recursively.
Every child node forms a subtree on its parent node.
Example-
13. Forest-
Example-
Expression Tree
Expression trees are used to evaluate the simple arithmetic expressions. Expression
tree is basically a binary tree where internal nodes are represented by operators
while the leaf nodes are represented by operands. Expression trees are widely used
to solve algebraic expressions like (a+b)*(a-b). Consider the following example.
(a + b) / (a*b - c) + d
Binary Tree-
Binary tree is a special tree data structure in which each node can have at most
2 children.
Example-
A binary tree is unlabeled if its nodes are not assigned any label.
Example-
Consider we want to draw all the binary trees possible with 3 unlabeled nodes.
= 2 x 3C3 / (3 + 1)
= 6C3 / 4
=5
Thus,
With 3 unlabeled nodes, 5 unlabeled binary trees are possible.
These unlabeled binary trees are as follows-
Consider we want to draw all the binary trees possible with 3 labeled nodes.
= { 2 x 3C3 / (3 + 1) } x 3!
= { 6C3 / 4 } x 6
=5x6
= 30
Thus,
A rooted binary tree is a binary tree that satisfies the following 2 properties-
Example-
Here,
A complete binary tree is a binary tree that satisfies the following 2 properties-
Here,
An almost complete binary tree is a binary tree that satisfies the following 2
properties-
All the levels are completely filled except possibly the last level.
The last level must be strictly filled from left to right.
Example-
Here,
A skewed binary tree is a binary tree that satisfies the following 2 properties-
All the nodes except one node has one and only one child.
The remaining node has no child.
OR
A skewed binary tree is a binary tree of n nodes such that its depth is (n-1).
Example-
Property-01:
=H+1
Example-
To construct a binary tree of height = 4, we need at least 4 + 1 = 5 nodes.
Property-02:
= 2H+1 – 1
Example-
= 23+1 – 1
= 16 – 1
= 15 nodes
Property-03:
Example-
Consider the following binary tree-
Here,
Clearly, number of leaf nodes is one greater than number of nodes with 2 children.
NOTE
Number of leaf nodes in any binary tree depends only on the number of nodes
with 2 children.
Property-04:
= 2L
Example-
= 22
=4
Thus, in a binary tree, maximum number of nodes that can be present at level-2 =
4.
Problem-01:
A binary tree T has n leaf nodes. The number of nodes of degree-2 in T is ______?
A. log2n
B. n-1
C. n
D. 2n
Solution-
=n–1
Problem-02:
In a binary tree, for every node the difference between the number of nodes in the
left and right subtrees is at most 2. If the height of the tree is h > 0, then the
minimum number of nodes in the tree is ______?
A. 2h-1
B. 2h-1 + 1
C. 2h – 1
D. 2h
Solution-
A. 4
B. 5
C. 7
D. 8
Problem-03:
In a binary tree, the number of internal nodes of degree-1 is 5 and the number of
internal nodes of degree-2 is 10. The number of leaf nodes in the binary tree is
______?
A. 10
B. 11
C. 12
D. 15
Solution-
= 10 + 1
= 11
Problem-04:
The height of a binary tree is the maximum number of edges in any root to leaf
path. The maximum number of nodes in a binary tree of height h is ______?
A. 2h
B. 2h-1 – 1
C. 2h+1 – 1
D. 2h+1
Solution-
Problem-05:
Solution-
Tree Traversal-
Tree Traversal refers to the process of visiting each node in a tree data
structure exactly once.
Various tree traversal techniques are-
1. Preorder Traversal
2. Inorder Traversal
3. Postorder Traversal
1. Preorder Traversal-
Algorithm-
1. Visit the root
2. Traverse the left sub tree i.e. call Preorder (left sub tree)
3. Traverse the right sub tree i.e. call Preorder (right sub tree)
Example-
Traverse the entire tree starting from the root node keeping yourself to the left.
Applications-
2. Inorder Traversal-
Algorithm-
1. Traverse the left sub tree i.e. call Inorder (left sub tree)
2. Visit the root
3. Traverse the right sub tree i.e. call Inorder (right sub tree)
Left → Root → Right
Example-
Keep a plane mirror horizontally at the bottom of the tree and take the
projection of all the nodes.
Application-
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
Example-
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.