3

I am doing a graduation project on Word Sense Disambiguation (WSD)

I have one problem in my code

I could not develop a code which can do the following: I want to store string values in this structure

                          root
                        /     \
                       A       B
                    / / \    / / \ 
                    C D E    C D E

and then I want to traverse every path in this structure and store it in a jagged array (Array of Array) and each array inside this jagged array should contain the traversed node's values like this (ignoring the root node)

A C
A D 
A E
B C
B D 
B E

Any idea on how to do that ?

Here is what exactly I need... This array would be given

 string[][] English_Senses = new string[][] { new string[] { "hit", "multiply" }, new string[] { "man", "leg" } };

I need a code that could fill up the following jagged array with the following values

string[,] Features = new string{{"hit","man"},{"hit","leg"},{"mutiply","man"},{"multiply","leg"}};

I hope someone can help me with this.

Note the size of the English_Senses array is not known until run time and it can be of any size and can be consist of any No. of arrays and each array inside this jagged array can have any No. of element..

2
  • 2
    Which part of this, specifically, are you having trouble with? Commented Feb 26, 2012 at 18:15
  • I am having trouble in both storing the data in a non binary tree structure and traversing all possible paths Commented Feb 26, 2012 at 20:32

1 Answer 1

3

Lets assume you had a node structure like the following

public class Node { 
  public string Data;
  public List<Node> Children = new List<Node>();    
}

It sounds like you want to create a List<string> which contains all possible traversal paths of the Data elements. If so try the following

public List<string> GetTraversal(Node root) {
  var list = new List<string>();
  foreach (var child in root.Children()) {
    GetTraversal(child, "", list);
  }
}

private void GetTraversal(Node node, string path, List<string> list) {
  path = path == "" ? node.Data : path + " " + node.Data;
  if (node.Children.Count == 0) {
    list.Add(path);
  } else {
    foreach(var child in node.Children) {
      GetTraversal(child, path, list);
    }
  }
}
Sign up to request clarification or add additional context in comments.

3 Comments

I tried to compile your code but I got this error Error"Inconsistent accessibility: parameter type 'Tree.Form1.Node' is less accessible than method 'Tree.Form1.GetTraversal(Tree.Form1.Node)'" Can you help ?? plz I'm really stuck in this particular section of the code.
@Noha make the Node type public
Thank you so much. it works now after I did some small changes to the code. Thanks again

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.