0% found this document useful (0 votes)
58 views

Traversal: Linear Data Structures Linked Lists Arrays

There are three main types of tree traversals: preorder, inorder, and postorder. Preorder visits the root node first, then traverses the left subtree and finally the right subtree. Inorder first traverses the left subtree, then visits the root node, and finally the right subtree. Postorder first traverses the left subtree, then the right subtree, and finally visits the root node. Breadth-first traversal visits every node on each level before moving to the next level.
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 DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
58 views

Traversal: Linear Data Structures Linked Lists Arrays

There are three main types of tree traversals: preorder, inorder, and postorder. Preorder visits the root node first, then traverses the left subtree and finally the right subtree. Inorder first traverses the left subtree, then visits the root node, and finally the right subtree. Postorder first traverses the left subtree, then the right subtree, and finally visits the root node. Breadth-first traversal visits every node on each level before moving to the next level.
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 DOC, PDF, TXT or read online on Scribd
You are on page 1/ 3

Traversal

Compared to linear data structures like linked lists and one dimensional arrays, which have a
canonical method of traversal, tree structures can be traversed in many different ways. Starting at
the root of a binary tree, there are three main steps that can be performed and the order in which
they are performed defines the traversal type. These steps (in no particular order) are: performing
an action on the current node (referred to as "visiting" the node), traversing to the left child node,
and traversing to the right child node. Thus the process is most easily described
through recursion. The names given for particular style of traversal came from the position of root
element with regard to the left and right nodes. Imagine that the left and right nodes are constant
in space, then the root node could be placed to the left of the left node (pre-order), between the
left and right node (in-order), or to the right of the right node (post-order).
[edit]Depth-first Traversal

To traverse a non-empty binary tree in preorder, perform the following operations recursively at
each node, starting with the root node:

1. Visit the root.


2. Traverse the left subtree.
3. Traverse the right subtree.

To traverse a non-empty binary tree in inorder (symmetric), perform the following operations
recursively at each node:

1. Traverse the left subtree.


2. Visit the root.
3. Traverse the right subtree.

To traverse a non-empty binary tree in postorder, perform the following operations recursively at
each node:

1. Traverse the left subtree.


2. Traverse the right subtree.
3. Visit the root.

For more information, see Depth-first traversal.


[edit]Breadth-first Traversal
Finally, trees can also be traversed in level-order, where we visit every node on a level before
going to a lower level. This is also calledBreadth-first traversal.

[edit]Example

In this binary search tree

 Preorder traversal sequence: F, B, A, D, C, E, G, I, H (root, left, right)


 Inorder traversal sequence: A, B, C, D, E, F, G, H, I (left, root, right); note how this produces a
sorted sequence
 Postorder traversal sequence: A, C, E, D, B, H, I, G, F (left, right, root)

 Level-order traversal sequence: F, B, G, A, D, I, C, E, H

pre-order in-order post-order level-order

push F
push F B A push F B G
pop F push F B A
pop A pop F
push G B pop A
pop B push A D
pop B push D C
push D C pop B
push D A pop C
pop C push I
pop A push E
pop D pop G
pop D pop E
push E pop A
push E C pop D
pop E push C E
pop C pop B
pop F pop D
pop E push G I H
push G push H
pop G pop H
pop G pop I
push I pop I
push I H pop C
pop I pop G
pop H pop E
push H pop F
pop I pop H
pop H
s

You might also like