Pregunta

I am working on CFTree. I need to get all siblings on same depth for example in following figure I need an array of all 8 CFTreeRef on 3rd depth. How can I get that?

enter image description here

¿Fue útil?

Solución

Following your depth numbering - i.e. starting with root at 1, not 0:

-(void)allNodesFromTree:(CFTreeRef)node currentDepth:(int)currDepth atDepth:(int)depth nodesFound:(NSMutableArray*)nodes
{
    bool atTargetDepth = depth -1 == currDepth;

    CFTreeRef curChild = CFTreeGetFirstChild(node);
    for (; curChild; curChild = CFTreeGetNextSibling(curChild)) {

        if(atTargetDepth) //stop recursion if target depth reached
        {   
            [nodes addObject:(__bridge id)(curChild)];
        }
        else [self allNodesFromTree:curChild currentDepth:currDepth+1 atDepth:depth nodesFound:nodes];
    }
}

To make things a bit cleaner, you might wrap it in a helper function:

-(NSMutableArray*)allNodesFromTree:(CFTreeRef)node atDepth:(int)depth 
{   
   NSMutableArray *nodesArray = [[NSMutableArray alloc]init];
   [self allNodesFromTree:node currentDepth:0 atDepth:depth nodesFound:nodesArray];
   return nodesArray;
}

Then you can just call

NSMutableArray *nodes = [self allNodesFromTree:root atDepth:3];
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top