0

我一直在努力寻找 .NET Tree Data Structure,我已经阅读了许多使用C5 库的推荐,但我还没有找到它的例子。

基本树

我已经阅读了 C5 文档,但没有找到它的示例(我承认我没有阅读所有文档页面)。

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

4

2 回答 2

2

如果您需要树数据结构,只需定义您的。(会浪费更少的时间)

public abstract class NodeAbstract
{
   abstract NodeAbstract Left {get;set:}
   abstract NodeAbstract Right {get;set:}
   .... 
   ....
}

public class NodeConcrete : NodeAbstract
{

   .... 
   //implementation
}
于 2012-02-15T12:35:18.300 回答
1

如果你只需要最基本的功能,那就构建自己的数据结构。

假设您有一个固定的根节点,我快速实现了一个基本树(有向边,不一定是二叉树)。我还添加了搜索深度优先和广度优先的方法。

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;
            }
        }
    }
}
于 2012-02-15T13:57:05.770 回答