【发布时间】:2013-11-23 04:58:14
【问题描述】:
我正在尝试以后序方式遍历 prolog 中的一般树。我发现了很多二叉树后序遍历,但无法将它们用于我的目的。我编写了一个程序,但它只以与输入方式相反的方式打印我的树,即用于输入
?-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([]).
我没有获得后序遍历的方法
非常感谢任何帮助
【问题讨论】:
-
好吧,我确实看到了那个问题,但是在那里他们没有在给定的解决方案中使用 univ 谓词。我想用大学来做。