Skip to content

Instantly share code, notes, and snippets.

@wilbo
Last active October 28, 2017 10:33
Show Gist options
  • Save wilbo/5891fb6179fb80889d7eacd7d5ec6d0d to your computer and use it in GitHub Desktop.
Save wilbo/5891fb6179fb80889d7eacd7d5ec6d0d to your computer and use it in GitHub Desktop.
Tree traversals
public class Node
{
public int Value;
public Node Left = null;
public Node Right = null;
public Node(int value)
{
Value = value;
}
// Pre Order Traversal
public void PrintPreOrder()
{
Console.WriteLine(Value.ToString());
if (Left != null)
Left.PrintPreOrder();
if (Right != null)
Right.PrintPreOrder();
}
// In Order Traversal
public void PrintInOrder()
{
if (Left != null)
Left.PrintInOrder();
Console.WriteLine(Value.ToString());
if (Right != null)
Right.PrintInOrder();
}
// Post Order Traversal
public void PrintPostOrder()
{
if (Left != null)
Left.PrintPostOrder();
if (Right != null)
Right.PrintPostOrder();
Console.WriteLine(Value.ToString());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment