我无法解决两个硬件问题。其中一个需要为具有契约的插入和搜索编写多态二叉树函数。
insert : a (treeof a) (a a -> bool) -> (treeof a)
和
search : a (treeof a) (a a -> bool) -> bool
合同是
;; A bst is either empty or (make-bst n l r) where
;; n is an alpha, r and l are bst.
(define-struct bst (n l r))
我并非真正理解合同的a a -> bool
部分,这让我感到困惑,因为我不知道我是否应该在那里进行测试或是否应该别的。我如何处理这些函数以适应第三个变量?
我的第二个问题有以下合同:
;; A bspt is either
;; - (make-rect c) (the rect represents the leaves of the tree)
;; - (make-bspt s l r) (this is a node with two children
;; Where s is either 'x or 'y, l and r are children
;; and c is a color
(define-struct bspt (s l r))
(define-struct rect (c))
并键入以下内容:
(define t1 (make-bspt 'x (make-rect 'red) (make-rect 'blue)))
(show-tree t1)
给你一个分为两个的正方形,红色为左侧,蓝色为右侧,
(define t2 (make-bspt 'x (make-bspt 'x (make-rect 'green) (make-rect 'red)) (make-rect 'blue)))
(show-tree t2)
再次分割左侧,首先是绿色矩形,然后是原始红色,然后蓝色侧保持不变。
最后,
(define t3 (make-bspt 'x (make-bspt 'x (make-bspt 'y (make-rect 'yellow) (make-rect 'orange)) (make-rect 'red)) (make-rect 'blue)))
(show-tree t3)
将绿色部分分成两半,绿色部分的顶部为橙色,底部为黄色,其他部分保持不变。我无法想到一个函数,如果你输入上面给定的函数,它会给你那些相应的图纸。任何有用的提示/代码都会有所帮助和赞赏!
答案 0 :(得分:2)
你真的一次提出很多问题,所以我会采用简单的方法来帮助你入门,即search
功能。
这是我写它的方式(使用null
代替empty
,它们是等效的):
; contract: e is of type a
; atree is of type (tree-of a)
; equalp is of type (a a -> bool)
(define (bst-search e atree equalp)
(define (sub atree)
(and (not (null? atree))
(or (equalp e (bst-n atree))
(sub (bst-l atree))
(sub (bst-r atree)))))
(sub atree))
所以e
是任何类型的元素,atree
是树,equalp
是一个可以用于e
类型的相等谓词。< / p>
对于符号,equalp
将是eq?
:
(define t1 (make-bst 'blue (make-bst 'red null null) (make-bst 'green null null)))
(bst-search 'blue t1 eq?)
=> #t
(bst-search 'red t1 eq?)
=> #t
(bst-search 'yellow t1 eq?)
=> #f
和字符串string=?
:
(define t2 (make-bst "blue" (make-bst "red" null null) (make-bst "green" null null)))
(bst-search "blue" t2 string=?)
=> #t
(bst-search "red" t2 string=?)
=> #t
(bst-search "yellow" t2 string=?)
=> #f
我对Racket绘图库并不熟悉,但是一个简单的show-tree看起来像这样:
(require 2htdp/image)
(define (show-tree tree)
(cond
((bspt? tree)
((if (eq? 'x (bspt-s tree)) beside above) (show-tree (bspt-l tree))
(show-tree (bspt-r tree))))
((rect? tree)
(square 20 'solid (rect-c tree)))))
然后
(define t1 (make-bspt 'x (make-rect 'red) (make-rect 'blue)))
(show-tree t1)
(define t2 (make-bspt 'x (make-bspt 'x (make-rect 'green) (make-rect 'red)) (make-rect 'blue)))
(show-tree t2)
(define t3 (make-bspt 'x (make-bspt 'x (make-bspt 'y (make-rect 'yellow) (make-rect 'orange)) (make-rect 'red)) (make-rect 'blue)))
(show-tree t3)
这些可能没有按照它们应该对齐或调整大小,但是你可以通过基于树高度进行一些计算来做得更好:
(define (tree-space tree)
(if (bspt? tree)
(let ((x (bspt-s tree)))
(define-values (lx ly) (tree-space (bspt-l tree)))
(define-values (rx ry) (tree-space (bspt-r tree)))
(if (eq? x 'x)
(values (* 2 (max lx rx)) (max ly ry))
(values (max lx rx) (* 2 (max ly ry)))))
(values 20 20)))
(define (show-tree tree)
(define (sub tree x y)
(cond
((bspt? tree)
(define s (bspt-s tree))
(define d (if (eq? 'x s) beside above))
(apply (lambda (x y) (d (sub (bspt-l tree) x y) (sub (bspt-r tree) x y)))
(if (eq? s 'x) (list (/ x 2) y) (list x (/ y 2)))))
((rect? tree)
(rectangle x y 'solid (rect-c tree)))))
(define-values (totx toty) (tree-space tree))
(sub tree totx toty))
产生
或类似的东西。