搜索树并在节点出现多次时返回true

时间:2012-11-28 17:31:48

标签: tree lisp

我有一份家庭作业,我需要做以下事情:

  

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

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

  (defun in (tree)
    (cond ((null tree)
           t)
          ((eq (first tree) (second tree))
           nil)
          ((listp (first tree))
           (or ((first tree) in  (second tree))
               ((first tree) in  (rest tree))))
          (t
           ((first tree) in (rest tree)))))

这是我的第二次尝试,但也无效:

(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)))))

能否请您提供一些见解?

2 个答案:

答案 0 :(得分:1)

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

(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)

(in A B)

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

答案 1 :(得分:0)

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

(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)))))