Search code examples
c#data-structuresc5

Using C5 Collection Tree Data Structure


I have been struggling for days finding .NET Tree Data Structure, I have read many recomendation using C5 library, but I have yet find example for it.

Basic Tree

I have read C5 documentation but didn't find example for it (I admit I haven't read all documentation page).

Edit: I need a Tree with basic functionality like search from parent to child node and vice versa.


Solution

  • If you only need the most basic functionality, then build your own data structure.

    I did a quick implementation of a basic tree (directional edges and not necessarily binary tree), assuming you have a fixed root node. I also added methods for searching depth first and breadth first.

    using System;
    using System.Collections.Generic;
    namespace TreeTest
    {
        class Program
        {
            static void Main(string[] args)
            {
                //Build example tree
                Tree tree = new Tree();
                Node a = new Node(2);
                Node b = new Node(7);
                Node c = new Node(2);
                Node d = new Node(6);
                Node e = new Node(5);
                Node f = new Node(11);
                Node g = new Node(5);
                Node h = new Node(9);
                Node i = new Node(4);
    
                tree.rootNode = a;
                a.Edges.Add(b);
                b.Edges.Add(c);
                b.Edges.Add(d);
                d.Edges.Add(e);
                d.Edges.Add(f);
                a.Edges.Add(g);
                g.Edges.Add(h);
                h.Edges.Add(i);
    
                //Find node scannin tree from top down
                Node node = tree.FindByValueBreadthFirst(6);
                Console.WriteLine(node != null ? "Found node" : "Did not find node");
    
                //Find node scanning tree branch for branch.
                node = tree.FindByValueDepthFirst(2);
                Console.WriteLine(node != null ? "Found node" : "Did not find node");
    
                Console.WriteLine("PRESS ANY KEY TO EXIT");
                Console.ReadKey();
            }
        }
        class Tree
        {
            public Node rootNode;
            public Node FindByValueDepthFirst(int val)
            {
                return rootNode.FindRecursiveDepthFirst(val);
            }
            public Node FindByValueBreadthFirst(int val)
            {
                if (rootNode.Value == val)
                    return rootNode;
                else
                    return rootNode.FindRecursiveBreadthFirst(val);
            }
        }
        class Node
        {
            public int Value { get; set; }
            public IList<Node> Edges { get; set; }
            public Node(int val)
            {
                Value = val;
                Edges = new List<Node>(2);
            }
            public Node FindRecursiveBreadthFirst(int val)
            {
                foreach (Node node in Edges)
                {
                    if (node.Value == val)
                        return node;
                }
                foreach (Node node in Edges)
                {
                    Node result = node.FindRecursiveBreadthFirst(val);
                    if (result != null)
                        return result;
                }
                return null;
            }
            public Node FindRecursiveDepthFirst(int val)
            {
                if (Value == val)
                    return this;
                else
                {
                    foreach (Node node in Edges)
                    {
                        Node result = node.FindRecursiveDepthFirst(val);
                        if (result != null)
                            return result;
                    }
                    return null;
                }
            }
        }
    }