You are reading a single comment by @frankenbike and its replies.
Click here to read the full conversation.
-
That's true, but think about when you would naturally process an entry in the stack:
lifo = [n] seen = {n} while lifo: node = lifo.pop() yield node # <- parent appears before child in iteration for c in node.children: if c not in seen: seen.add(c) lifo.append(c)If you care about a strict left-to-right order of children in a tree-like structure you would walk over
reversed(node.children).
frankenbike
Oh yeah, sorry. Here’s a reasonably idiomatic child before parent traversal.
https://github.com/firedrakeproject/tsfc/blob/c9fc62334de20229540be7dc57f6ed91ec26ae3f/gem/node.py#L123