2012-02-15 86 views
0

我一直在努力尋找.NET的天,我已經閱讀了很多推薦使用C5 library,但我還沒有找到它的例子。使用C5集合樹數據結構

Basic Tree

我已閱讀C5文檔,但沒有發現例如它(我承認我沒有閱讀所有文檔頁面)。

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

回答

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

謝謝你,我的例子正是我需要的:P – 2012-02-15 16:00:51

2

如果您需要樹數據結構,只是定義你的。 (會減少更少的時間)

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

public class NodeConcrete : NodeAbstract 
{ 

    .... 
    //implementation 
} 
+0

謝謝您的回答,您能否詳細說明您的答案? – 2012-02-15 12:44:43

+0

@CodingJunkies:你是什麼意思?簡而言之,我的答案是:如果你只需要**數據持有者那樣的樹,請使用你自己的。但是如果你需要一些樹遍歷算法(比如說),這種情況下你的數據結構可能會變得無用,所以你應該像你所提到的一個外部框架一樣。 – Tigran 2012-02-15 12:48:39

+0

我需要一些基本功能,如從父節點到子節點的搜索,反之亦然。 – 2012-02-15 12:55:55