【发布时间】:2015-10-29 04:53:18
【问题描述】:
我正在尝试获取表示二叉搜索树的列表并按顺序输出列表元素,因此(displayBST '(10(5(3(2()())())())())) -> (2 3 5 10)。我似乎能得到的只是看起来像(((2 3) 5) 10) 的列表,我不确定如何使所有数字成为基本元素。
(let((SUMS))
(defun displayBST(elements)
;IF NO ELEMENTS return SUMS
(cond((null elements)
nil)
;if both branches null return first element
((and(null (second elements))(null (third elements)))
(print (first elements))
(first elements))
;if left branch not null
((not(null (second elements)))
;if right branch null
(cond((null (third elements))
;set SUMS to (left branch) and first element
(setf SUMS (list (displayBST(second elements)) (first elements))))
;else set SUMS to (left branch) and first element and (right branch)
(t(SETF sums (append (displayBST(second elements))(first elements)(displayBST(third elements)))))))
;if left branch null and right not null
((not (null(third elements)))
;set SUMS to first element and (right branch)
(setf SUMS (list (first elements) (displayBST(third elements))))))))
【问题讨论】:
-
您可以在结果列表中使用
flatten(((2 3) 5) 10)
标签: recursion lisp common-lisp