DS Lecture Week 10
DS Lecture Week 10
𝑽𝟎 Root Node
𝑽𝟏 𝑽𝟕
𝑽𝟐 𝑽𝟑 𝑽𝟒 𝑽𝟖 𝑽𝟗
Terminal or
𝑽𝟓 𝑽𝟔 𝑽𝟏𝟎
Leaf Node
2
Tree– Concepts & Definitions
𝑽𝟓 𝑽𝟔 𝑽𝟏𝟎 𝑽𝟎
𝑽𝟎
𝑽𝟐 𝑽𝟑 𝑽𝟒 𝑽𝟖 𝑽𝟗 𝑽𝟏 𝑽𝟕
𝑽𝟕 𝑽𝟏
𝑽𝟐 𝑽𝟑 𝑽𝟒 𝑽𝟖 𝑽𝟗 𝑽𝟖 𝑽𝟗 𝑽𝟐 𝑽𝟑 𝑽𝟒
𝑽𝟏 𝑽𝟕
𝑽𝟓 𝑽𝟔 𝑽𝟏𝟎 𝑽𝟏𝟎 𝑽𝟓 𝑽𝟔
𝑽𝟎
3
Tree– Concepts & Definitions
Terminal Node (Leaf Node)
In a directed tree, any node which has out degree 0 is called terminal node or leaf node.
Level of Node
The level of any node is the length of its path from the root.
Ordered Tree
In a directed tree an ordering of the nodes at each level is prescribed then such a tree is called ordered tree.
The diagrams (b) and (c) represents same directed tree but different ordered tree.
Forest
If we delete the root and its edges connecting the nodes at level 1, we obtain a set of disjoint tree. A set of
disjoint tree is a forest.
4
Representation of Directed Tree
Other way to represent directed tree are
𝑽𝟎
Venn Diagram
Nesting of Parenthesis
𝑽𝟏 𝑽𝟕
Like table content of Book
Level Format
𝑽𝟐 𝑽𝟑 𝑽𝟒 𝑽𝟖 𝑽𝟗
𝑽𝟓 𝑽𝟔 𝑽𝟏𝟎
5
Venn Diagram
𝑽𝟎
V0
𝑽𝟏 𝑽𝟕
V1 V7
V2 V9
𝑽𝟐 𝑽𝟑 𝑽𝟒 𝑽𝟖 𝑽𝟗 V3
V5 V10
𝑽𝟓 𝑽𝟔 𝑽𝟏𝟎 V4
V6
V8
6
Nesting of Parenthesis
Like a table Content of Book
𝑽𝟎 V0
V1
𝑽𝟏 𝑽𝟕 V2
V5
V6
𝑽𝟐 𝑽𝟑 𝑽𝟒 𝑽𝟖 𝑽𝟗 V3
V4
V7
𝑽𝟓 𝑽𝟔 𝑽𝟏𝟎
V8
V9
V10
(V0 (V1 (V2 (V5) (V6) ) (V3) (V4) ) (V7 (V8) (V9 (V10) ) ) )
Nesting of Parenthesis
7
Level Format
𝑽𝟎
1 V0
2 V1
𝑽𝟏 𝑽𝟕 3 V2
4 V5
4 V6
𝑽𝟐 𝑽𝟑 𝑽𝟒 𝑽𝟖 𝑽𝟗
3 V3
3 V4
𝑽𝟓 𝑽𝟔 𝑽𝟏𝟎
2 V7
3 V8
3 V9
4 V10
8
Tree– Concepts & Definitions
The node that is reachable from a node is called descendant of a node.
The nodes which are reachable from a node through a single edge are called the children of
node.
M-ary Tree
If in a directed tree the out degree of every node is less than or equal to m then tree is called an m-ary tree.
9
Tree– Concepts & Definitions
Height of the tree
The height of a tree is the length of the path from the root to the deepest node in the tree.
Binary Tree
If in a directed tree the out degree of every node is less than or equal to 2 then tree is called binary tree.
10
Tree– Concepts & Definitions
Sibling
Siblings are nodes that share the same parent node
0 1 0 1 0 1
00 01 11 00 01 11 11 00 01 10
11
Tree Traversal
The most common operations performed on tree structure is that of traversal.
This is a procedure by which each node in the tree is processed exactly once in a systematic
manner.
There are three ways of traversing a binary tree. A
1. Preorder Traversal
2. Inorder Traversal B D
3. Postorder Traversal
C E G
12
Preorder Traversal
Preorder traversal of a binary tree is defined as follow
1. Process the root node A ✓
A B C D E F G
13
Inorder Traversal
Inorder traversal of a binary tree is defined as follow
A ✓
1. Traverse the left subtree in Inorder
2. Process the root node
B ✓ D ✓
3. Traverse the right subtree in Inorder
C ✓ E ✓ G ✓
F ✓
C B A E F D G
14
Postorder Traversal
Postorder traversal of a binary tree is defined as follow
A ✓
1. Traverse the left subtree in Postorder
2. Traverse the right subtree in Postorder
B ✓ D ✓
3. Process the root node
C ✓ E ✓ G ✓
F ✓
15
Write Pre/In/Post Order Traversal
1 50 15
2 3 25 75
3 1
6 22
4 22 40 60 80
5 45
5
15 30 90
23 65
34 78
16
Linked Representation of Binary Tree
T
LPTR DATA RPTR
A
B D
B D
C E G
C E G
F
F
17
Algorithm of Binary Tree Traversal
Preorder Traversal - Procedure: RPREORDER(T)
Inorder Traversal - Procedure: RINORDER(T)
Postorder Traversal - Procedure: RPOSTORDER(T)
18
Procedure: RPREORDER(T)
This procedure traverses the tree in preorder, in a recursive manner.
T is root node address of given binary tree LPTR DATA RPTR
Node structure of binary tree is described as below Typical node of Binary Tree
19
Procedure: RINORDER(T)
This procedure traverses the tree in InOrder, in a recursive manner.
T is root node address of given binary tree. LPTR DATA RPTR
Node structure of binary tree is described as below. Typical node of Binary Tree
20
Procedure: RPOSTORDER(T)
This procedure traverses the tree in PostOrder, in a recursive manner.
T is root node address of given binary tree. LPTR DATA RPTR
Node structure of binary tree is described as below. Typical node of Binary Tree
21
Construct Binary Tree from Traversal
Construct a Binary tree from the given Inorder and Postorder traversals
• Step 1: Find the root node
• Preoder Traversal – first node is root node
Inorder : D G B A H E I C F • Postoder Traversal last node is root node
Postorder : G D B H I E F C A • Step 2: Find Left & Right Sub Tree
• Inorder traversal gives Left and right sub tree
Postorder : G D B H I E F C A
A A
Inorder :DGBAHEICF
B C B C
A
D,G H,E,I F D E F
D,G,B H,E,I,C,F
G H I
22
Construct Binary Tree from Traversal
Preorder : G B Q A C K F P D E R H Inorder : Q B K C F A G P E D H R
G G G
QBKCFA PED HR B P B P
D Q A D
Q A
G
C E R
B KCF E HR
P
K F H
Q KCFA ED HR
23
Linked Representation of Binary Tree
T
LPTR DATA RPTR
A
B D
B D
C E G
C E G
F
F
24