0% found this document useful (0 votes)
41 views9 pages

DS29.

The document discusses three algorithms for traversing binary trees: preorder, inorder, and postorder traversal. It provides pseudocode for implementing preorder and inorder traversals using a stack. For preorder traversal, the root node is processed before traversing its left and right subtrees. For inorder traversal, the left subtree is traversed before processing the root node and then the right subtree.
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
41 views9 pages

DS29.

The document discusses three algorithms for traversing binary trees: preorder, inorder, and postorder traversal. It provides pseudocode for implementing preorder and inorder traversals using a stack. For preorder traversal, the root node is processed before traversing its left and right subtrees. For inorder traversal, the left subtree is traversed before processing the root node and then the right subtree.
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 9

Traversing Binary Trees

Three Algorithms
 Preorder (NLR)
 Process the root R.

 Traverse the left subtree of R in Preorder.

 Traverse the right subtree of R in Preorder.

 Inorder (LNR)
 Traverse the left subtree of R in Inorder.

 Process the root R.

 Traverse the right subtree of R in Inorder.

 Postorder (LRN)
 Traverse the left subtree of R in Postorder.

 Traverse the right subtree of R in Postorder.

 Process the root R.


Example
 From the tree find out the Inorder & Postorder
A

B C

D E G H

F J K

L
Inorder: - D B F E A G C L J H K
Postorder: - D F E B G L J K H C A
Solve the Expression: -
 (a – b) / ((c * d) + e)
/
- +
a b * e
c d

Preorder: - ?
Postorder: - ?
Traversal Algo using Stacks
 PREORD(INFO, LEFT, RIGHT, ROOT)
A binary tree T is in memory. The algorithm does a Preorder traversal
of T, applying an operation PROCESS to each of its nodes. An array
STACK is used to temporarily hold the addresses of nodes.
1. [Initially push NULL onto STACK, & initialize PTR.] Set TOP := 1,
STACK[1] := NULL & PTR := ROOT.
2. Repeat Steps 3 to 5 while PTR != NULL.
3. Apply PROCESS to INFO[PTR].
4. [Right Child?] If RIGHT[PTR] != NULL, then: [Push on STACK.]
Set TOP := TOP + 1, & STACK[TOP] := RIGHT[PTR]. [End of
If structure.]
5. [Left Child?] If LEFT[PTR] != NULL, then: Set PTR := LEFT[PTR].
Else: [Pop from STACK.] Set PTR := STACK[TOP] & TOP := TOP
+ 1. [END OF If structure.] [End of Step 2 loop.]
6. Exit.
Example: -

STACK: - Φ A
PTR : = A
B C

D E F

G H

K
 Preorder: - A B D G H K C E F
 INORD(INFO, LEFT, RIGHT, ROOT)
A binary tree T is in memory. The algorithm does a Inorder traversal
of T, applying an operation PROCESS to each of its nodes. An
array STACK is used to temporarily hold the addresses of nodes.
1. [Initially push NULL onto STACK, & initialize PTR.]
Set TOP := 1, STACK[1] := NULL & PTR := ROOT.
2. Repeat while PTR != NULL: [Pushes left-most path onto stack.]
(a) Set TOP := TOP + 1, & STACK[TOP] := PTR. [Saves node.]
(b) Set PTR := LEFT[PTR]. [Updates PTR.]
[End of loop.]
3. Set PTR := STACK[TOP] & TOP := TOP - 1. [Pops nodes.]
4. Repeat Steps 5 to 7 while PTR != NULL: [Backtracking.]
5. Apply PROCESS to INFO[PTR].
6. [Right Child?] If RIGHT[PTR] != NULL, then:
(a) Set PTR := RIGHT[PTR].
(b) Go to Step 3.
[End of If structure.]
7. Set PTR := STACK[TOP] & TOP := TOP - 1. [Pops nodes.]
[End of Step 4 loop.]
8. Exit.
Example: -

STACK: - Φ A
PTR : = A
B C

D E

G H

K L M

 Inorder: - K G D L H M B A E C
Thank You!!!

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy