0% found this document useful (0 votes)
2 views24 pages

DS Lecture Week 10

The document provides an overview of tree data structures, including definitions of directed trees, terminal nodes, and various types of trees such as binary and m-ary trees. It explains tree traversal methods (preorder, inorder, postorder) and includes algorithms for these traversals. Additionally, it discusses representations of trees and methods for constructing binary trees from traversal data.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views24 pages

DS Lecture Week 10

The document provides an overview of tree data structures, including definitions of directed trees, terminal nodes, and various types of trees such as binary and m-ary trees. It explains tree traversal methods (preorder, inorder, postorder) and includes algorithms for these traversals. Additionally, it discusses representations of trees and methods for constructing binary trees from traversal data.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 24

Module-3

Non-Linear Data Structure


Tree

Dr. Ram Kishan Dewangan


Associate Professor, SCSET
Bennett University, Gr. Noida
Tree– Concepts & Definitions
 Directed Tree
 A directed tree is an acyclic digraph which has one node called its root with in degree 0, while all other nodes
have in degree 1.
 Every directed tree must have at least one node.
 An isolated node is also a directed tree.

𝑽𝟎 Root Node

𝑽𝟏 𝑽𝟕

𝑽𝟐 𝑽𝟑 𝑽𝟒 𝑽𝟖 𝑽𝟗

Terminal or
𝑽𝟓 𝑽𝟔 𝑽𝟏𝟎
Leaf Node
2
Tree– Concepts & Definitions

𝑽𝟓 𝑽𝟔 𝑽𝟏𝟎 𝑽𝟎
𝑽𝟎

𝑽𝟐 𝑽𝟑 𝑽𝟒 𝑽𝟖 𝑽𝟗 𝑽𝟏 𝑽𝟕
𝑽𝟕 𝑽𝟏

𝑽𝟐 𝑽𝟑 𝑽𝟒 𝑽𝟖 𝑽𝟗 𝑽𝟖 𝑽𝟗 𝑽𝟐 𝑽𝟑 𝑽𝟒
𝑽𝟏 𝑽𝟕

𝑽𝟓 𝑽𝟔 𝑽𝟏𝟎 𝑽𝟏𝟎 𝑽𝟓 𝑽𝟔
𝑽𝟎

(a) (b) (c)

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.

 Full or Complete M-ary Tree


 If the out degree of each and every node is exactly equal to m or 0 and their number of nodes at level i is
m(i-1) then the tree is called a full or complete m-ary tree.

 Positional M-ary Tree


 If we consider m-ary trees in which the m children of any node are assumed to have m distinct positions, if
such positions are taken into account, then tree is called positional 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.

 Strictly Binary Tree


 A strictly binary tree (sometimes proper binary tree or 2-tree or full binary tree) is a tree in which every node
other than the leaves has two children.

 Complete Binary Tree


 If the out degree of each and every node is exactly equal to 2 or 0 and their number of nodes at level i is
2(i-1) then the tree is called a full or complete binary tree.

10
Tree– Concepts & Definitions
 Sibling
 Siblings are nodes that share the same parent node

 Positional m-ary Tree


 If we consider m-ary trees in which the m children of any node are assumed to have m distinct positions, if
such positions are taken into account, then tree is called positional m-ary tree

0 1 0 1 0 1

00 01 11 00 01 11 11 00 01 10

(a) Binary tree (b) Complete binary tree (c)

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 ✓

2. Traverse the left subtree in preorder


3. Traverse the right subtree in preorder B ✓ D ✓

 If particular subtree is empty (i.e., node has no left or


C ✓ E ✓ G ✓
right descendant) the traversal is performed by doing
nothing.
F ✓
 In other words, a null subtree is considered to be fully
traversed when it is encountered. Preorder traversal of a given tree as

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 ✓

Inorder traversal of a given tree as

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 ✓

Postorder traversal of a given tree as


C B F E G D A

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

Typical node of Binary Tree A

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

1. [Check for Empty Tree] 3. [Process the Right Sub Tree]


IF T = NULL IF RPTR (T) ≠ NULL
THEN write (‘Empty Tree’) THEN RPREORDER (RPTR (T))
return 4. [Finished]
ELSE write (DATA(T)) Return
2. [Process the Left Sub Tree]
IF LPTR (T) ≠ NULL
THEN RPREORDER (LPTR (T))

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

1. [Check for Empty Tree] 4. [Process the Right Sub Tree]


IF T = NULL IF RPTR (T) ≠ NULL
THEN write (‘Empty Tree’) THEN RINORDER (RPTR (T))
return 5. [Finished]
2. [Process the Left Sub Tree] Return
IF LPTR (T) ≠ NULL
THEN RINORDER (LPTR (T))
3. [Process the Root Node]
write (DATA(T))

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

1. [Check for Empty Tree] 4. [Process the Root Node]


IF T = NULL write (DATA(T))
THEN write (‘Empty Tree’) 5. [Finished]
return Return
2. [Process the Left Sub Tree]
IF LPTR (T) ≠ NULL
THEN RPOSTORDER (LPTR (T))
3. [Process the Right Sub Tree]
IF RPTR (T) ≠ NULL
THEN RPOSTORDER (RPTR (T))

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

Typical node of Binary Tree A

A
B D

B D

C E G
C E G

F
F

24

You might also like