Skip to content

Instantly share code, notes, and snippets.

@jianminchen
Created February 25, 2017 20:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jianminchen/bb1c403002c46ff2cf1d558fc0de3e29 to your computer and use it in GitHub Desktop.
Save jianminchen/bb1c403002c46ff2cf1d558fc0de3e29 to your computer and use it in GitHub Desktop.
Ternary tree preorder traversal - code review
namespace TernaryTreePreorderTraversal
{
class TernaryTree
{
static void Main(string[] args)
{
RunTestcase();
}
/*
* Ternary tree:
* 1
* / | \
* 3 2 5
* /| |
* 4 33 22
*/
public static void RunTestcase()
{
var root = new TernaryTree(1);
root.Middle = new TernaryTree(2);
root.Left = new TernaryTree(3);
root.Right = new TernaryTree(4);
root.Middle.Middle = new TernaryTree(22);
root.Left.Middle = new TernaryTree(33);
root.Right = new TernaryTree(5);
TernaryTree.PreOrderTraversal(root);
// manually verify the console output is 1 2 22 3 33 4 5
}
public TernaryTree Left { get; set; }
public TernaryTree Right { get; set; }
public TernaryTree Middle { get; set; }
public int Data { get; set; }
public TernaryTree(int val)
{
Data = val;
}
public static void PreOrderTraversal(TernaryTree node)
{
if (node == null)
{
return;
}
Console.WriteLine(node.Data);
PreOrderTraversal(node.Middle);
PreOrderTraversal(node.Left);
PreOrderTraversal(node.Right);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment