Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/performance/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Recursion lisp中的树遍历_Recursion_Lisp - Fatal编程技术网

Recursion lisp中的树遍历

Recursion lisp中的树遍历,recursion,lisp,Recursion,Lisp,我试图在lisp中遍历一棵树并打印出所有的父子关系。 这是我的输入:5341G96N80Q7BFCA 我试图让它打印出以下内容: 5>3 5>n 3>4 3>g 3>9 4>1 9>6 n>8 n>q n>7 n>b 8>0 b>f f>c c>a 我目前的代码如下: (defun par-child-print (l) (print l) (cond ((no

我试图在lisp中遍历一棵树并打印出所有的父子关系。 这是我的输入:5341G96N80Q7BFCA 我试图让它打印出以下内容:

5>3

5>n

3>4

3>g

3>9

4>1

9>6

n>8

n>q

n>7

n>b

8>0

b>f

f>c

c>a
我目前的代码如下:

(defun par-child-print (l)
    (print l)
    (cond ((not (null (caadr l))) 
    (print "start")
    (print (car l)) 
    (print ">") 
    (print (caadr l)) 
    (print "end")
    (cond ((not (atom (car l))) (when (not (eq (car l) NIL)) (par-child-print (car l)))));

    (when (not (eq (cdr l) NIL)) (par-child-print (cdr l)))

    )
(t 
)));
问题是,我的输出有时只打印父级,而且它不能通过整个树。有什么想法吗

我也有这个,它贯穿了整棵树,但我甚至没有试图跟踪父母:

(defun atom-print (l)
(print l)
(cond ((atom l) (print l));
(t 
(when (not (eq (car l) NIL)) (atom-print (car l)))
(when (not (eq (cdr l) NIL)) (atom-print (cdr l)))


)));

树中的每个列表由两部分组成,一个名称和一个子列表。这些与列表中的CAR和CDR相同,但出于语义原因,您可以首先为它们定义别名:

(defun name (tree) (car tree))
(defun children (tree) (cdr tree))
这些抽象描述了树是如何实现的。然后,给定一棵树,您需要做两件事:

为每个孩子打印一行,其中包含父母姓名和孩子姓名。可以这样做:

(dolist (child (children tree))
  (format t "~&~a > ~a" (name tree) (name child)))
(defun print-tree (tree)
  (dolist (child (children tree))
    (format t "~&~a > ~a" (name tree) (name child)))
  (dolist (child (children tree))
    (print-tree child)))

(print-tree '(5 (3 (4 (1)) (g) (9 (6))) (n (8 (0)) (q) (7) (b (f (c (a)))))))
; 5 > 3
; 5 > N
; 3 > 4
; 3 > G
; 3 > 9
; 4 > 1
; 9 > 6
; N > 8
; N > Q
; N > 7
; N > B
; 8 > 0
; B > F
; F > C
; C > A
以相同的方式打印每个子项。这是通过递归调用函数来实现的:

(dolist (child (children tree))
  (print-tree child))
因此,整个函数将如下所示:

(dolist (child (children tree))
  (format t "~&~a > ~a" (name tree) (name child)))
(defun print-tree (tree)
  (dolist (child (children tree))
    (format t "~&~a > ~a" (name tree) (name child)))
  (dolist (child (children tree))
    (print-tree child)))

(print-tree '(5 (3 (4 (1)) (g) (9 (6))) (n (8 (0)) (q) (7) (b (f (c (a)))))))
; 5 > 3
; 5 > N
; 3 > 4
; 3 > G
; 3 > 9
; 4 > 1
; 9 > 6
; N > 8
; N > Q
; N > 7
; N > B
; 8 > 0
; B > F
; F > C
; C > A

树中的每个列表由两部分组成,一个名称和一个子列表。这些与列表中的CAR和CDR相同,但出于语义原因,您可以首先为它们定义别名:

(defun name (tree) (car tree))
(defun children (tree) (cdr tree))
这些抽象描述了树是如何实现的。然后,给定一棵树,您需要做两件事:

为每个孩子打印一行,其中包含父母姓名和孩子姓名。可以这样做:

(dolist (child (children tree))
  (format t "~&~a > ~a" (name tree) (name child)))
(defun print-tree (tree)
  (dolist (child (children tree))
    (format t "~&~a > ~a" (name tree) (name child)))
  (dolist (child (children tree))
    (print-tree child)))

(print-tree '(5 (3 (4 (1)) (g) (9 (6))) (n (8 (0)) (q) (7) (b (f (c (a)))))))
; 5 > 3
; 5 > N
; 3 > 4
; 3 > G
; 3 > 9
; 4 > 1
; 9 > 6
; N > 8
; N > Q
; N > 7
; N > B
; 8 > 0
; B > F
; F > C
; C > A
以相同的方式打印每个子项。这是通过递归调用函数来实现的:

(dolist (child (children tree))
  (print-tree child))
因此,整个函数将如下所示:

(dolist (child (children tree))
  (format t "~&~a > ~a" (name tree) (name child)))
(defun print-tree (tree)
  (dolist (child (children tree))
    (format t "~&~a > ~a" (name tree) (name child)))
  (dolist (child (children tree))
    (print-tree child)))

(print-tree '(5 (3 (4 (1)) (g) (9 (6))) (n (8 (0)) (q) (7) (b (f (c (a)))))))
; 5 > 3
; 5 > N
; 3 > 4
; 3 > G
; 3 > 9
; 4 > 1
; 9 > 6
; N > 8
; N > Q
; N > 7
; N > B
; 8 > 0
; B > F
; F > C
; C > A
有一个问题:它在给定的输入上工作,但这只是因为每个孩子都有自己的孩子。这个答案的一个变体在示例输入上具有相同的行为,但在其他树上也适用

defun打印树 dolist子cdr树 格式t~&~a>~如果是consp child,则为汽车树 车童 小孩 dolist子cdr树 如果consp儿童 打印树子对象 打印树A B C 1 2 3 4 A>B B>C B>4 C>1 C>2 C>3 有一个问题:它在给定的输入上工作,但这只是因为每个孩子都有自己的孩子。这个答案的一个变体在示例输入上具有相同的行为,但在其他树上也适用

defun打印树 dolist子cdr树 格式t~&~a>~如果是consp child,则为汽车树 车童 小孩 dolist子cdr树 如果consp儿童 打印树子对象 打印树A B C 1 2 3 4 A>B B>C B>4 C>1 C>2 C>3