Skip to content

Instantly share code, notes, and snippets.

@kellypleahy
Created November 15, 2009 01:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save kellypleahy/234915 to your computer and use it in GitHub Desktop.
Save kellypleahy/234915 to your computer and use it in GitHub Desktop.
void Main()
{
var rootList = new List<Node>(new[]{ new Node() }); // only ever one root, I assume.
var childrenAtThisLevel = FillLevel(rootList);
while(childrenAtThisLevel.Count > 0)
childrenAtThisLevel = FillLevel(childrenAtThisLevel);
}
List<Node> FillLevel(List<Node> childrenAtThisLevel)
{
var childrenAtNextLevel = new List<Node>();
foreach(var child in childrenAtThisLevel)
{
child.Label = GetNextSymbol();
var newChildCount = GetNumberOfChildrenForThisSymbolType(child.Label) // 0 1 or 2
for(var i=0; i<newChildCount; i++)
{
var newChild = new Node();
child.Children.Add(newNode);
childrenAtNextLevel.Add(newChild);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment