2016-11-15 1 views
-2

좋아,이 함수를 실행할 때마다 이진 트리의 각 수준에서 노드를 참조하는 linkendlist 목록을 가져올 것으로 예상됩니다. 그러나이 함수는 모든 레벨에 대해 빈 linkendlists를 반환합니다. 누군가 왜 저에게 말할 수 있습니까? 나는 자바에서도 해결책을 추가했다.C# list가 변수에 대한 참조를 추가하는 이유는 무엇입니까?

 public static void Main() 
     { 
      BinaryTree t = new BinaryTree(0); 

      BinaryTree l1 = new BinaryTree(1); 
      BinaryTree l2 = new BinaryTree(2); 
      BinaryTree l3 = new BinaryTree(3); 
      BinaryTree l4 = new BinaryTree(4); 
      BinaryTree l5 = new BinaryTree(5); 
      BinaryTree l6 = new BinaryTree(6); 

      t.left = l1; 
      t.right = l4; 

      l1.left = l2; 
      l1.right = l3; 

      l4.left = l5; 
      l4.right = l6; 

      List<LinkedList<BinaryTree>> lod = ListOfDepth(t); 

      // this will print empty list 
      foreach (var item in lod) 
      { 
       foreach (var lst in item) 
       { 
        Console.Write(lst.data); 

       } 
       Console.WriteLine(); 
      } 


} 
    public class BinaryTree 
    { 
     public BinaryTree(int d) { data = d; } 
     public BinaryTree() { } 
     public int id; 
     public int data; 
     public BinaryTree left = null; 
     public BinaryTree right = null; 
} 

public static List<LinkedList<BinaryTree>> ListOfDepth(BinaryTree t) 
{ 
    List<LinkedList<BinaryTree>> lst = new List<LinkedList<BinaryTree>>(); 
    // add current level to lst 
    LinkedList<BinaryTree> curLevel = new LinkedList<BinaryTree>(); 
    curLevel.AddLast(t); 
    lst.Add(curLevel); 

    LinkedList<BinaryTree> newLevel = new LinkedList<BinaryTree>(); 
    while (curLevel.Count > 0) 
    { 
     BinaryTree curNode = curLevel.First.Value; 
     curLevel.RemoveFirst(); 

     if (curNode.left != null) 
     { 
      newLevel.AddLast(curNode.left); 
     } 
     if (curNode.right != null) 
     { 
      newLevel.AddLast(curNode.right); 
     } 

     if (curLevel.Count == 0) 
     { 
      curLevel = newLevel; 
      if (newLevel.Count > 0) 
      { 
       lst.Add(newLevel); 
      } 
      newLevel = new LinkedList<BinaryTree>(); 

     } 

    } 

    return lst; 
} 

Java 버전.

ArrayList<LinkedList<BinaryTree>> listofDepth(BinaryTree root) { 
    ArrayList<LinkedList<BinaryTree>> result = new ArrayList<LinkedList<BinaryTree>>(); 
    LinkedList<BinaryTree> current = new LinkedList<BinaryTree>(); 
    if (root != null) { 
     current.add(root); 
    } 

    while (current.size() > 0) { 
     result.add(current); 
     LinkedList<BinaryTree> parents = current; 
     current = new LinkedList<BinaryTree>(); 

     for (BinaryTree parent : parents) { 
     if (parent.left != null) { 
      current.add(parent.left); 
     } 
     if (parent.right != null) { 
      current.add(parent.right); 
      } 
     } 

    } 
return result; 
} 
+1

C#, Java 및 C++은 3 가지 매우 관련이없는 언어입니다. 구문 상 유사점이 있지만 그 차이가 있습니다. 그들과 그들의 도서관을 비교할 수 없으며 비교할 필요도 없습니다. –

+0

@Someprogrammerdude : 제 질문은 C# 참조에 관한 것입니다. C++을 참조하는 태그를 제거했습니다. – Mark

+0

* 여기 curLevel을 lst에 추가하면 curlevel에 변수를 추가하고 참조하는 것이 아닌 것 같습니다. * 그게 무슨 뜻입니까? –

답변

2

문제는 분명합니다. 자바 버전의 코드에서는 메서드 바깥에있는 변수 current을 사용합니다. 여기에 항목 (root)을 추가합니다.

C# 버전에서는 해당 변수가 없으므로 현재 버전에 추가되지 않습니다. 대신 메소드 안에 새 변수를 작성하여 추가하십시오. 이들은 완전히 다른 두 개의 코드 블록입니다.

+0

죄송합니다. Java에서 솔루션을 타이핑하는 실수였습니다. 실제로 선언이 있습니다. – Mark

+0

나는 C#에서 자바 솔루션을 시도하고 잘 작동하는 것 같다. 나는 그 버그가 어디에서 다른 곳으로 가야하고 내 견해를 이해하지 못하고 있어야한다고 생각한다. – Mark

관련 문제