Ungewöhnlicher Stapelüberlauf beim Einfügen von Knoten in einen Binärbaum

CLISP Version: 2.49

Blattknote

(value (NIL) (NIL))

Non-Leaf Node

(value (value (NIL) (NIL)) (NIL))

Code ("Format" nur für Debug)

; (nil) means NULL
(defun binary-insert (root obj <) 
(if (null (cdr root))
    (progn 
        (format t "In Null [~A] => " root) 
        (setf (car root) obj) 
        (format t "mid [~A] => " root) 
        (setf (cdr root) '((nil) (nil))) 
        (format t "[~A]~%" root))
    (if (funcall < obj (car root))
        (progn 
            (format t "In Left [~A] => " root) 
            (binary-insert (nth 1 root) obj <) 
            (format t "[~A]~%" root)) ; Left
        (progn 
            (format t "In Right [~A] => " root) 
            (binary-insert (nth 2 root) obj <) 
            (format t "[~A]~%" root)) ; Right
        )
    )
)

Prüfun

[1]> (load "binary_tree.lisp")
;; Loading file binary_tree.lisp ...
;; Loaded file binary_tree.lisp
T
[2]> (setf *glb-rt* '(NIL))
(NIL)
[3]> (binary-insert *glb-rt* 10 #'<)
In Null [(NIL)] => mid [(10)] => [(10 (NIL) (NIL))]
NIL
[4]> *glb-rt*
(10 (NIL) (NIL))
[5]> (binary-insert *glb-rt* 5 #'<)
In Left [(10 (NIL) (NIL))] => In Null [(NIL)] => mid [(5)] => [
*** - Lisp stack overflow. RESET

Es scheint, dass das Programm nach der Ausführung von @ abgestorben i

(setf (cdr root) '((NIL) (NIL)))

Vielen Dank...

[Aktualisieren

Before (setf (cdr root) '((NIL) (NIL))) ist die "Wurzel" (5)

Ein weiterer Test

[6]> (setf glb-ls '(5))
(5)
[7]> (setf (cdr glb-ls) '((NIL) (NIL)))
((NIL) (NIL))
[8]> glb-ls
(5 (NIL) (NIL))

Antworten auf die Frage(2)

Ihre Antwort auf die Frage