Tree PDF
Tree PDF
• In a tree with n number of nodes, there are exactly (n-1) • In other words, the node which has one or more children
number of edges. is called as a parent node.
• In a tree, a parent node can have any number of child
nodes.
Child Node
• The node which is a descendant of some node is called as a child node.
• All the nodes except root node are child nodes.
Sibling Degree
• Nodes which belong to the same parent are called as • Degree of a node is the total number of children of that
siblings. node.
• In other words, nodes with the same parent are sibling • Degree of a tree is the highest degree of a node among
nodes. all the nodes in the tree.
Internal Node Leaf Node
• The node which has at least one child is called as an • The node which does not have any child is called as a leaf
internal node. node.
• Internal nodes are also called as non-terminal nodes. • Leaf nodes are also called as external nodes or terminal
nodes.
• Every non-leaf node is an internal node.
Level
• In a tree, each step from top to bottom is called as level of a tree.
• The level count starts with 0 and increments by 1 at each level or step.
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 node A = 3
• Height of all leaf nodes = 0 • 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
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 • Depth of node A = 0
• Depth of node B = 1
• The terms “level” and “depth” are used interchangeably. • 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
Subtree
• In a tree, each child from a node forms a subtree recursively.
• Every child node forms a subtree on its parent node.
Forest
• A forest is a set of disjoint trees
Binary Tree
• 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.
Unlabelled Binary Tree
• A binary tree is unlabeled if its nodes are not assigned any label.
• Consider we want to draw all the binary trees possible with 3 unlabeled nodes.
• Using the above formula, we have number of binary trees possible with 3
unlabeled nodes
= 2𝑥3𝐶3 / (3 + 1) = 6𝐶3 / 4 = 5
Unlabelled Binary Tree
Thus,
• With 3 unlabeled nodes, 5 unlabeled binary trees are possible.
• These unlabeled binary trees are as follows-
Labelled Binary Tree
• A binary tree is labeled if all its nodes are assigned a label.
• Consider we want to draw all the binary trees possible with 3 labeled nodes.
• Using the above formula, we have number of binary trees possible with 3
labeled nodes
= { 2𝑥3𝐶3 / (3 + 1) } 𝑥 3! = { 6𝐶3 / 4 } 𝑥 6 = 5 𝑥 6 = 30
Labelled Binary Tree
Thus,
• With 3 labeled nodes, 30
labeled binary trees are
possible.
• Each unlabeled structure gives
rise to 3! = 6 different labeled
structures.
Types of Binary Tree
Rooted Binary Tree
• A rooted binary tree is a binary tree that satisfies the following 2 properties-
• It has a root node.
• Each node has at most 2 children.
Full/Strictly Binary Tree
• A binary tree in which every node has either 0 or 2 children is called as a Full
binary tree.
• Full binary tree is also called as Strictly binary tree.
Complete/Perfect Binary Tree
• A complete binary tree is a binary tree that satisfies the following 2 properties-
• Every internal node has exactly 2 children.
• All the leaf nodes are at the same level.
• Complete binary tree is also called as Perfect binary tree.
Almost Complete Binary Tree
• 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.
Skewed Binary Tree
• 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).
Binary Tree Properties
Property-1
• Minimum number of nodes in a binary tree of height H is H + 1
• To construct a binary tree of height = 4, we need at least 4 + 1 = 5 nodes.
Binary Tree Properties
Property-2
+𝟏
• Maximum number of nodes in a binary tree of height 𝑯 𝒊𝒔 𝟐𝑯 –𝟏
• Maximum number of nodes in a binary tree of height 3
3 +1
= 2 – 1 = 16 – 1 = 15 𝑛𝑜𝑑𝑒𝑠
Binary Tree Properties
Property-3
• Total Number of leaf nodes in
a Binary Tree is Total Number
of nodes with 2 children + 1
• In following tree 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.
Binary Tree Properties
Property-4
• Maximum number of nodes at any level ‘L’ in a binary tree is 2L
• Maximum number of nodes at level-2 in a binary tree = 22 = 4
N-ary Tree
• In the previous part, we focused more on binary tree.
• A binary tree is a rooted tree in which each node has no more than 2 children.
• Let's extend this definition to N-ary tree.
• If a tree is a rooted tree in which each node has no more than N children, it is
called N-ary tree.
• Trie is one of the most frequently used N-ary trees.
• Also, a binary tree is a special form of a N-ary tree.
Tree Traversal
• Tree Traversal refers to the process of visiting each node in a tree data structure
exactly once.
Depth First Traversal
• Following three traversal techniques fall under Depth First Traversal-
1. Pre-order Traversal
2. In-order Traversal
3. Post-order Traversal
Pre-order Traversal
• Algorithm-
1. Visit the root
2. Traverse the left sub tree i.e.
call Pre-order (left sub tree)
3. Traverse the right sub tree
i.e. call Pre-order (right sub
tree)
Root → Left → Right
• Applications-
1. Pre-order traversal is used to get prefix expression of an expression tree.
2. Pre-order traversal is used to create a copy of the tree.
In-order Traversal
• Algorithm-
1. Traverse the left sub tree i.e.
call In-order (left sub tree)
2. Visit the root
3. Traverse the right sub tree
i.e. call In-order (right sub
tree)
Left → Root → Right
• Applications-
1. In-order traversal is used to get infix expression of an expression tree.
Post-order Traversal
• Algorithm-
1. Traverse the left sub tree i.e.
call Post-order (left sub tree)
2. Traverse the right sub tree
i.e. call Post-order (right sub
tree)
3. Visit the root
Left → Right → Root
• Applications-
1. Post-order traversal is used to get postfix expression of an expression tree.
2. Post-order traversal is used to delete the tree. This is because it deletes the children first and then it deletes
the parent.
Breadth First Traversal
• 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.
• Level order traversal is used to print the data in the same order as stored in the
array representation of a complete binary tree.
Exercise To Solve
• If the binary tree in figure is traversed in in-order, then the order in which the
nodes will be visited is ____?