drRacket结构错误

时间:2015-05-16 15:54:28

标签: racket

当我写这些代码时,程序会出错;

"leaf-name: expects a leaf, given empty"

(define-struct leaf (parent children name level-of-vertex))
(define A (make-leaf empty '(B C D) 'A 1))
(define B (make-leaf A '(E F) 'B 2))
(define C (make-leaf A 'G 'C 2))
(define D (make-leaf A '(H J) 'D 2))
(define E (make-leaf B empty 'E 3))
(define F (make-leaf B '(K L) 'F 3))
(define G (make-leaf C empty 'G 3))
(define H (make-leaf D empty 'H 3))
(define J (make-leaf D empty 'J 3))
(define K (make-leaf F empty 'K 4))
(define L (make-leaf F empty 'L 4))
(define binarytree (list A B C D E F G H J K L empty))
(define (findchild child)
  (display (leaf-name (leaf-children child))))
(findchild E)

如何解决此错误?

2 个答案:

答案 0 :(得分:0)

更改

(define binarytree (list A B C D E F G H J K L empty))

(define binarytree (list A B C D E F G H J K L))

完成上述更改后,请注意:

(define E (make-leaf B empty 'E 3))

因此

  

(叶子E)       空

这就是为什么(leaf-name(leaf-children E)))变成(叶子名称为空)并且你得到错误。

答案 1 :(得分:0)

我想 class KeyCanvas extends Canvas { int x=50;int y=50; Image image = null; private Random rnd = new Random(); public KeyCanvas() { for (int I=0; I<=5; I++){ x = rnd.nextInt(146) + 1; y = rnd.nextInt(150) + 1; try { Thread.sleep(1000); } catch (Exception ex) {} repaint(); } } public void paint(Graphics g) { try{image = Image.createImage("/ship01.JPG"); }catch(Exception e){ e.printStackTrace();} g.drawImage(image, x, y, Graphics.HCENTER | Graphics.VCENTER); } protected void keyPressed(int keyCode) { System.out.println("keyPressed " +((char)keyCode)); } } 会评估对象列表而不是(leaf-children node)。事实上,leaf没有孩子。

当您选择父级时,您使用的是实际对象,但是对于子级,您可以使用引号列表作为子级。因此,它们不是对象列表而是符号。

如果没有突变,您可以向上或向下指向。两者都需要通过变异做一个然后另一个。也许从叶到根。 E很奇怪,因为我猜全二叉树应该是binarytree