2011-02-25 5 views
0

이진 검색 트리의 모든 노드에 깊이를 추가하려고합니다. 이것은 내 Add 메서드입니다. 하지만 깊이 속성이 임의의 값으로 설정되는 것을 봅니다. 누군가 내가 실수 한 것을 알려주십시오.깊이 속성이 BST에서 올바른 값을 설정하지 않음

public void Add(int data) 
    { 
     BNode current = root; 
     int depth = 0;   
     while (true) 
     { 
      if (data < (int)current.Data && current.Left != null) 
      { 
       depth = depth + 1; 
       current = current.Left; 
      } 
      else if (data < (int)current.Data && current.Left == null) 
      { 
       depth = depth + 1; 
       current.Left = new BNode(data); 
       current.Left.Parent = current; 
       current.Depth = depth; 
       break; 
      } 
      else if (data > (int)current.Data && current.Right != null) 
      { 
       depth = depth + 1; 
       current = current.Right; 
      } 
      else if (data > (int)current.Data && current.Right == null) 
      { 
       depth = depth + 1; 
       current.Right = new BNode(data); 
       current.Right.Parent = current; 
       current.Right.Depth = depth; 
       break; 
      } 
     } 
    } 

답변

0

미안하지만, 나는 내 실수를 찾아 냈습니다.

대신

current.Depth = depth; 

의 그것이어야한다

current.Left.Depth = depth; 
관련 문제