Tree data structure in C#


I was looking for a tree or graph data structure in C# but I guess there isn't one provided. An Extensive Examination of Data Structures Using C# 2.0 explains a bit about why. Is there a convenient library which is commonly used to provide this functionality? Perhaps through a strategy pattern to solve the issues presented in the article.

I feel a bit silly implementing my own tree, just as I would implementing my own ArrayList.

I just want a generic tree which can be unbalanced. Think of a directory tree. C5 looks nifty, but their tree structures seem to be implemented as balanced red-black trees better suited to search than representing a hierarchy of nodes.

12/9/2015 12:30:15 PM

Accepted Answer

My best advice would be that there is no standard tree data structure because there are so many ways you could implement it that it would be impossible to cover all bases with one solution. The more specific a solution, the less likely it is applicable to any given problem. I even get annoyed with LinkedList - what if I want a circular linked list?

The basic structure you'll need to implement will be a collection of nodes, and here are some options to get you started. Let's assume that the class Node is the base class of the entire solution.

If you need to only navigate down the tree, then a Node class needs a List of children.

If you need to navigate up the tree, then the Node class needs a link to its parent node.

Build an AddChild method that takes care of all the minutia of these two points and any other business logic that must be implemented (child limits, sorting the children, etc.)

9/15/2008 9:04:39 PM

delegate void TreeVisitor<T>(T nodeData);

class NTree<T>
    private T data;
    private LinkedList<NTree<T>> children;

    public NTree(T data)
    { = data;
        children = new LinkedList<NTree<T>>();

    public void AddChild(T data)
        children.AddFirst(new NTree<T>(data));

    public NTree<T> GetChild(int i)
        foreach (NTree<T> n in children)
            if (--i == 0)
                return n;
        return null;

    public void Traverse(NTree<T> node, TreeVisitor<T> visitor)
        foreach (NTree<T> kid in node.children)
            Traverse(kid, visitor);

Simple recursive implementation... < 40 lines of code... You just need to keep a reference to the root of the tree outside of the class, or wrap it in another class, maybe rename to TreeNode??

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow