# Preorder, Inorder, and Postorder Traversal of a Tree

The following post is derived from notes taken while reading through this textbook .

Some of the terms used in this post have been defined here.

For the purpose of this post, we will be using the tree above. Each node of the tree holds a color and, for our traversal, we will pretend that the names of these colors are being printed as a loop traverses the tree in whichever way is being discussed. We will also assume that the traversals all start at the root-node.

Click here to see the pseudocode for both a recursive, and iterative, preorder traversal.

## Preorder Traversal:

The preorder traversal works on the concept of Node-Left-Right. If we apply a preorder traversal algorithm to the example tree, we will be given the following output.

Red, Purple, Orange, Yellow, and Green.

- The algorithm prints the color of the current node. This color is Red.
- Then it checks if the current node has a left node.
- The algorithm finds a left node.
- The algorithm traverses to the left node.
- The algorithm prints the color of the current node. This color is Purple.
- Then it checks if the current node has a left node.
- The algorithm finds a left node.
- The algorithm traverses to the left node.
- The algorithm prints the color of the current node. This color is Orange.
- The algorithm checks if the current node has a left node.
- The algorithm doesn't find a left node.
- The algorithm checks if the current node has a right node.
- The algorithm finds no right node.
- The algorithm returns to the previous node.
- The algorithm checks if the current node has a right node.
- The algorithm finds a right node.
- The algorithm traverses to the right nod.
- The algorithm prints the color of the current node. This color is Yellow.
- Then it checks if the current node has a left node.
- The algorithm finds no left node.
- The algorithm checks if the current node has a right node.
- The algorithm finds no right node.
- The algorithm returns to the previous node.
- The algorithm checks if the current node has a right node.
- The algorithm finds a right node.
- The algorithm traverses to the right node.
- The algorithm prints the color of the current node. This color is Green.
- Then it checks if the current node has a left node.
- The algorithm finds no left node.
- The algorithm checks if the current node has a right node.
- The algorithm finds no right node.
- The algorithm returns to the previous node.
- The algorithm has no more nodes to go back to.
- The algorithm is finished.

## Inorder Traversal:

The inorder traversal works on the concept of Left-Node-Right. If we apply an inorder traversal algorithm to the example tree, we will be given the following output.

Orange, Purple, Yellow, Red, and Green.

- The algorithm checks if the current node has a left node.
- The algorithm finds a left node.
- The algorithm traverses to the left node.
- The algorithm checks if the current node has a left node.
- The algorithm finds a left node.
- The algorithm traverses to the left node.
- The algorithm checks if the current node has a left node.
- The algorithm doesn't find a left node.
- The algorithm prints the color of the current node. This color is Orange.
- The algorithm checks if the current node has a right node.
- The algorithm doesn't find a right node.
- The algorithm returns to the previous node.
- The algorithm prints the color of the current node. This color is Purple.
- The algorithm checks if the current node has a right node.
- The algorithm finds a right node.
- The algorithm traverses to the right node.
- The algorithm checks if the current node has a left node.
- The algorithm doesn't find a left node.
- The algorithm prints the color of the current node. This color is Yellow.
- The algorithm checks if the current node has a right node.
- The algorithm doesn't find a right node.
- The algorithm returns to the previous node.
- The algorithm returns to the previous node.
- The algorithm prints the color of the current node. This color is Red.
- The algorithm checks if the current node has a right node.
- The algorithm finds a right node.
- The algorithm traverses to the right node.
- The algorithm checks if the current node has a left node.
- The algorithm doesn't find a left node.
- The algorithm prints the color of the current node. This color is Green.
- The algorithm checks if the current node has a right node.
- The algorithm doesn't find a right node.
- The algorithm returns to the previous node.
- The algorithm has no more nodes to go back to.
- The algorithm is finished.

## Postorder Traversal:

The postorder traversal works on the concept of Left-Right-Node. If we apply a postorder traversal algorithm to the example tree, we will be given the following output.

Orange, Yellow, Purple, Green, and Red.

- The algorithm checks if the current node has a left node.
- The algorithm finds a left node.
- The algorithm traverses to the left node.
- The algorithm checks if the current node has a left node.
- The algorithm finds a left node.
- The algorithm traverses to the left node.
- The algorithm checks if the current node has a left node.
- The algorithm doesn't find a left node.
- The algorithm checks if the current node has a right node.
- The algorithm doesn't find a right node.
- The algorithm prints the color of the current node. This color is Orange.
- The algorithm returns to the previous node.
- The algorithm checks if the current node has a right node.
- The algorithm finds a right node.
- The algorithm traverses to the right node.
- The algorithm checks if the current node has a left node.
- The algorithm doesn't find a left node.
- The algorithm checks if the current node has a right node.
- The algorithm doesn't find a right node,.
- The algorithm prints the color of the current node. This color is Yellow.
- The algorithm returns to the previous node.
- The algorithm prints the color of the current node. This color is Purple.
- The algorithm has already finished with this node.
- The algorithm returns to the previous node.
- The algorithm checks if the current node has a right node.
- The algorithm finds a right node.
- The algorithm traverses to the right node.
- The algorithm checks if the current node has a left node.
- The algorithm doesn't find a left node.
- The algorithm checks if the current node has a right node.
- The algorithm doesn't find a right node.
- The algorithm prints the color of the current node. This color is purple.
- The algorithm returns to the previous node.
- The algorithm prints the color of the current node. This color is red.
- The algorithm has no more nodes to go back to.
- The algorithm is finished.