'I get an error saying (*** - Lisp stack overflow. RESET) when I try Sorting a list using Binary Search Tree Traversal In order in Common Lisp

I get the error mentioned in the title when I try to run my code with an example (included in the code provided below). I can not figure out where the issue is. Help is highly appreciated

Code:

(defun l-tree(left)
    (cond
        ((or (null left) (not (listp left)))nil)
        (t (car (cdr left)))))

(defun r-tree(right)
    (cond
        ((or (null right)(not (listp right)))nil)
        (t (car ( cdr (cdr right))))))

(defun in-order(tree)
    (append
        (in-order (l-tree tree))
        (list (car tree))
        (in-order (r-tree tree))
     )
 )
        
(defparameter *tree2* '(40 (30 (25 () ()) (35 a() ())) (60 (50 () ()) ())))
(print (in-order *tree2*))


Solution 1:[1]

It would help if you wrote when the program must stop the recursion, which in this program is (unless (null tree) ); otherwise, the program keeps running until it reaches the memory limit.

(defun l-tree (left)
  (cond
   ((or (null left) (not (listp left))) nil)
   (t (car (cdr left)))))

(defun r-tree (right)
  (cond
   ((or (null right)(not (listp right))) nil)
   (t (car ( cdr (cdr right))))))

(defun in-order (tree)
  (unless (null tree) ;; Stop condtion
    (append (in-order (l-tree tree))
        (list (car tree))
        (in-order (r-tree tree)))))
        
(defparameter *tree2* '(40
            (30
             (25
              () ())
             (35
              () ()))
            (60
             (50
              () ())
             ())))

(print (in-order *tree2*))

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Vee Satayamas