I would like to compute list being bfs order on binary tree. Moreover, it should work in second side - for list it find tree.
Can you give me some hint, so far I have used something like that, of course it doesn't work...
bfs(nil) --> [].
bfs(t(X, L, R)), [X] --> bfs(L), bfs(R).
Looks like @repeat came up with a nice DCG solution. I had, in the meantime, come up with a "traditional" queue-based solution that is non-DCG:
This follows the methodology shown in the links I provided in my comments. It also follows a strict left-to-right traversal ordering which, I believe, is conventional in binary tree traversals. It's a little simpler than those in the links as those are for more general graphs rather than binary trees. The description of what's happening above is simple:
(a) Dequeue and accept the current node value
(b) Enqueue the left and right nodes
The above code yields:
And:
Here's a revised version that uses a difference list rather than
append/3
.