我正在Lisp中开发k-d树。我正在写一个函数,允许我在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
。为什么这个函数不返回节点?
我更喜欢第一个解决方案:)。 – JNevens