Professional Documents
Culture Documents
By
Ravi Kant Sahu
Asst. Professor,
Lovely Professional University, Punjab
Contents
• Introduction
• Binary Tree
• Basic Terminology
• Complete Binary Tree
• Extended Binary Tree
• Traversing Binary Tree
•Pre-order
• In-order
• Post-order
Ravi Kant Sahu, Asst. Professor @ LPU Phagwara (Punjab) India
Introduction
• Trees are non-linear data structures.
OR
E C
D
K F G
H
L
M I
J
Samples of Trees
A A Complete Binary Tree
A
B B
Skewed Binary Tree
C B C
D
D E F G
E
H I
Maximum Number of Nodes in
BT
The maximum number of nodes on level i of a
binary tree is 2i, i>=0.
B C
B C
D F
D F
G
Fig: Binary Tree T Fig: Extended 2-tree
Memory Representation of Tree
data
left right
• If any subtree is empty then corresponding pointer will
contain the NULL value.
A 6 4
E 0 0
10 C 0 9
AVAIL
8
B 7 3
D 0 0
F 0 0
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
Sequential Representation of Binary Tree
[16] E
H I
Traversing Binary Trees
• There are three standard ways of traversing a binary tree.
• Preorder
1. Process the Root R.
2. Traverse the left subtreeof R in Preorder.
3. Traverse the right subtreeof R in Preorder.
• Postorder
1. Traverse the left subtreeof R in Postorder.
2. Traverse the right subtreeof R in Postorder.
3. Process the Root R.
• Inorder
1. Traverse the left subtreeof R in Inorder.
3. Process the Root R.
2. Traverse the right subtreeof R in Inorder.
Ravi Kant Sahu, Asst. Professor @ LPU Phagwara (Punjab) India
Binary Tree Traversal
Preorder Traversal of Binary Tree
PREORDER (INFO, LEFT, RIGHT, ROOT )
1.IF: ROOT == NULL
THEN: Return
PreOrder:
100,50,75,74,73,76,150,125,124,130,131
PostOrder: 73,74,76,75,50,124,131,130,125,150,100
Create the binary tree
INORDER: M,C,B,D,K,A,T,V,W,U,S
PREORDER: A,B,C,M,D,K,S,T,U,V,W
POSTORDER:M C K D B W V U T S A
PREORDER:
25,15,10,4,12,22,18,24,50,35,31,44,70,66,90
Postorder:
4,12,10,18,24,22,15,31,44,35,66,90,70,50,25
inorder traversal
* D A/B*C*D+E
infix expression
/ C
postorder traversal
AB/C*D*E+
A B postfix expression
Review Questions
• Given Inorder and Preorder Traversal of a binary tree:
Preorder: L, G, D, C, M, H, T, K
Inorder: G, D, L, C, H, T, M, K
Construct the binary tree and find out the Postorder traversal of the
Tree.