;; BinTree is either ;; - MTBinTree ;; - NEBinTree ;; A MTBinTree is an empty structure ;; (make-MTBinTree) (define-struct MTBinTree ()) ;; A NEBinTree has a first, which is an any ;; and a left and right, which are BinTrees. ;;(make-NEBinTree any BinTree BinTree) (define-struct NEBinTree (first left right)) ;; An empty BinTree singleton (define MTBT (make-MTBinTree)) ;; BT->string: BinTree --> string ;; Returns a string representation of a Binary Tree filled with numbers (define (BT->string bt) (local [;; prefix values used by BT->String (define left-prefix " | ") (define right-prefix " ") (define dat-prefix " |_ ") (define newline-string (make-string 1 #\newline)) ;; a string with just a linefeed (define empty-string "*") ] (cond [(MTBinTree? bt) (string-append newline-string empty-string newline-string)] [(NEBinTree? bt) (local [;;BT->string_help: BinTree string --> string ;;returns a string representation of a family tree ;; where the prefix string is pre-pended onto every parent's row (define (BT->string_help bt prefix) (cond [(MTBinTree? bt) (string-append dat-prefix empty-string)] [(NEBinTree? bt) (string-append dat-prefix (number->string (NEBinTree-first bt)) newline-string prefix (BT->string_help (NEBinTree-left bt) (string-append prefix left-prefix)) newline-string prefix (BT->string_help (NEBinTree-right bt) (string-append prefix right-prefix)))])) ] (string-append newline-string (number->string (NEBinTree-first bt)) newline-string (BT->string_help (NEBinTree-left bt) left-prefix) newline-string (BT->string_help (NEBinTree-right bt) right-prefix) newline-string))]))) (define bt1 (make-NEBinTree 1 (make-NEBinTree 2 MTBT (make-NEBinTree 4 MTBT MTBT)) (make-NEBinTree 3 (make-NEBinTree 5 MTBT MTBT) (make-NEBinTree 6 MTBT MTBT)))) "bt1: " (display (BT->string bt1)) ;;getNextLevel: BinTree --> list-of-BinTree ;; Returns the BinTrees in the next level of the given BinTree (define (getNextLevel aBT) (cond [(MTBinTree? aBT) empty] [(NEBinTree? aBT) (list (NEBinTree-left aBT) (NEBinTree-right aBT))])) "getNextLevel test cases:" (display (map BT->string (getNextLevel MTBT))) (define newline-string (make-string 1 #\newline)) (display newline-string) (display (map BT->string (getNextLevel bt1))) ;;getNextLevels: list-of-BinTree --> list-of-BinTree ;; Returns the BinTrees in the next level of all the given BinTrees (define (getNextLevels loBT) (apply append (map getNextLevel loBT))) "getNextLevels test cases:" (display (map BT->string (getNextLevels empty))) (display newline-string) (display (map BT->string (getNextLevels (getNextLevel bt1)))) (display newline-string) (display (map BT->string (getNextLevels (getNextLevels (getNextLevel bt1))))) ;; depthTraverse BinTree --> list-of-any ;; returns the elements of aBT in depth-first order. (define (depthTraverse aBT) (cond [(MTBinTree? aBT) empty] [(NEBinTree? aBT) (cons (NEBinTree-first aBT) (apply append (map depthTraverse (getNextLevel aBT))))])) "depthTraverse test cases:" (depthTraverse MTBT) (depthTraverse (make-NEBinTree 1 MTBT MTBT)) (depthTraverse bt1) ;;breadthTraverse: BinTree --> list-of-any ;; Returns the elements of aBT in breadth-first order. (define (breadthTraverse aBT) (cond [(MTBinTree? aBT) empty] [(NEBinTree? aBT) (cons (NEBinTree-first aBT) (breadthTraverse_helper (getNextLevel aBT)))])) ;; breadthTraverse_helper: list-of-BinTree --> list-of-any ;; returns the elements of loBT in breadth-first order (define (breadthTraverse_helper loBT) (cond [(empty? loBT) empty] [(cons? loBT) (append (breadthTraverse_1level loBT) (breadthTraverse_helper (getNextLevels loBT)))])) ;;breadthTraverse_1level: list-of-BinTree --> list-of-any ;; returns the first elements of loBT as a list. (define (breadthTraverse_1level loBT) (apply append (map getFirst loBT))) ;; getFirst: BinTree --> list-of-any ;; utility function to return the first of aBT as a list. ;; returns empty if aBT is an MTBinTree (define (getFirst aBT) (cond [(MTBinTree? aBT) empty] [(NEBinTree? aBT) (list (NEBinTree-first aBT))])) "breadthTraverse test cases:" (breadthTraverse MTBT) (breadthTraverse (make-NEBinTree 1 MTBT MTBT)) (breadthTraverse bt1) ;; buildRandBT: num --> BinTree ;; Builds a random binary tree where all 0<=first<100 ;; and the probablility of an empty tree is 0<= percentMT <100 ;; The probability of an empty tree is increased by 10% for each ;; successive level to limit the size of the total tree. (define (buildRandBT percentMT) (cond [(< (random 100) percentMT) MTBT] [else (make-NEBinTree (random 100) (buildRandBT (* 1.1 percentMT)) (buildRandBT (* 1.1 percentMT)))])) "buildRandBT test cases:" (define percentMT 30) (define bt2 (buildRandBT percentMT)) (define bt3 (buildRandBT percentMT)) (define bt4 (buildRandBT percentMT)) (display (BT->string bt2)) (depthTraverse bt2) (breadthTraverse bt2) (display (BT->string bt3)) (depthTraverse bt3) (breadthTraverse bt3) (display (BT->string bt4)) (depthTraverse bt4) (breadthTraverse bt4)