Advertisement
Guest User

binary tree

a guest
Apr 6th, 2019
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Scheme 2.23 KB | None | 0 0
  1. (define check
  2.   (lambda (bst)
  3.     (and (not (null? bst)) ;;not null
  4.          (number? (car bst)) ;; entry is a number
  5.          (= (length bst) 3) ;;3 long
  6.          (list? (car (cdr bst))) ;; 2 lists
  7.          (list? (car (cdr (cdr bst))))
  8.     )
  9.   )
  10. )
  11.  
  12.  
  13.  
  14.  
  15. (define entry
  16.     (lambda (bst)
  17.       (if  (check bst)
  18.            (car bst)
  19.            #f
  20.       )
  21.  ))
  22.  
  23. (define left
  24.     (lambda (bst)
  25.       (if (check bst)
  26.           (car (cdr bst))
  27.           #f
  28.       )
  29.  ))
  30.  
  31. (define right
  32.   (lambda (bst)
  33.     (if (check bst)
  34.       (car (cdr (cdr bst)))
  35.       #f
  36.       )
  37.  ))
  38.  
  39. ;(entry '(5 (3 () (4 () ()) ) () ))
  40.  
  41. (define proper-tree?
  42.   (lambda (bst)
  43.         (or (null? bst) ;;is it a leaf
  44.             (and (check bst) ;; is this level proper
  45.                 (or (null? (car (cdr bst)))       (proper-tree? (car (cdr bst))))        ;; is the left good
  46.                 (or (null? (car (cdr (cdr bst)))) (proper-tree? (car (cdr (cdr bst)))))  ;; is the right good
  47.             )
  48.         )
  49.     )
  50. )
  51.  
  52. ;(make-bst elt left right)
  53. (define make-bst
  54.   (lambda (elt left right)
  55.     (and (number? elt) (proper-tree? left) (proper-tree? right)
  56.          (list elt left right))))
  57.  
  58. ;;(define preorder
  59. ;;  (lambda (bst)
  60. ;;         (if (null? bst)
  61. ;;             ()
  62. ;;             (cons (cons (entry bst) (preorder (left bst)))
  63. ;;                  (preorder (right bst))
  64. ;;               )
  65. ;;              )
  66. ;;          )
  67. ;;    )
  68.  
  69. (define preorder
  70.   (lambda (bst)
  71.          (if (null? bst)
  72.              ()
  73.              (cond ((and (null? (left bst)) (null? (right bst)))    (entry bst));; both null
  74.                    ((null? (left bst))                           (append (entry bst) ((preorder (right bst))) ()));; left null
  75.                    ((null? (right bst))                          (append (entry bst) ((preorder (left bst)))));; right null
  76.                    (else                                         (append (entry bst) ((preorder (left bst)))((preorder (right bst))))) ;;neither null
  77.              )  
  78.           )
  79.     )
  80. )
  81.  
  82.  
  83. (define inorder
  84.   (lambda (bst)
  85.     (if (null? bst)
  86.         ()
  87.        
  88.     )
  89.  ))
  90.  
  91.  (require racket/trace)
  92.  (trace preorder)
  93. (preorder '(5 (3 () (4 () ()) ) (8 () (10 () ()) )))
  94.  
  95.  
  96.  
  97.  
  98. ;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement