Tree 在树中搜索,如果节点多次出现,则返回true

Tree 在树中搜索,如果节点多次出现,则返回true,tree,lisp,Tree,Lisp,我有一个家庭作业,我需要做以下工作: 函数,该函数将树作为其参数,并返回一个nil/non-nil值,该值指示树是否只包含唯一的节点(即:树中没有重复的节点) 到目前为止,我已经编写了以下代码。我是一个口齿不清的新手,我需要完成我的家庭作业。 这是我试图实现的第一个解决方案。但当我编译它时,它给了我以下错误:函数位置必须包含符号或lambda表达式:(第一棵树) 这是我的第二次尝试,但也不起作用: (defun flatten (structure) (cond ((null structu

我有一个家庭作业,我需要做以下工作:

函数,该函数将树作为其参数,并返回一个nil/non-nil值,该值指示树是否只包含唯一的节点(即:树中没有重复的节点)

到目前为止,我已经编写了以下代码。我是一个口齿不清的新手,我需要完成我的家庭作业。 这是我试图实现的第一个解决方案。但当我编译它时,它给了我以下错误:函数位置必须包含符号或lambda表达式:(第一棵树)

这是我的第二次尝试,但也不起作用:

(defun flatten (structure)
  (cond ((null structure) nil)
        ((atom structure) `(,structure))
        (t (mapcan #'flatten structure))))

(defun uniNodes (inList &optional (out t) (test 0))
  (cond ((null inList)
         out)
        ((zerop test)
         (uniNodes (flatten(cons (first inList) (rest inList))) out (+ test 1)))
        ((eq t (first out))
         (uniNodes (rest inList) (compare1 (first inList) (rest inList) (first out)) test))
        ((eq nil (first out))
         out)))

(defun compare1 (a list &optional (out t))
  (cond ((null list)
         out)
        ((equal a (first list))
         nil)
        (t
         (compare1 a (rest list) (first out)))))

你能给我提供一些见解吗?

如果树不是很大,那么这种递归方法可以:

(defun tree-contains-duplicates-p (tree)
  (labels ((%tree-contains-duplicates-p (node table)
             (cond
               ((null node) t)
               ((consp node)
                ;; lists can't be same unless they have
                ;; same atoms, but having two `nil' lists
                ;; is a trivial case, which you want to ignore
                ;; probably
                (and (%tree-contains-duplicates-p (car node) table)
                     (%tree-contains-duplicates-p (cdr node) table)))
               (t (unless (gethash node table)
                    (setf (gethash node table) t))))))
    (not (%tree-contains-duplicates-p tree (make-hash-table)))))
否则,您希望将其展开成一个循环,在该循环中记录遍历树所采取的最后一个操作,并在击中叶子后从那里恢复

看起来这样应该行得通:

(defun tree-contains-duplicates-p (tree)
  (loop with leaf = tree
     with stack = nil
     with table = (make-hash-table)
     while (or leaf stack)
     do (cond
          ((null leaf)
           (setq leaf (car stack) stack (cdr stack)))
          ((consp (car leaf))
           (when (cdr leaf)
             (setq stack (cons (cdr leaf) stack)))
           (setq leaf (car leaf)))
          (t (setq leaf (cdr leaf))))
       (when leaf
         (if (gethash (car leaf) table)
             (return t)
             (setf (gethash (car leaf) table) t)))))

我建议递归遍历树,收集表中的节点

(defun find-dupes (tree)
  (let ((table (make-hash-table)))
    (labels ((check-node (node)
               (when (consp node)
                 (when (gethash node table)
                   (return-from find-dupes node)) ; return the dupe
                 (setf (gethash node table) node) ; memorize the node
                 (check-node (car node))
                 (check-node (cdr node)))))
      (check-node tree))))
您需要找出如何更改上述代码以适应您的问题

至于你的错误

Function position must contain a symbol or lambda expression: (FIRST TREE)
意味着您需要修复函数调用

(A in B)

您没有解释您的第二次尝试有什么问题,尽管它在参数大小上似乎是二次的

(A in B)
(in A B)