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?