Chinaunix首页 | 论坛 | 博客
  • 博客访问: 220954
  • 博文数量: 136
  • 博客积分: 2919
  • 博客等级: 少校
  • 技术积分: 1299
  • 用 户 组: 普通用户
  • 注册时间: 2011-03-11 09:08
文章分类

全部博文(136)

文章存档

2013年(1)

2011年(135)

我的朋友

分类: LINUX

2011-11-18 11:19:16

  1. ;; mht created on Nov 17, 2011

  2. ;; data definition :
  3. (define-struct node (ssn name left right))
  4. ;; A binary-tree (short : BT) is either
  5. ;; 1. false; or
  6. ;; 2. (make-node soc pn lft rgt)
  7. ;; shere soc is a number, pn js a symbol, and lft and rgt are BTs.

  8. ;; examples
  9. (make-node
  10.  15 'd false (make-node 24 'i false false))

  11. ;; Draw two trees:
  12. (define treeA
  13.   (make-node 63 'a
  14.            (make-node 29 'b
  15.                       (make-node 15 'c
  16.                                  (make-node 10 'd false false)
  17.                                  (make-node 24 'e false false))
  18.                       false)
  19.            (make-node 89 'f
  20.                       (make-node 77 'g false false)
  21.                       (make-node 95 'h false
  22.                                  (make-node 99 'i false false)))))

  23. ;; contains-bt : number a-bt -> boolean
  24. ;; to determines whether n occurs in a-bt
  25. (define (contains-bt n a-bt)
  26.   (cond
  27.     [(false? a-bt) false]
  28.     [else
  29.      (cond
  30.        [(= n (node-ssn a-bt)) true]
  31.        [(contains-bt n (node-left a-bt)) true]
  32.        [(contains-bt n (node-right a-bt)) true]
  33.        [else false])]))

  34. ;; test
  35. ;(contains-bt 63 treeA)
  36. ;(contains-bt 99 treeA)
  37. ;(not (contains-bt 2 treeA))

  38. ;; search-bt : number BT -> symbol
  39. (define (search-bt n a-bt)
  40.   (cond
  41.     [(false? a-bt) false]
  42.     [else (cond
  43.             [(= n (node-ssn a-bt)) (node-name a-bt)]
  44.             [(contains-bt n (node-left a-bt)) (search-bt n (node-left a-bt))]
  45.             [(contains-bt n (node-right a-bt)) (search-bt n (node-right a-bt))]
  46.             [else false])]))

  47. ;; test
  48. (symbol=? 'a (search-bt 63 treeA))
  49. (symbol=?
阅读(263) | 评论(0) | 转发(0) |
0

上一篇:exam14.2.1

下一篇:exam14.2.3

给主人留下些什么吧!~~