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

全部博文(136)

文章存档

2013年(1)

2011年(135)

我的朋友

分类: LINUX

2011-11-18 11:21:48

  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. ;; A list-of-number/name is either
  15. ;; 1. empty or
  16. ;; 2. (cons (list ssn nom) lonn)
  17. ;; where ssn is a number, nom a symbol, and lonn is a list-of-number/name.



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

  20. ;; create-bst : BST n s -> BST
  21. ;; to create a new BST by adding a new node with n s
  22. (define (create-bst a-bt n s)
  23.   (cond
  24.     [(false? a-bt) (make-node n s false false)]
  25.     [else
  26.      (cond
  27.        [(= n (node-ssn a-bt)) (error "change a new ssn")]
  28.        [(< n (node-ssn a-bt))
  29.         (make-node (node-ssn a-bt) (node-name a-bt)
  30.                    (create-bst (node-left a-bt) n s)
  31.                    (node-right a-bt))]
  32.        [else
  33.         (make-node (node-ssn a-bt) (node-name a-bt)
  34.                    (node-left a-bt)
  35.                    (create-bst (node-right a-bt) n s))])]))

  36. ;; test
  37. ;(create-bst false 66 'a)
  38. ;(create-bst (create-bst (create-bst (create-bst false 66 'c) 77 'a) 53 'b) 88 'd)

  39. ;;
  40. (define treeA1
  41.   (create-bst
  42.    (create-bst
  43.     (create-bst
  44.      (create-bst
  45.       (create-bst
  46.        (create-bst
  47.         (create-bst
  48.          (create-bst
  49.           (create-bst false 63 'a) 29 'b) 15 'c) 10 'd) 24 'e) 89 'f) 77 'g) 95 'h) 99 'i))

  50. ;;test
  51. ;; inorder : BT -> list-of-numbers
  52. ;; to produce a list of all the ssn in a-bt
  53. (define (inorder a-bt)
  54.   (cond
  55.     [(false? a-bt) empty]
  56.     [else
  57.      (append
  58.       (cons (node-ssn a-bt) empty)
  59.       (inorder (node-left a-bt))
  60.       (inorder (node-right a-bt)))]))

  61. ;; test
  62. ;(inorder treeA1)

  63. ;; create-bst-from-list : list-of-number/name -> BST
  64. ;; to produce a BST from a-lonn
  65. (define (create-bst-from-list a-lonn)
  66.   (cond
  67.     [(empty? a-lonn) false]
  68.     [else
  69.       (create-bst (create-bst-from-list (rest a-lonn)) (first (first a-lonn)) (second (first a-lonn)))]))
  70. ;; test
  71. (define sample
  72.  
阅读(370) | 评论(0) | 转发(0) |
0

上一篇:exam14.2.5

下一篇:exam2.1.1

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