DS L-5
DS L-5
LECTURE-5
TREE
➢ Number of edges: An edge can be defined as the connection between two nodes. If a tree has N nodes then it
will have (N-1) edges. There is only one path from each node to any other node of the tree.
➢ Depth of a node: The depth of a node is defined as the length of the path from the root to that node. Each edge
adds 1 unit of length to the path. So, it can also be defined as the number of edges in the path from the root of
the tree to the node.
➢ Height of a node: The height of a node can be defined as the length of the longest path from the node to a leaf
node of the tree.
➢ Height of the Tree: The height of a tree is the length of the longest path from the root of the tree to a leaf node
of the tree.
➢ Degree of a Node: The total count of subtrees attached to that node is called the degree of the node. The degree
of a leaf node must be 0. The degree of a tree is the maximum degree of a node among all the nodes in the tree.
•Degree of node A = 2
•Degree of node B = 3
•Degree of node C =
•Degree of node D =
•Degree of node E =
•Degree of node F =
•Degree of node G =
•Degree of node H =
•Degree of node I =
•Degree of node J =
•Degree of node K =
Height
Height of node A =
Height of node B = 2
Height of node C =
Height of node D =
Height of node E =
Height of node F =
Height of node G =
Height of node H = 0
Height of node I =
Height of node J =
Height of node K =
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.
•Depth of node A =
•Depth of node B =
•Depth of node C =
•Depth of node D =
•Depth of node E =
•Depth of node F =
•Depth of node G =
•Depth of node H = 2
•Depth of node I =
•Depth of node J =
•Depth of node K =
Binary Trees
Binary tree is a tree data structure(non-linear) in which
each node can have at most two children which are
referred to as the left child and the right child Root
Leaf
Properties of Binary Tree
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𝑥3𝐶 3 / (3 + 1)
= 6C3 / 4
=5
Labeled Binary Tree
Consider, we want to draw all the binary trees possible with 3 labeled nodes.
Using the above formula, we have-
Similarly,
•Every other unlabeled structure gives rise to 5 different
labeled structures.
•Thus, in total 30 different labeled binary trees are possible.
Binary Tree Properties
Example-
Here,
Tree Traversal refers to the process of visiting or accessing each node of the tree exactly once in a
certain order. Tree traversal algorithms help us to visit and process all the nodes of the tree.
Inorder Traversal
Inorder traversal visits the node in the order: Left -> Root -> Right
Algorithm:
Inorder(tree)
Algorithm
Preorder(tree)
•Visit the root.
•Traverse the left subtree, i.e., call Preorder(left->subtree)
•Traverse the right subtree, i.e., call Preorder(right->subtree)
Postorder Traversal
Postorder traversal visits the node in the order: Left -> Right -> Root
Algorithm
Postorder(tree)
•Traverse the left subtree, i.e., call Postorder(left->subtree)
•Traverse the right subtree, i.e., call Postorder(right->subtree)
•Visit the root
Breadth First Traversal
➢ Breadth First Traversal of a tree prints all the nodes of a tree level by level.
➢ Level Order Traversal visits all nodes present in the same level completely before
LevelOrder(tree)
•Create an empty queue Q
•Enqueue the root node of the tree to Q
•Loop while Q is not empty
• Dequeue a node from Q and visit it
• Enqueue the left child of the dequeued node if it exists
• Enqueue the right child of the dequeued node if it exists
Example:
If the binary tree in figure is traversed in inorder, then the order in which the nodes will be visited is ____?
Preorder:
Postorder: