Create a gist now

Instantly share code, notes, and snippets.

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