Skip to content

Instantly share code, notes, and snippets.

@musicm122
Created July 13, 2014 00:24
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 musicm122/488b6ecb177d2a3f3936 to your computer and use it in GitHub Desktop.
Save musicm122/488b6ecb177d2a3f3936 to your computer and use it in GitHub Desktop.
Simple Traversing Nodes in a Tree Example
void Main()
{
List<string> vals = "1 2 3 4 5".Split(' ').ToList<string>();
vals.Dump();
Node tree = new Node();
NodeExt.RecurseSet(tree,vals);
//tree.Dump();
tree.GetRightMostNode().Dump();
var sb = new StringBuilder();
tree.GetValues().Dump();
}
// Define other methods and classes here
public class Node
{
public string Value{get;set;}
public Node Right {get;set;}
}
public static class NodeExt
{
public static void RecurseSet(this Node node, List<string> vals)
{
Node tNode;
for(var i=0;i<vals.Count();i++)
{
tNode = new Node();
tNode.Value = vals[i];
node.Right = tNode;
vals.Remove(vals[i]);
RecurseSet(tNode,vals);
}
}
public static Node GetRightMostNode(this Node node)
{
while(node.Value!=null)
{
node = node.MoveRight();
}
return node;
}
public static Node MoveRight(this Node node)
{
return node.Right;
}
public static string GetValues(this Node node)
{
StringBuilder sb = new StringBuilder();
while (node.Value!=null)
{
sb.Append(node.Value);
node = node.Right;
}
return sb.ToString();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment