1

I am iterating a tree using Breadth first search and I want to check whenever the iteration goes to a lower level in the tree each time to display a message and that using a pseudo code. I don't want to use the classic while-for loops but instead I want one for loop that is commented with (traverse BFS) like I do below it:

  PreviousNodeLevel ← null
  for node in tree do //(traverse BFS)
    if node ∈ children(PreviousNodeLevel) then // if the current node is a child of any element of PreviousNodeLevel
        display message // next level of nodes reached 
    else if PreviousNodeLevel != null then 
        PreviousNodeLevel ← null // next the next level case
    end if
    PreviousNodeLevel.insert(node)
  end for

Any idea if this pseudo-code is correct to adopt ?

2
  • why do you need the for loop - is it to parallelize or do you want to count the level ? Commented May 9, 2023 at 21:11
  • 1
    Looks fine to me. Is there a problem? It is just a pity that you have to store again the nodes that are in a level, while the hidden BFS algorithm is already doing that. Commented May 10, 2023 at 14:54

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.