2014-09-16 2 views
-1

첫 번째 자식 노드 형제 구조에서 특정 노드를 찾으려고 최선을 다했지만이 방법을 사용하여 노드 메서드를 찾는 방법을 찾거나 도와 줄 수있는 사람이 없습니다.첫 번째 자식 노드 형제 구조의 고유 값으로 노드 찾기

메소드를 추가 할 수는 있지만 메소드를 작성할 수는 없습니다.

내 코드 :

public int Weight { get; private set; } 
public string Data { get; private set; } 
public int NodeIndex { get; private set; } 
public TreeNode FirstChild { get; private set; } 
public TreeNode NextSibling { get; private set; } 
public TreeNode ParentNode { get; private set; } 

TreeNode Root, Head; 

public TreeNode() 
{ 
} 

public TreeNode(int Weight, TreeNode firstChild, TreeNode nextSibling, string Data, int NodeIndex) 
{ 
      this.Durability = Durability; 
      this.Weight = Weight; 
      this.Data = Data; 
      this.FirstChild = firstChild; 
      this.NextSibling = nextSibling; 
      this.NodeIndex = NodeIndex; 
} 

내가 모두 고유 NodeIndex을 가지고 구조를 다음 트리를 만들었다 고 가정 그래서 어떻게이 노드를 찾을 수 있습니까?

미리 감사드립니다. 이 같은

Root 
| 
p1 ----- p2 ----- p4 ----- p6 
|  |   |  | 
c1  p3  c4  p7 
      |     | 
      c2 - c3   c5 

답변

0

뭔가 :

여기 내 구조인가?

public TreeNode NodeByIndex(TreeNode root, int NodeIndex) 
{ 
    if (root.NodeIndex == NodeIndex) 
    return root; 

    if (root.FirstChild != nil) 
    { 
    TreeNode c = NodeByIndex(root.FirstChild, NodeIndex); 
    if (c != nil) 
     return c; 
    } 

    if (root.NextSibling != nil) 
    { 
    TreeNode c = NodeByIndex(root.NextSibling, NodeIndex); 
    if (c != nil) 
     return c; 
    } 

    return null; 
} 
관련 문제