3

I have a tree structure where each node can have essentially unlimited children, it's modelling the comments for a blog.

I'm trying to figure out, given the ID of a specific comment, at what depth/level that comment lies in the tree.

I was following this guide that explains it for binary trees, but when adapting it into non-binary trees I'm having some trouble.

Here's my attempt so far (in Swift):

func commentLevelRecursive(comment: Comment, commentID: String, currentLevel: Int) -> Int {
    if comment.identifier == commentID {
        return currentLevel
    }

    var newLevel = currentLevel

    for reply in comment.replies {
        newLevel = commentLevelRecursive(reply, commentID: commentID, currentLevel: currentLevel + 1)
    }

    return newLevel
}

But it always seemingly returns 1. I think this is because newLevel always gets increased from 0 to 1 then returns.

Could anyone give me some insight into where I'm going wrong?

Doug Smith
  • 29,668
  • 57
  • 204
  • 388
  • 1
    newLevel = max(newLevel, commentLevelRecursive(reply, commentID: commentID, currentLevel: currentLevel + 1)) – Mark Dec 10 '14 at 03:52
  • @IonescuRobert Why use max? There's only one occurrence of the node with this ID. – Doug Smith Dec 10 '14 at 20:00
  • Yes but you are going trough multiple replies, so you want the maximum depth from all of them. "newLevel" in this case will be for each level the maximum depth for it's kids. So you will have on the first node the maximum depth if you follow that rule. – Mark Dec 10 '14 at 20:57
  • @IonescuRobert How would this factor into the code above? If I replace the `newLevel` set within the for loop it doesn't output the right value. – Doug Smith Dec 10 '14 at 20:58
  • Ohhh sorry, I read wrong.. You have to check every time if you found the node. Make your function do this: return 0 if you couldn't find it or return the level if you find it. In for for loop you ask every time if commentLevelRecursive(reply, commentID: commentID, currentLevel: currentLevel + 1) is 0 or if it's not, if it's 0 you continue the for loop, if not you return that value because you found your answer. After the for loop you always return 0. If you are there that's because you couldn't find the commendId in the food loop so that's why you return 0. Sorry of misleading you:( – Mark Dec 10 '14 at 21:10
  • @IonescuRobert It's okay. :) Could you show me the pseudo code for that? I'm a little confused. – Doug Smith Dec 10 '14 at 23:04
  • for reply in comment.replies { value = commentLevelRecursive(reply, commentID: commentID, currentLevel: currentLevel + 1) if value != 0 return value } return 0 – Mark Dec 11 '14 at 00:02

2 Answers2

0

Binary tree is a special case where each node has only two children. The method used to find node level can be extended to a common tree.

The idea is the same:

level(P) = max(level(C) for each child node C of P)+1
squid
  • 2,597
  • 1
  • 23
  • 19
-1
int MaxDepth(int x,int parent){
    int mx = 0;
    for(int  i = 0;i < graph[x].size();i++)
        if(graph[x][i]!=parent){
            mx = max(MaxDepth(graph[x][i],x),mx);
        }
        return mx + 1;
}