-1

i have a problem. I have a element and another list of elements, which are connected to the first one in some way. i want to check, which elements of the list are children of the first. Save the result in an array. Then i will pick the first child and look for his children. save that and so on and on and on. The problem is, i dont know the exact number of relations. so i could have many many loops and searches. do i need to program every single loop and array (to save to) or is there a better way?

EDIT: Iam talking about DB tables. I have two tables. I want to check the children (data in table2) for every element in table1. So i start with a Loop in table1. Elements in Table 1 and table2 are connected with coordinates xy. so iam searching for all elements in table2 where table1_element1.xy == table2.xy. As result there could be n children. Now i want to save them and start for those children a new loop to find for every child his children based on the coordinates. save that new result an so on and on. More clear?

Thank you.

  • 1
    your input is not clear, your output is not clear. replace 'in some way' with a description of your input data ... – opi Jul 31 '13 at 07:31
  • You tagged this for Java, C++, and PL/SQL. I suspect that you are not really using three procedural languages to solve this problem. Please edit the post, improve it as requested by @opi, and remove the tags which don't apply. Thanks. – Bob Jarvis - Слава Україні Jul 31 '13 at 11:16
  • You potentially can do this with SQL. Check out "Hierarchical Queries" - usually the data is in one table with a parent column. But maybe you can make a view on your two tables and then use HQ to fetch all at once. – opi Jul 31 '13 at 12:42

2 Answers2

1

this problem can be easily solved with the help of recursion.

In this case your termination condition will be when any node don't have any relation in the list or when you reach the end of the list where you are storing the children.

Thanks

saurav
  • 3,424
  • 1
  • 22
  • 33
  • ok. for someone who is not used to program. what is recursion and how does it work. – Sebastian Bruno L Jul 31 '13 at 07:36
  • 1
    In kind of pseudo-code, recusion may look like this: `void doStuff(T source){ T childs[] = source.getChilds(); for(child : childs){ doStuff(child); }}` In fact you just use the same code until you run out of child (going down all the tree). http://en.wikipedia.org/wiki/Recursion – orion78fr Jul 31 '13 at 08:04
0

While the question is a little vague, rather than programming every individual loop, a more elegant way of writing this would be through recursion. You would set your break condition to be when you find a child (if I'm understanding the problem correctly) with no relations.

As for the storage issue, it sounds like you may want to use some form of a Topology data structure where each node would hold a list of all nodes connected to it through your relation.

Hope this helps.

Aurora
  • 11
  • 2
  • could you please explain my how recursion works? – Sebastian Bruno L Jul 31 '13 at 07:55
  • Rather than duplicating an answer, I will point you to an [answer](http://stackoverflow.com/questions/1949454/understanding-basic-recursion?rq=1) to this that already exists and is probably better than how I'd explain it. Any other questions, let me know. – Aurora Jul 31 '13 at 08:45