# How to traverse binary tree by depth (Part I) ?

In this article, I will introduce the traversal of binary tree in CSharp.

There are depth first traversal which includes:

• pre-order traversal: root node first, then left node, then right node

• in-order traversal: left node first, then root node, then right node

• post-order traversal: left node first, then right node, then root node

and breath first traversal which is level-order traversal.

In this post I will introduce the depth first traversal, and later for breath first traversal.

So, before we traverse a binary tree, we need to create it before. And before we create a tree, we need to create tree node. I assume the data hold the value, and the left node and right node are its children.

``````public class Node<T>
{
public Node<T> LNode { get; set; }
public Node<T> RNode { get; set; }
public T Data { get; set; }
public Node(T data)
{
Data = data;
}
}
``````

Then, I need to create a binary tree.

``````static Node<string> BinTree()
{
Node<string>[] binTree = new Node<string>;
binTree = new Node<string>("A");
binTree = new Node<string>("B");
binTree = new Node<string>("C");
binTree = new Node<string>("D");
binTree = new Node<string>("E");
binTree = new Node<string>("F");
binTree = new Node<string>("G");
binTree = new Node<string>("H");
binTree = new Node<string>("I");
binTree = new Node<string>("J");
binTree = new Node<string>("K");

binTree.LNode = binTree;
binTree.RNode = binTree;
binTree.LNode = binTree;
binTree.RNode = binTree;
binTree.LNode = binTree;
binTree.RNode = binTree;
binTree.RNode = binTree;
binTree.LNode = binTree;
binTree.LNode = binTree;
binTree.RNode = binTree;
return binTree;
}
``````

Once I have the tree, I can think about creating the different binary tree traversals. Firstly, let’s see the pre-order traversal.

The principe is the we get the root value, then its left node value and finally its right node value.

Remember: Left node is always before Right node.

``````static void PreOrder<T>(Node<T> node)
{
if (node != null)
{
Console.WriteLine(node.Data);
PreOrder<T>(node.LNode);
PreOrder<T>(node.RNode);
}
}
``````

In the previous method, we’ve used recursive method to get the node’s child, and its child’s child etc.

Then, we can see the in-order traversal. It just change the order of the three nodes.

The principe is the we get the its left node value, then root value, and finally its right node value.

``````static void InOrder<T>(Node<T> node)
{
if (node != null)
{
PreOrder<T>(node.LNode);
Console.WriteLine(node.Data);
PreOrder<T>(node.RNode);
}
}
``````

Finally, the post-order traversal. The principe is the we get the its left node value, then its right node value, and finally its root value.

``````static void PostOrder<T>(Node<T> node)
{
if (node != null)
{
PreOrder<T>(node.LNode);
PreOrder<T>(node.RNode);
Console.WriteLine(node.Data);
}
}
``````

Now, we just need to call the different implementations and get the results.

``````public static void Main()
{
Node<string> tree = BinTree();
PreOrder<string>(tree); //result: A B D H E I C F J K G
InOrder(tree); //result: B D H E I A C F J K G
PostOrder<string>(tree); //result: B D H E I C F J K G A