Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- (define check
- (lambda (bst)
- (and (not (null? bst)) ;;not null
- (number? (car bst)) ;; entry is a number
- (= (length bst) 3) ;;3 long
- (list? (car (cdr bst))) ;; 2 lists
- (list? (car (cdr (cdr bst))))
- )
- )
- )
- (define entry
- (lambda (bst)
- (if (check bst)
- (car bst)
- #f
- )
- ))
- (define left
- (lambda (bst)
- (if (check bst)
- (car (cdr bst))
- #f
- )
- ))
- (define right
- (lambda (bst)
- (if (check bst)
- (car (cdr (cdr bst)))
- #f
- )
- ))
- ;(entry '(5 (3 () (4 () ()) ) () ))
- (define proper-tree?
- (lambda (bst)
- (or (null? bst) ;;is it a leaf
- (and (check bst) ;; is this level proper
- (or (null? (car (cdr bst))) (proper-tree? (car (cdr bst)))) ;; is the left good
- (or (null? (car (cdr (cdr bst)))) (proper-tree? (car (cdr (cdr bst))))) ;; is the right good
- )
- )
- )
- )
- ;(make-bst elt left right)
- (define make-bst
- (lambda (elt left right)
- (and (number? elt) (proper-tree? left) (proper-tree? right)
- (list elt left right))))
- ;;(define preorder
- ;; (lambda (bst)
- ;; (if (null? bst)
- ;; ()
- ;; (cons (cons (entry bst) (preorder (left bst)))
- ;; (preorder (right bst))
- ;; )
- ;; )
- ;; )
- ;; )
- (define preorder
- (lambda (bst)
- (if (null? bst)
- ()
- (cond ((and (null? (left bst)) (null? (right bst))) (entry bst));; both null
- ((null? (left bst)) (append (entry bst) ((preorder (right bst))) ()));; left null
- ((null? (right bst)) (append (entry bst) ((preorder (left bst)))));; right null
- (else (append (entry bst) ((preorder (left bst)))((preorder (right bst))))) ;;neither null
- )
- )
- )
- )
- (define inorder
- (lambda (bst)
- (if (null? bst)
- ()
- )
- ))
- (require racket/trace)
- (trace preorder)
- (preorder '(5 (3 () (4 () ()) ) (8 () (10 () ()) )))
- ;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement