我试图以后序方式遍历prolog中的普通树。我发现很多二叉树后序遍历,但无法将它们用于我的目的。我写了一个程序,但它只能打印我的树在如何进入它的相反的方式,即输入
Prolog Postorder在使用univ的普通树中遍历
?-postorder(a(b,c,d(e,f,g))).
->g f e d c b a true (is what I get)
->b c e f g d a true (what i want to get)
这里是代码我已成功地编写到现在
postorder([]).
postorder(List):- List =..X , myfun(X).
myfun([A|B]):- atom(A), myfun(B),write(A),write(' ').
myfun([A|B]):- postorder(A),myfun(B).
myfun([]).
我没有得到的后序遍历的方式
任何帮助深表感谢
可能重复[PROLOG(如何后序多路树)(http://stackoverflow.com/questions/20035673/prolog-how-to-postorder-a-multiway-tree) – Shevliaskovic
嗯,我确实看到了这个问题,但是在那里他们并没有在给定的解决方案中使用univ谓词。我想用univ来做。 –