How to get all children from a given parent node?

I have a list of parent / child IDs and would like to get all IDs for this parent ID. No null parents (top-level identifiers are not displayed as child identifiers).

Currently, parent / child ids are written to the KeyValuePair list in the list, however this can easily be changed to a different data structure if this is better:

List<KeyValuePair<int, int>> groups = new List<KeyValuePair<int, int>>(); groups.Add(new KeyValuePair<int,int>(parentID, childID)); 

For example, here are examples of parent / children. Children of motherhood 27 will be 5944, 2065, 2066, 2067, 6248, 6249, 6250.

 Parent Child 27 1888 1888 5943 1888 5944 5943 2064 5943 2065 5943 2066 5943 2067 2064 6248 2064 6249 2064 6250 

Any help would be greatly appreciated!

+4
source share
2 answers

Why don't you change the type of Dictionary<int, List<int>> , where is the parent key, and the value (list int) are children?

Then you will return the list of children using:

  private List<int> GetAllChildren(int parent) { List<int> children = new List<int>(); PopulateChildren(parent, children); return children; } private void PopulateChildren(int parent, List<int> children) { List<int> myChildren; if (myitems.TryGetValue(parent, out myChildren)) { children.AddRange(myChildren); foreach (int child in myChildren) { PopulateChildren(child, children); } } } 

You will need to take into account the impact of performance, as this will speed up reading and slowing down the recording (a huge amount of time that no one would even notice).

You will also need to check if this list is in the dictionary using myitems.TryGet(...) , and if not, you need to create it, but it is o (1), so it is almost instantaneous.

 private static void AddEntry(int parent, int child) { List<int> children; if (!myitems.TryGetValue(parent, out children)) { children = new List<int>(); myitems[parent] = children; } children.Add(child); } 
+5
source

Simple Just think that you have a list in the next array

  List<KeyValuePair<int, int>> groups = new List<KeyValuePair<int, int>>(); groups.Add(new KeyValuePair<int, int>(27, 1888)); groups.Add(new KeyValuePair<int, int>(1888, 5943)); groups.Add(new KeyValuePair<int, int>(1888, 5944)); groups.Add(new KeyValuePair<int, int>(5943, 2064)); groups.Add(new KeyValuePair<int, int>(5943, 2065)); groups.Add(new KeyValuePair<int, int>(5943, 2066)); groups.Add(new KeyValuePair<int, int>(5943, 2067)); groups.Add(new KeyValuePair<int, int>(2064, 6248)); groups.Add(new KeyValuePair<int, int>(2064, 6249)); groups.Add(new KeyValuePair<int, int>(2064, 6250)); groups.Add(new KeyValuePair<int, int>(2000, 1000)); // Pass the 1st parameter as the parent to get all children List<int> childs = GetAllChild(27, groups); 

To obtain dynamic data, you need to use the "Recursive Function" function. Just call the following method to get all the children of the parent

 public List<int> GetAllChild(int id,List<KeyValuePair<int, int>> newLst) { List<int> list = new List<int>(); for (int i = 0; i < newLst.Count; i++) { if (Convert.ToInt32(newLst[i].Key) == id) { if (!list.Contains(Convert.ToInt32(newLst[i].Value))) { list.Add(Convert.ToInt32(newLst[i].Value)); List<int> l = GetAllChild(newLst[i].Value, newLst); list.AddRange(l); } } } return list; } 
0
source

All Articles