August 19, 2024

P54 - Check whether a given expression represents a binary tree

Write a function istree which returns true if and only if its argument is a list representing a binary tree.

Example:* (istree '(a (b nil nil) nil))T* (istree '(a (b nil nil)))NIL

lisp

;;; P54A: Write a predicate istree which reurns t if and only if its argument
;;;      is an S-expression representing a binary tree.
;;;

(defun istree (tree)
  (if (null tree)
      t
    (and (listp tree)
	 (= 3 (length tree))
	 (istree (second tree))
	 (istree (third tree))
	 )
    )
  )
Be first to comment
Leave a reply