One of the simplest way to define a tree structure is using already available list implementation, for example:
using NodeList = System.Collections.Generic.List<treenode>;
class TreeNode {
internal string Name { get; set; }
internal TreeNode[] Children { get { return children; } }
NodeList children = new NodeList();
internal void AddChild(string name) { }
}
During parsing, you need to walk the tree and find matching path, adding new children as nodes which haven't been added before.
Anyway, this is a really bad representation of tree in your input data. Are you sure it's given and you cannot change it into anything better? Did you get an idea on tree operations?
The property
Name
is shown just for example. Another step would be using a generic parameter:
using System.Collections.Generic;
class TreeNode<NodeInfo> {
internal NodeInfo Info { get; set; }
internal TreeNode<NodeInfo>[] Children { get { return children; } }
List<TreeNode<NodeInfo>> children = new List<TreeNode<NodeInfo>>();
internal void AddChild(NodeInfo name) { }
}
You may need to add tree operations to the node class, such as insertion, removals, search, and so on…
—SA