0% found this document useful (0 votes)
7 views39 pages

Traversal Methods

Uploaded by

Kingsman
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views39 pages

Traversal Methods

Uploaded by

Kingsman
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 39

Data Structure(BCS301)

Pranveer Singh Institute of Technology,


Kanpur
W9.L2
Binary Tree Traversal Methods

• In a traversal of a binary tree, each element of


the binary tree is visited exactly once.
• During the visit of an element, all action (make
a clone, display, evaluate the operator, etc.)
with respect to this element is taken.
Binary Tree Traversal Methods

• Preorder
• Inorder
• Postorder
• Level order
Preorder Traversal
void preOrder(treePointer ptr)
{
if (ptr != NULL)
{
visit(t);
preOrder(ptr->leftChild);
preOrder(ptr->rightChild);
}
}
Preorder Example (Visit = print)
a

b c

abc
Preorder Example (Visit = print)
a

b c
f
d e
g h i j

abdghei cf j
Preorder Of Expression Tree
/

* +
e f
+ -
a b c d

/ * +a b - c d +e f

Gives prefix form of expression!


Inorder Traversal

void inOrder(treePointer ptr)


{
if (ptr != NULL)
{
inOrder(ptr->leftChild);
visit(ptr);
inOrder(ptr->rightChild);
}
}
Inorder Example (Visit = print)
a

b c

bac
Inorder Example (Visit = print)
a

b c
f
d e
g h i j

gdhbei af j c
Inorder By Projection (Squishing)
a

b c
f
d e
g h i j

g d h b e i a f jc
Inorder Of Expression Tree
/

* +
e f
+ -
a b c d

a + b * c - d/ e + f

Gives infix form of expression (sans parentheses)!


Inorder Tree Traversal without
Recursion
Using Stack is the obvious way to traverse tree without
recursion.
Algorithm
1.Create an empty stack S
2) Initialize current node as root
3) Push the current node to S and set current = current->left until current
is NULL
4) If current is NULL and stack is not empty then
a) Pop the top item from stack.
b) Print the popped item, set current = popped_item->right
c) Go to step 3.
5) If current is NULL and stack is empty then we are done.
Example

Step 1 Creates an empty stack: S = NULL


Step 2 sets current as address of root: current -> 1 1
Step 3 Pushes the current node and
set current = current->left until current is NULL
current -> 1
push 1: Stack S -> 1 2 3
current -> 2
push 2: Stack S -> 2, 1
current -> 4
push 4: Stack S -> 4, 2, 1
4 5
current = NULL
Example…
Step 4 pops from S
a) Pop 4: Stack S -> 2, 1
b) print "4"
c) current = NULL /*right of 4 */ and go to step 3 1
Since current is NULL step 3 doesn't do anything.
Step 4 pops again.
a) Pop 2: Stack S -> 1
b) print "2"
2 3
c) current -> 5/*right of 2 */ and go to step 3
Step 3 pushes 5 to stack and makes
current NULL
4 5
Stack S -> 5, 1
current = NULL
Example…
Step 4 pops from S
a) Pop 5: Stack S -> 1
b) print "5"
c) current = NULL /*right of 5 */ and go to step 3 1
Since current is NULL step 3 doesn't do anything
Step 4 pops again.
a) Pop 1: Stack S -> NULL
b) print "1"
2 3
c) current -> 3 /*right of 1 */

Step 3 pushes 3 to stack and makes current NULL


4 5
Stack S -> 3
current = NULL
Example…
Step 4 pops from S
a) Pop 3: Stack S -> NULL
b) print "3"
c) current = NULL /*right of 3 */ 1
Traversal is done now as
stack S is empty and current is NULL.
2 3

4 5
Morris Traversal
No stacks, no recursion!
Inorder Traversal
P

M L

S E
R
A A
T E

E
inorder predecessor of node with left subtree
is the right child of node’s left subtree
Morris Traversal

• The NULL right child of a node is used to point


to the node’s inorder successor. This is called the
start of a thread.
• If a node has a left sub-tree then it is the end-
point of a thread.
• The start-point of its thread is its inorder
predecessor, i.e. the right-most child of its left
subtree
• Thread goes from node to inorder successor
Morris Traversal
1. Initialize current as root
2. While current is not NULL
If current does not have left child
a) Print current’s data
b) Go to the right, i.e., current = current->right
Else
a) Make current as right child of the rightmost
node in current's left subtree
b) Go to this left child, i.e., current = current->left
Postorder Traversal

void postOrder(treePointer ptr)


{
if (ptr != NULL)
{
postOrder(ptr->leftChild);
postOrder(ptr->rightChild);
visit(t);
}
}
Postorder Example (Visit = print)
a

b c

bca
Postorder Example (Visit = print)
a

b c
f
d e
g h i j

ghdi ebj f ca
Postorder Of Expression Tree
/

* +
e f
+ -
a b c d

a b +c d - * e f + /

Gives postfix form of expression!


Traversal Applications
a

b c
f
d e
g h i j

• Make a clone.
• Determine height.
• Determine number of nodes.
Level Order
Let ptr be a pointer to the tree root.
while (ptr != NULL)
{
visit node pointed at by ptr and put its children on
a FIFO queue;
if FIFO queue is empty, set ptr = NULL;
otherwise, delete a node from the FIFO queue and
call it ptr;
}
Level-Order Example (Visit = print)
a

b c
f
d e
g h i j

abcdef ghi j
Binary Tree Construction
• Suppose that the elements in a binary tree
are distinct.
• Can you construct the binary tree from
which a given traversal sequence came?
• When a traversal sequence has more than
one element, the binary tree is not uniquely
defined.
• Therefore, the tree from which the sequence
was obtained cannot be reconstructed
uniquely.
Some Examples
preorder a a
= ab b b

inorder b a
= ab a b

postorder b b
= ab a a

level order a a
= ab b b
Binary Tree Construction

• Can you construct the binary tree,


given two traversal sequences?
• Depends on which two sequences are
given.
Preorder And Postorder

preorder = ab a a
postorder = ba b b

• Preorder and postorder do not uniquely define a


binary tree.
• Nor do preorder and level order (same example).
• Nor do postorder and level order (same
example).
Inorder And Preorder
• inorder = g d h b e i a f j c
• preorder = a b d g h e i c f j
• Scan the preorder left to right using the
inorder to separate left and right subtrees.
• a is the root of the tree; gdhbei are in the
left subtree; fjc are in the right subtree.

gdhbei fjc
Inorder And Preorder
a

gdhbei fjc
• preorder = a b d g h e i c f j
• b is the next root; gdh are in the left
subtree; ei are in the right subtree.
a

b fjc
gdh ei
Inorder And Preorder
a

b fjc
gdh ei
• preorder = a b d g h e i c f j
• d is the next root; g is in the left
subtree; h is in the right subtree.
a
b fjc
d ei
g h
Inorder And Postorder

• Scan postorder from right to left using


inorder to separate left and right subtrees.
• inorder = g d h b e i a f j c
• postorder = g h d i e b j f c a
• Tree root is a; gdhbei are in left subtree; fjc
are in right subtree.
Inorder And Level Order

• Scan level order from left to right using


inorder to separate left and right subtrees.
• inorder = g d h b e i a f j c
• level order = a b c d e f g h i j
• Tree root is a; gdhbei are in left subtree; fjc
are in right subtree.

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