2016-08-16 18 views
0

私はツリーDFSをトラバースするときに要素を見つける作業に苦労しています。以下は私のツリーの実装です。別のクラスのオブジェクトによってデータが取り込まれています。私が望むのは、与えられた価値によってツリー内の要素を見つけることです。それをしようとするときにいくつかの真の問題があります。ノードへのKey参照を追加して、この参照のためにすべてのノードを検索する方法はありますか?私は本当に助けに感謝します! :) ありがとうございます。このデータが取り込まれますどのようにその本当に見かけないコードから任意のツリー内の特定の要素を見つける

public class TreeNode<T> 
    { 

    private T value; 
    private bool hasParent; 
    public TreeNode<T> parent; 
    private List<TreeNode<T>> children; 
    public TreeNode(T value, TreeNode<T> parent) 
    { 
     this.parent = parent; 
     if (value == null) 
     { 
      throw new ArgumentNullException(
        "Cannot insert null value!"); 
     } 
     this.value = value; 
     this.children = new List<TreeNode<T>>(); 
    } 

    public T Value 
    { 
     get 
     { 
      return this.value; 
     } 
     set 
     { 
      this.value = value; 
     } 
    } 
    public int ChildrenCount 
    { 
     get 
     { 
      return this.children.Count; 
     } 
    } 
     public class Tree<T> 
    { 
    // The root of the tree 
    private TreeNode<T> root; 


    public Tree(T value) 
    { 
     if (value == null) 
     { 
      throw new ArgumentNullException(
        "Cannot insert null value!"); 
     } 

     this.root = new TreeNode<T>(value,null); 
    } 


    public Tree(T value, params Tree<T>[] children) 
     : this(value) 
    { 
     foreach (Tree<T> child in children) 
     { 
      this.root.AddChild(child.root); 
     } 
    } 

答えて

0

は、しかし、私は、コードにいくつかの調整をしたので、私たちは木を移入し、検索を実現することができ、それをきれいにしようとしています。私はいくつかの公共のプロパティとメソッドを追加しました。

public class Tree<T> 
{ 
    // The root of the tree 
    private TreeNode<T> root; 


    public Tree(T value) 
    { 
     if (value == null) 
     { 
      throw new ArgumentNullException(nameof(value), "Cannot insert null value!"); 
     } 

     root = new TreeNode<T>(value, null); 
    } 


    public Tree(T value, params Tree<T>[] children) 
     : this(value) 
    { 
     foreach (Tree<T> child in children) 
     { 
      root.AddChild(child.root); 
     } 
    } 

    public TreeNode<T> Root => root; 

    public TreeNode<T> FindByValue(T value) => Root.FindByValue(value); 

} 


public class TreeNode<T> 
{ 
    public TreeNode(T value, TreeNode<T> parent) 
    { 
     this.parent = parent; 
     if (value == null) 
     { 
      throw new ArgumentNullException(nameof(parent), "Cannot insert null value!"); 
     } 
     this.value = value; 
     children = new List<TreeNode<T>>(); 
    } 


    private T value; 
    public TreeNode<T> parent; 
    private List<TreeNode<T>> children; 


    public T Value 
    { 
     get { return value; } 
     set { this.value = value; } 
    } 
    public int ChildrenCount => children.Count; 

    public TreeNode<T> AddChild(TreeNode<T> child) 
    { 
     children.Add(child); 
     return child; 
    } 

    public TreeNode<T> AddChild(T value) => AddChild(new TreeNode<T>(value, this)); 

    public TreeNode<T> FindByValue(T value) 
    { 
     if (value.Equals(Value)) 
      return this; 

     foreach (var child in children) 
     { 
      var match = child.FindByValue(value); 
      if (match != null) 
       return match; 
     } 
     return null; 
    } 
} 

使用例:

//create a new tree.. 
var tree = new Tree<string>("root-item"); 

//populate the tree with 10 items with 10 sub items 
for (int i = 0; i < 10; i++) 
{ 
    var node = tree.Root.AddChild($"item-{i}"); 
    for (int w = 0; w < 10; w++) 
    { 
     node.AddChild($"sub-item-{i}-{w}"); 
    } 
} 

//find the root node 
var findNode = tree.Root.FindByValue("root-item"); 

//find a sub node item 
findNode = tree.FindByValue("sub-item-1-1"); 
+0

がたくさんありがとう、これは今はるかに理にかなっています。非常に役に立ちます。 Employeeクラスからインスタンス化されたオブジェクトをツリーに挿入しています。例えば、どうすればいいですかvar node = tree.Root.AddChild($ new Employee); ?私は、このforループの中のオブジェクトをどのように埋めるようにするのですか?この種のデータで可能ですか? –

関連する問題