Last active
October 28, 2017 10:33
-
-
Save wilbo/5891fb6179fb80889d7eacd7d5ec6d0d to your computer and use it in GitHub Desktop.
Tree traversals
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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