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

全部博文(136)

文章存档

2013年(1)

2011年(135)

我的朋友

分类: LINUX

2011-11-18 11:21:11

  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. ;; A binary-search-tree (short: BST) is a BT:
  9. ;; 1. false is always a BST;
  10. ;; 2. (make-node soc pn lft rgt) is a BST if
  11. ;; a. lft and rgt are BSTs;
  12. ;; b. all ssn numbers in lft are smaller than soc, and
  13. ;; c. all ssn numbers in rgt are larger than soc.

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

  16. ;; Draw a tree:
  17. (define treeA
  18.   (make-node 63 'a
  19.            (make-node 29 'b
  20.                       (make-node 15 'c
  21.                                  (make-node 10 'd false false)
  22.                                  (make-node 24 'e false false))
  23.                       false)
  24.            (make-node 89 'f
  25.                       (make-node 77 'g false false)
  26.                       (make-node 95 'h false
  27.                                  (make-node 99 'i false false)))))


  28. ;; create-bst : BST n s -> BST
  29. ;; to create a new BST by adding a new node with n s
  30. (define (create-bst a-bt n s)
  31.   (cond
  32.     [(false? a-bt) (make-node n s false false)]
  33.     [else
  34.      (cond
  35.        [(= n (node-ssn a-bt)) (error "change a new ssn")]
  36.        [(< n (node-ssn a-bt))
  37.         (make-node (node-ssn a-bt) (node-name a-bt)
  38.                    (create-bst (node-left a-bt) n s)
  39.                    (node-right a-bt))]
  40.        [else
  41.         (make-node (node-ssn a-bt) (node-name a-bt)
  42.                    (node-left a-bt)
  43.                    (create-bst (node-right a-bt) n s))])]))

  44. ;; test
  45. ;(create-bst false 66 'a)
  46. (create-bst (create-bst (create-bst (create-bst false 66 'c) 77 'a) 53 'b) 88 'd)

  47. ;;
  48. (define treeA1
  49.   (create-bst
  50.    (create-bst
  51.     (create-bst
  52.      (create-bst
  53.       (create-bst
  54.        (create-bst
  55.         (create-bst
  56.          (create-bst
  57.           (create-bst false 63 'a) 29 'b) 15 'c) 10 'd) 24 'e) 89 'f) 77 'g) 95 'h) 99
阅读(336) | 评论(0) | 转发(0) |
0

上一篇:exam14.2.4

下一篇:exam14.2.6

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