-1

I tried my best to find specific node in first child node sibling structure but I am not able to do this any one who can help or give me method to find node method.

I can write add method but I'm not able to write method.

My code:

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

Assume that I have created tree in following structure every one has unique NodeIndex so how can I find this node?

Thanks in advance.

Here's my structure:

Root
 |
 p1 ----- p2 ----- p4 ----- p6  
 |        |         |       |
 c1       p3       c4       p7
          |                 |
          c2 - c3           c5
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Wajihurrehman
  • 567
  • 3
  • 15
  • 29

1 Answers1

0

Something like this?

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