AnsweredAssumed Answered

Get all Parents recursively

Question asked by IPCOS_DV on Jun 22, 2016
Latest reply on Jun 22, 2016 by ernstamort

Hi all,

 

I have a list of parent elements (at different levels). Starting from a child element and moving upwards, I would like to find the first match.

Let me illustrate it with an example. Let's assume I have the following element structure:

sample_tree.png

My list of parent elements is {Root, 1C, 2A, 3D, 3E}. My algorithm should have the following results:

 

InputOutput
3A, 3B, 2A2A
3D3D
4A, 4B, 4C, 3E3E
2D, 2E, 1C1C
2B, 2C, 1A, 1B, RootRoot

 

My current implementation does some looping from child to root

 

var element = childElement;
for (int levelIteration = 0; levelIteration < 25; levelIteration++)

{

    if (parentElements.Contains(element))

    {

          return element;

    }

    element = element.Parent;

    if (element == null)

    {

          return null;

    }

}

return null;

 

 

 

Is there a more efficient way to do it?

Outcomes