SlideShare a Scribd company logo
1 of 23
Binary trees: introduction (complete and
extended binary trees),
Memory representation (sequential, linked)
Binary tree traversal: pre-order, in-order and
post-order (traversal algorithms using stacks)
A tree is a finite set of zero or more nodes
such that:
There is a specially designated node called
the root.
The remaining nodes are partitioned into
n>=0 disjoint sets T1, ..., Tn, where each of
these sets is a tree.
We call T1, ..., Tn the subtrees of the root.
Definition of Tree
Level and Depth
K L
E F
B
G
C
M
H I J
D
A
Level
0
1
2
3
node (13)
degree of a node(shown by green
number)
leaf (terminal)
nonterminal
parent
children
sibling
ancestor
descendant
level of a node
height(depth) of a tree (4)
3
2 1 3
2 0 0 1 0 0
0 0 0
Terminology
The degree of a node is the number of subtrees
of the node
The degree of A is 3; the degree of C is 1.
The node with degree 0 is a leaf or terminal
node.
A node that has subtrees is the parent of the
roots of the subtrees.
The roots of these subtrees are the children of
the node.
Children of the same parent are siblings.
The ancestors of a node are all the nodes
along the path from the root to the node.
Tree Properties
A
B C
D
G
E F
I
H
Property Value
Number of nodes
Height
Root Node
Leaves
Interior nodes
Number of levels
Ancestors of H
Descendants of B
Siblings of E
degree of node A
Binary Trees
A special class of trees: max degree for each
node is 2
Recursive definition: A binary tree is a finite
set of nodes that is either empty or consists of
a root and two disjoint binary trees called the
left subtree and the right subtree.
Example
J
I
M
H
L
A
B
C
D
E
F G
K
Samples of Trees
A
B
A
B
A
B C
G
E
I
D
H
F
Complete Binary Tree
Skewed Binary Tree
E
C
D
Maximum Number of Nodes in BT
The maximum number of nodes on level i of a
binary tree is 2i, i>=0.
The maximum number of nodes in a binary
tree
of depth k is 2k-1, k>=1.
Full BT vs. Complete BT
A full binary tree of depth k is a binary tree of depth k
having 2k-1 nodes, k>=1.
A binary tree with n nodes and depth k is
complete iff its nodes correspond to the nodes numbered
from 1 to n in the full binary tree of depth k.
A
B C
G
E
I
D
H
F
A
B C
G
E
K
D
J
F
I
H O
N
M
L
Full binary tree of depth 4
Complete binary tree
Complete Binary Tree
If a complete binary tree with n nodes
(depth=└log n + 1┘)
is represented sequentially,
then for any node with index i, 1<=i<=n, we have:
parent(i) is at └i/2┘ if i!=1. If i=1, i is at the root and
has no parent.
leftChild(i) is at 2i if 2i<=n. If 2i>n, then i has no
left child.
rightChild(i) is at 2i+1 if 2i+1<=n. If 2i+1>n,
then i has no right child.
Extended Binary Tree
 A binary tree T is said to be a 2-tree or an extended binary tree if each
node N has either 0 or 2 children.
 The nodes with 2 children are called internal nodes.
 The nodes with o children are called external nodes. A
B C
G
D F
A
B C
G
D F
Fig: Binary Tree T Fig: Extended 2-tree
Sequential Representation
A
B
--
C
--
--
--
D
--
.
E
[1]
[2]
[3]
[4]
[5]
[6]
[7]
[8]
[9]
.
[16]
[1]
[2]
[3]
[4]
[5]
[6]
[7]
[8]
[9]
A
B
C
D
E
F
G
H
I
A
B
E
C
D
A
B C
G
E
I
D
H
F
(1) waste space
(2) insertion/deletion
problem
Linked Representation
struct btnode {
int data;
btnode *left, *right;
};
data
left right
data
left right
Binary Tree Traversals
There are three standard ways of traversing a
binary tree T with root R.
These three algorithms, called:
preorder
inorder
postorder
Arithmetic Expression Using BT
+
*
A
*
/
E
D
C
B
preorder traversal
+ * * / A B C D E
prefix expression
inorder traversal
A / B * C * D + E
infix expression
postorder traversal
A B / C * D * E +
postfix expression
Preorder Traversal (recursive version)
Algorithm:
1. Process the root R.
2. Traverse the left subtree of R in preorder.
3. Traverse the right subtree of R in preorder.
Pseudo-Code:
void preorder(btnode ptr)
/* preorder tree traversal */
{
if (ptr!=NULL) {
cout<<ptr->data;
preorder(ptr->left);
predorder(ptr->right);
}
}
+ * * / A B C D E
Inorder Traversal (recursive version)
Algorithm:
1. Traverse the left subtree of R in inorder.
2. Process the root R.
3. Traverse the right subtree of R in inorder.
Pseudo-Code:
void inorder(btnode ptr)
/* inorder tree traversal */
{
if (ptr!=NULL) {
inorder(ptr->left);
cout<<ptr->data;
indorder(ptr->right);
}
}
A / B * C * D + E
Postorder Traversal (recursive version)
Algorithm:
1. Traverse the left subtree of R in postorder.
2. Traverse the right subtree of R in postorder.
3. Process the root R.
Pseudo-Code:
void postorder(btnode ptr)
/* postorder tree traversal */
{
if (ptr!=NULL) {
postorder(ptr->left);
postorder(ptr->right);
cout<<ptr->data;
}
}
A B / C * D * E +
Preorder Traversal (using stack)
PREORD(INFO, LEFT, RIGHT, ROOT)
1. Set TOP:= 1, STACK[1]:= NULL and PTR := ROOT.
2. Repeat Steps 3 to 5 while PTR != NULL:
3. Apply PROCESS to INFO[PTR].
4. If RIGHT[PTR]!= NULL then
Set TOP := TOP+1, and STACK[TOP]:= RIGHT[PTR].
5. If LEFT[PTR]!= NULL then
Set PTR:= LEFT[PTR].
Else
Set PTR:= STACK[TOP] TOP:= TOP-1.
[End of If structure]
[End of step 2 Loop]
6. Exit.
+ * * / A B C D E
Inorder Traversal (using stack)
INORD(INFO, LEFT, RIGHT, ROOT)
1. Set TOP:= 1, STACK[1]:= NULL and PTR := ROOT.
2. Repeat while PTR != NULL:
a) Set TOP:=TOP+1, STACK[TOP]:=PTR.
b) Set PTR:= LEFT[PTR].
[End of loop]
3. Set PTR:=STACK[TOP], TOP:=TOP-1.
4. Repeat Steps 5 to 7 while PTR != NULL:
5. Apply PROCESS to INFO[PTR].
6. If RIGHT[PTR]!= NULL, then:
a) Set PTR:=RIGHT[PTR]
b) Go to Step 2.
[End of If structure]
7. Set PTR:=STACK[TOP] and TOP:=TOP-1.
[End of step 4 Loop.]
8. Exit
A / B * C * D + E
Postorder Traversal (using stack)
POSTORD(INFO, LEFT, RIGHT, ROOT)
1. Set TOP:= 1, STACK[1]:= NULL and PTR := ROOT.
2. Repeat Step 3 to 5 while PTR!=NULL:
3. Set TOP := TOP+1, STACK[Top]:=PTR.
4. If RIGHT[PTR]!=NULL, then:
Set TOP := TOP+1, Stack[Top]:=-RIGHT[PTR].
[End of If structure]
5. Set PTR:=LEFT[PTR]
[End of step 3 Loop]
6. Set PTR:=STACK[Top], TOP := TOP-1.
7. Repeat while PTR>0:
a)Apply PROCESS to INFO[PTR].
b)PTR:=STACK[Top], TOP := TOP-1.
[End of Loop]
8. Repeat while PTR<0:
a) Set PTR:= -PTR.
b) Go to Step 2.
[End of If structure]
9. Exit.
A B / C * D * E +
Thank You

More Related Content

Similar to Binary Tree Traversal and Representation

Similar to Binary Tree Traversal and Representation (20)

Lecture notes data structures tree
Lecture notes data structures   treeLecture notes data structures   tree
Lecture notes data structures tree
 
Unit 4 tree
Unit 4   treeUnit 4   tree
Unit 4 tree
 
Chap 5 Tree.ppt
Chap 5 Tree.pptChap 5 Tree.ppt
Chap 5 Tree.ppt
 
Trees in Data Structure
Trees in Data StructureTrees in Data Structure
Trees in Data Structure
 
Review session2
Review session2Review session2
Review session2
 
Unit 3.ppt
Unit 3.pptUnit 3.ppt
Unit 3.ppt
 
Tree distance algorithm
Tree distance algorithmTree distance algorithm
Tree distance algorithm
 
Tree terminology and introduction to binary tree
Tree terminology and introduction to binary treeTree terminology and introduction to binary tree
Tree terminology and introduction to binary tree
 
Chapter 09-Trees
Chapter 09-TreesChapter 09-Trees
Chapter 09-Trees
 
7 chapter4 trees_binary
7 chapter4 trees_binary7 chapter4 trees_binary
7 chapter4 trees_binary
 
Trees unit 3
Trees unit 3Trees unit 3
Trees unit 3
 
Unit 6 tree
Unit   6 treeUnit   6 tree
Unit 6 tree
 
Unit 4.1 (tree)
Unit 4.1 (tree)Unit 4.1 (tree)
Unit 4.1 (tree)
 
CS-102 BST_27_3_14v2.pdf
CS-102 BST_27_3_14v2.pdfCS-102 BST_27_3_14v2.pdf
CS-102 BST_27_3_14v2.pdf
 
Types of Tree in Data Structure in C++
Types of Tree in Data Structure in C++Types of Tree in Data Structure in C++
Types of Tree in Data Structure in C++
 
data_structures_and_applications_-_module-4.ppt
data_structures_and_applications_-_module-4.pptdata_structures_and_applications_-_module-4.ppt
data_structures_and_applications_-_module-4.ppt
 
DS MCQS.pptx
DS MCQS.pptxDS MCQS.pptx
DS MCQS.pptx
 
Python lecture 07
Python lecture 07Python lecture 07
Python lecture 07
 
binary tree.pptx
binary tree.pptxbinary tree.pptx
binary tree.pptx
 
b-tree.ppt
b-tree.pptb-tree.ppt
b-tree.ppt
 

Recently uploaded

Historical philosophical, theoretical, and legal foundations of special and i...
Historical philosophical, theoretical, and legal foundations of special and i...Historical philosophical, theoretical, and legal foundations of special and i...
Historical philosophical, theoretical, and legal foundations of special and i...jaredbarbolino94
 
Pharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdfPharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdfMahmoud M. Sallam
 
Meghan Sutherland In Media Res Media Component
Meghan Sutherland In Media Res Media ComponentMeghan Sutherland In Media Res Media Component
Meghan Sutherland In Media Res Media ComponentInMediaRes1
 
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfEnzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfSumit Tiwari
 
CELL CYCLE Division Science 8 quarter IV.pptx
CELL CYCLE Division Science 8 quarter IV.pptxCELL CYCLE Division Science 8 quarter IV.pptx
CELL CYCLE Division Science 8 quarter IV.pptxJiesonDelaCerna
 
Hierarchy of management that covers different levels of management
Hierarchy of management that covers different levels of managementHierarchy of management that covers different levels of management
Hierarchy of management that covers different levels of managementmkooblal
 
Crayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon ACrayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon AUnboundStockton
 
Painted Grey Ware.pptx, PGW Culture of India
Painted Grey Ware.pptx, PGW Culture of IndiaPainted Grey Ware.pptx, PGW Culture of India
Painted Grey Ware.pptx, PGW Culture of IndiaVirag Sontakke
 
How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17Celine George
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxNirmalaLoungPoorunde1
 
Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17Celine George
 
How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxmanuelaromero2013
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Educationpboyjonauth
 
Proudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxProudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxthorishapillay1
 
DATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginnersDATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginnersSabitha Banu
 
History Class XII Ch. 3 Kinship, Caste and Class (1).pptx
History Class XII Ch. 3 Kinship, Caste and Class (1).pptxHistory Class XII Ch. 3 Kinship, Caste and Class (1).pptx
History Class XII Ch. 3 Kinship, Caste and Class (1).pptxsocialsciencegdgrohi
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxpboyjonauth
 
Solving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxSolving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxOH TEIK BIN
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptxVS Mahajan Coaching Centre
 

Recently uploaded (20)

Historical philosophical, theoretical, and legal foundations of special and i...
Historical philosophical, theoretical, and legal foundations of special and i...Historical philosophical, theoretical, and legal foundations of special and i...
Historical philosophical, theoretical, and legal foundations of special and i...
 
Pharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdfPharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdf
 
Meghan Sutherland In Media Res Media Component
Meghan Sutherland In Media Res Media ComponentMeghan Sutherland In Media Res Media Component
Meghan Sutherland In Media Res Media Component
 
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfEnzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
 
CELL CYCLE Division Science 8 quarter IV.pptx
CELL CYCLE Division Science 8 quarter IV.pptxCELL CYCLE Division Science 8 quarter IV.pptx
CELL CYCLE Division Science 8 quarter IV.pptx
 
Hierarchy of management that covers different levels of management
Hierarchy of management that covers different levels of managementHierarchy of management that covers different levels of management
Hierarchy of management that covers different levels of management
 
Crayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon ACrayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon A
 
Painted Grey Ware.pptx, PGW Culture of India
Painted Grey Ware.pptx, PGW Culture of IndiaPainted Grey Ware.pptx, PGW Culture of India
Painted Grey Ware.pptx, PGW Culture of India
 
How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptx
 
Model Call Girl in Bikash Puri Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Bikash Puri  Delhi reach out to us at 🔝9953056974🔝Model Call Girl in Bikash Puri  Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Bikash Puri Delhi reach out to us at 🔝9953056974🔝
 
Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17
 
How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptx
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Education
 
Proudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxProudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptx
 
DATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginnersDATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginners
 
History Class XII Ch. 3 Kinship, Caste and Class (1).pptx
History Class XII Ch. 3 Kinship, Caste and Class (1).pptxHistory Class XII Ch. 3 Kinship, Caste and Class (1).pptx
History Class XII Ch. 3 Kinship, Caste and Class (1).pptx
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptx
 
Solving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxSolving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptx
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
 

Binary Tree Traversal and Representation

  • 1. Binary trees: introduction (complete and extended binary trees), Memory representation (sequential, linked) Binary tree traversal: pre-order, in-order and post-order (traversal algorithms using stacks)
  • 2. A tree is a finite set of zero or more nodes such that: There is a specially designated node called the root. The remaining nodes are partitioned into n>=0 disjoint sets T1, ..., Tn, where each of these sets is a tree. We call T1, ..., Tn the subtrees of the root. Definition of Tree
  • 3. Level and Depth K L E F B G C M H I J D A Level 0 1 2 3 node (13) degree of a node(shown by green number) leaf (terminal) nonterminal parent children sibling ancestor descendant level of a node height(depth) of a tree (4) 3 2 1 3 2 0 0 1 0 0 0 0 0
  • 4. Terminology The degree of a node is the number of subtrees of the node The degree of A is 3; the degree of C is 1. The node with degree 0 is a leaf or terminal node. A node that has subtrees is the parent of the roots of the subtrees. The roots of these subtrees are the children of the node. Children of the same parent are siblings. The ancestors of a node are all the nodes along the path from the root to the node.
  • 5. Tree Properties A B C D G E F I H Property Value Number of nodes Height Root Node Leaves Interior nodes Number of levels Ancestors of H Descendants of B Siblings of E degree of node A
  • 6. Binary Trees A special class of trees: max degree for each node is 2 Recursive definition: A binary tree is a finite set of nodes that is either empty or consists of a root and two disjoint binary trees called the left subtree and the right subtree.
  • 8. Samples of Trees A B A B A B C G E I D H F Complete Binary Tree Skewed Binary Tree E C D
  • 9. Maximum Number of Nodes in BT The maximum number of nodes on level i of a binary tree is 2i, i>=0. The maximum number of nodes in a binary tree of depth k is 2k-1, k>=1.
  • 10. Full BT vs. Complete BT A full binary tree of depth k is a binary tree of depth k having 2k-1 nodes, k>=1. A binary tree with n nodes and depth k is complete iff its nodes correspond to the nodes numbered from 1 to n in the full binary tree of depth k. A B C G E I D H F A B C G E K D J F I H O N M L Full binary tree of depth 4 Complete binary tree
  • 11. Complete Binary Tree If a complete binary tree with n nodes (depth=└log n + 1┘) is represented sequentially, then for any node with index i, 1<=i<=n, we have: parent(i) is at └i/2┘ if i!=1. If i=1, i is at the root and has no parent. leftChild(i) is at 2i if 2i<=n. If 2i>n, then i has no left child. rightChild(i) is at 2i+1 if 2i+1<=n. If 2i+1>n, then i has no right child.
  • 12. Extended Binary Tree  A binary tree T is said to be a 2-tree or an extended binary tree if each node N has either 0 or 2 children.  The nodes with 2 children are called internal nodes.  The nodes with o children are called external nodes. A B C G D F A B C G D F Fig: Binary Tree T Fig: Extended 2-tree
  • 14. Linked Representation struct btnode { int data; btnode *left, *right; }; data left right data left right
  • 15. Binary Tree Traversals There are three standard ways of traversing a binary tree T with root R. These three algorithms, called: preorder inorder postorder
  • 16. Arithmetic Expression Using BT + * A * / E D C B preorder traversal + * * / A B C D E prefix expression inorder traversal A / B * C * D + E infix expression postorder traversal A B / C * D * E + postfix expression
  • 17. Preorder Traversal (recursive version) Algorithm: 1. Process the root R. 2. Traverse the left subtree of R in preorder. 3. Traverse the right subtree of R in preorder. Pseudo-Code: void preorder(btnode ptr) /* preorder tree traversal */ { if (ptr!=NULL) { cout<<ptr->data; preorder(ptr->left); predorder(ptr->right); } } + * * / A B C D E
  • 18. Inorder Traversal (recursive version) Algorithm: 1. Traverse the left subtree of R in inorder. 2. Process the root R. 3. Traverse the right subtree of R in inorder. Pseudo-Code: void inorder(btnode ptr) /* inorder tree traversal */ { if (ptr!=NULL) { inorder(ptr->left); cout<<ptr->data; indorder(ptr->right); } } A / B * C * D + E
  • 19. Postorder Traversal (recursive version) Algorithm: 1. Traverse the left subtree of R in postorder. 2. Traverse the right subtree of R in postorder. 3. Process the root R. Pseudo-Code: void postorder(btnode ptr) /* postorder tree traversal */ { if (ptr!=NULL) { postorder(ptr->left); postorder(ptr->right); cout<<ptr->data; } } A B / C * D * E +
  • 20. Preorder Traversal (using stack) PREORD(INFO, LEFT, RIGHT, ROOT) 1. Set TOP:= 1, STACK[1]:= NULL and PTR := ROOT. 2. Repeat Steps 3 to 5 while PTR != NULL: 3. Apply PROCESS to INFO[PTR]. 4. If RIGHT[PTR]!= NULL then Set TOP := TOP+1, and STACK[TOP]:= RIGHT[PTR]. 5. If LEFT[PTR]!= NULL then Set PTR:= LEFT[PTR]. Else Set PTR:= STACK[TOP] TOP:= TOP-1. [End of If structure] [End of step 2 Loop] 6. Exit. + * * / A B C D E
  • 21. Inorder Traversal (using stack) INORD(INFO, LEFT, RIGHT, ROOT) 1. Set TOP:= 1, STACK[1]:= NULL and PTR := ROOT. 2. Repeat while PTR != NULL: a) Set TOP:=TOP+1, STACK[TOP]:=PTR. b) Set PTR:= LEFT[PTR]. [End of loop] 3. Set PTR:=STACK[TOP], TOP:=TOP-1. 4. Repeat Steps 5 to 7 while PTR != NULL: 5. Apply PROCESS to INFO[PTR]. 6. If RIGHT[PTR]!= NULL, then: a) Set PTR:=RIGHT[PTR] b) Go to Step 2. [End of If structure] 7. Set PTR:=STACK[TOP] and TOP:=TOP-1. [End of step 4 Loop.] 8. Exit A / B * C * D + E
  • 22. Postorder Traversal (using stack) POSTORD(INFO, LEFT, RIGHT, ROOT) 1. Set TOP:= 1, STACK[1]:= NULL and PTR := ROOT. 2. Repeat Step 3 to 5 while PTR!=NULL: 3. Set TOP := TOP+1, STACK[Top]:=PTR. 4. If RIGHT[PTR]!=NULL, then: Set TOP := TOP+1, Stack[Top]:=-RIGHT[PTR]. [End of If structure] 5. Set PTR:=LEFT[PTR] [End of step 3 Loop] 6. Set PTR:=STACK[Top], TOP := TOP-1. 7. Repeat while PTR>0: a)Apply PROCESS to INFO[PTR]. b)PTR:=STACK[Top], TOP := TOP-1. [End of Loop] 8. Repeat while PTR<0: a) Set PTR:= -PTR. b) Go to Step 2. [End of If structure] 9. Exit. A B / C * D * E +