-1
我试图实现对BST二叉搜索树Clojure中
(defn size
"Return the number of nodes in a BST."
[bst]
(cond (nil? bst) 0
:else (+ 1 (+ size(:left bst) size(:right bst))))
)
这看起来正确的一个附加功能?
我试图实现对BST二叉搜索树Clojure中
(defn size
"Return the number of nodes in a BST."
[bst]
(cond (nil? bst) 0
:else (+ 1 (+ size(:left bst) size(:right bst))))
)
这看起来正确的一个附加功能?
的东西夫妇错在这里。你还是在“标准”的方式调用函数。 size(:left bst)
将引发异常,则需要(size (:left bst))
(paranthesis周围是重要的,以及函数的名称后面有一个空格)。
第二件事 - 无需使用cond
当你有一个案例和:else
。仅在以下情况下使用:
(if (nil? bst)
0
(+ 1 (+ (size (:left bst)) (size (:right bst)))))
这是一个有效的'size'(不是'add')函数,只要':left'和':right'存在非零节点。 – Thumbnail