我想返回一个树(不一定是二叉树)的节点列表访问inorder。例如:(a(b)(c(d)(e))),b - 左子树,(c(d)(e)) - 右边-subtree,一个 - 根。 结果应该是:b,a,d,c,e遍历树LISP
这是我的代码,但我总是看到“堆栈溢出”错误。有人可以帮帮我吗?
;return left-subtree
(defun left-tree(tree)
(cond
((null tree) NIL)
((not (listp tree)) NIL)
(t (car (cdr tree)))
)
)
;return right-tree
(defun right-tree(tree)
(cond
((null tree) NIL)
((not (listp tree)) NIL)
(t (cdr (cdr tree)))
)
)
;perform inorder
(defun inorder(tree)
(if (not (list-length tree)) 0
(append
(inorder (left-tree tree))
(list (car tree))
(inorder (right-tree tree))
)
)
)
什么是'(NOT(名单长树))'的目的是什么? “不”的数字总是假的。 –
树遍历的顺序通常是前序(root,left,right);后序(左,右,根);并按顺序(左,右,右)。我知道你张贴了一个输出的例子,但当你有两个以上的孩子时,“顺序”意味着什么?例如,如果你有树(a(b)(c)(d)),那么按顺序遍历应该是什么? a b a c a d? –
另外一个问题是,当你想要一个扁平列表时,你在'inorder'的每个评估中创建一个新列表。 [Common Lisp:符号计算的简洁介绍](http://www.cs.cmu.edu/~dst/LispBook/)中有关'car/cdr递归'的部分可能对您有所帮助。 – Pascal