ITreeNode
ITreeNode is general purpose tree node.
/// <summary>Generic tree node</summary>
public interface ITreeNode
{
/// <summary>Name of node, e.g. "Menu". Must not contain slash '/'</summary>
string Name { get; set; }
/// <summary>Parent node</summary>
ITreeNode? Parent { get; set; }
/// <summary>Children</summary>
IList<ITreeNode> Children { get; set; }
/// <summary>Associated object</summary>
object? UserData { get; set; }
/// <summary>Node at <paramref name="path"/>.</summary>
/// <param name="path">Relative path "Node/Node", "../Node" or absolute "/Menu/Node"</param>
ITreeNode? this[string path] { get; set; }
/// <summary>Try add <paramref name="child"/> to tree.</summary>
/// <param name="path">Relative path, e.g. "Settings", "../Settings", or absolute "/Menu/Settings"</param>
/// <exception cref="InvalidOperationException">If the end of <paramref name="path"/> doesn't match <see cref="ITreeNode.Name"/>.</exception>
bool TryAdd(ReadOnlySpan<char> path, ITreeNode child, char separator = '/');
/// <summary>Try-get or try-get-or-create a node at <paramref name="path"/>.</summary>
/// <param name="path">Path to node, relative or absolute, e.g. "Menu/Settings", "/Menu/Settings", "../Attributions"</param>
/// <param name="create">If true, creates nodes that don't exist</param>
bool TryGet(ReadOnlySpan<char> path, [NotNullWhen(true)] out ITreeNode? child, bool create = false, char separator = '/');
/// <summary>Add <paramref name="child"/>.</summary>
void Add(ITreeNode child);
}
TreeNode is the default implementation.
ITreeNode root = new TreeNode();
TryGet(path, out child, create, separator) get or creates child nodes.
root.TryGet("/folder/folder1/node", out ITreeNode? child, create: true, separator: '/');
root.TryGet("/folder/folder2/node", out child, create: true, separator: '/');
child!.UserData = "Hello World";
AsKeyValues() visits tree as KeyValuePair<string, object>.
foreach (var kv in root.AsKeyValues(delimiter: '/'))
WriteLine(kv.Key);
ListDecendents() visits tree. Path() prints canonical path to root using specific separator.
ITreeNode root = new TreeNode();
/folder
/folder/folder1
/folder/folder1/node
/folder/folder2
/folder/folder2/node
Full Example
Full example
using Avalanche.Utilities.Tree;
using static System.Console;
public class treenode
{
public static void Run()
{
{
// <01>
ITreeNode root = new TreeNode();
// </01>
// <02>
root.TryGet("/folder/folder1/node", out ITreeNode? child, create: true, separator: '/');
root.TryGet("/folder/folder2/node", out child, create: true, separator: '/');
child!.UserData = "Hello World";
// </02>
// <03>
foreach (var kv in root.AsKeyValues(delimiter: '/'))
WriteLine(kv.Key);
// </03>
// <04>
foreach (ITreeNode kv in root.ListDecendents())
WriteLine(kv.Path('/'));
// </04>
}
}
}