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

全部博文(136)

文章存档

2013年(1)

2011年(135)

我的朋友

分类: LINUX

2011-11-18 11:20:01

;; mht created on Nov 17, 2011

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

;; A binary-search-tree (short: BST) is a BT:
;; 1. false is always a BST;
;; 2. (make-node soc pn lft rgt) is a BST if
;;    a. lft and rgt are BSTs;
;;    b. all ssn numbers in lft are smaller than soc, and
;;    c. all ssn numbers in rgt are larger than soc.

;; examples
;(make-node 15 'd false (make-node 24 'i false false))

;; Draw a tree:
(define treeA
  (make-node 63 'a
           (make-node 29 'b
                      (make-node 15 'c
                                 (make-node 10 'd false false)
                                 (make-node 24 'e false false))
                      false)
           (make-node 89 'f
                      (make-node 77 'g false false)
                      (make-node 95 'h false
                                 (make-node 99 'i false false)))))


;; inorder : BT -> list-of-numbers
;; to produce a list of all the ssn in a-bt
(define (inorder a-bt)
  (cond
    [(false? a-bt) empty]
    [else
     (append
      (cons (node-ssn a-bt) empty)
      (inorder (node-left a-bt))
      (inorder (node-right a-bt)))]))

;; test
(inorder treeA)
阅读(370) | 评论(0) | 转发(0) |
0

上一篇:exam14.2.2

下一篇:exam14.2.4

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