【发布时间】:2017-07-06 23:47:00
【问题描述】:
我正在用 Lisp 开发一个 k-d 树。我正在编写一个允许我在 k-d 树中搜索节点的函数。该函数定义如下:
(defmethod find-node ((kdt kdtree) target &key (key #'value) (test #'equal))
(unless (null (root kdt))
(find-node (root kdt) target :key key :test test)))
(defmethod find-node ((node kdnode) target &key (key #'value) (test #'equal))
(format t "Testing node ~a~%" (value node))
(format t "Result is ~a~%" (funcall test (funcall key node) target))
(if (funcall test (funcall key node) target)
node
(progn
(unless (null (left node))
(find-node (left node) target :key key :test test))
(unless (null (right node))
(find-node (right node) target :key key :test test)))))
我用以下数据构建了一棵树:'((2 3) (5 4) (9 6) (4 7) (8 1) (7 2))。所以现在,我正在使用这个函数来查找节点'(2 3)。
(find-node kdt '(2 3))
使用format 语句,我得到以下输出:
Testing node (7 2)
Result is NIL
Testing node (5 4)
Result is NIL
Testing node (2 3)
Result is T
Testing node (4 7)
Result is NIL
Testing node (9 6)
Result is NIL
Testing node (8 1)
Result is NIL
NIL
因此,如您所见,由于测试结果为T,因此找到了该节点,但是继续搜索,结果为NIL。为什么这个函数不返回节点?
【问题讨论】:
标签: search tree lisp common-lisp