我试图返回按顺序访问的树(不一定是二叉树)的节点列表。
树表示为带有子列表的列表,例如: (a (b) (c (d) (e))), b - 左子树, (c (d) (e)) - 右子树, a -根。结果应该是:b,a,d,c,e
这是我的代码,但我似乎总是收到“堆栈溢出”错误。有人可以帮帮我吗?
;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))
)
)
)