私はLispでk-dツリーを開発しています。私は、k-dツリーのノードを検索できるようにする関数を書いています。 '((2 3) (5 4) (9 6) (4 7) (8 1) (7 2))
:私は、以下のデータをツリーを構築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)
を見つけるためにこの関数を使用しています。 format
文で
(find-node kdt '(2 3))
、私はこの出力を得る:だから
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