使用C5集合树数据结构

发布于 2025-01-06 00:12:07 字数 323 浏览 0 评论 0原文

我几天来一直在努力寻找 .NET 树数据结构,我已经阅读了许多使用 C5 库,但我还没有找到它的示例。

Basic Tree

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

编辑:我需要一棵具有基本功能的树,例如从父节点到子节点的搜索,反之亦然。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

断肠人 2025-01-13 00:12:07

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

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

public class NodeConcrete : NodeAbstract
{

   .... 
   //implementation
}

If you need only tree datastructure, just define yours. (will loose less time)

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

public class NodeConcrete : NodeAbstract
{

   .... 
   //implementation
}
雨后咖啡店 2025-01-13 00:12:07

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

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

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;
            }
        }
    }
}

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;
            }
        }
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文