Main Page | See live article | Alphabetical index

# Inorder traversal

In computer science, inorder traversal is used in data structures, and specifically, trees and binary trees.

Programs that utilize tree structures need to process nodes in a tree (represented as circles in below diagram). Nodes contain information about an object. For now, let's assume each node contains a letter. The arrows indicate a link between nodes.

Inorder Traversal is a type of tree traversal algorithm. Inorder refers to when the root is processed inbetween to its two subtrees.

### Steps to Inorder Traversal

Given a non-empty tree,

Given a binary tree PY:

The order would go D,B,G,E,A,C,F

Here is an example of InOrder in C++

```template
int inorder_print(const binary_tree_nodes* ptr)
// ptr is a pointer to a node in a binary tree OR null
// meaning empty tree.
{
if (ptr != NULL)
{
inorder_print( ptr->left() );
std::cout << ptr->data() << std::endl;
inorder_print( ptr->right() );
}
return 0;
}
```
The same example in
```data Tree a = ET | Node(a, Tree a, Tree a)