;;; ======================================= ;;; CMPU-365, Fall 2014 ;;; Scheme warm-up with Binary Trees ;;; ======================================= ;;; TESTER ;;; ----------------------------- ;;; INPUT: EXPR, anything ;;; OUTPUT: The result of evaluating EXPR ;;; SIDE EFFECT: Prints out EXPR in interactions window, ;;; before evaluating it. ;;; EXAMPLE: (tester '(+ 1 2)) displays (+ 1 2) then evaluates to 3. ;;; NOTE THE QUOTE!! (define tester (lambda (expr) (printf "~A ==> " expr) (eval expr))) ;;; PROBLEM ;;; ----------------------------------- ;;; INPUT: STR, a string describing the problem ;;; OUTPUT: None ;;; SIDE EFFECT: Displays a nice problem header in the interactions window (define problem (lambda (str) (printf "~%============================================~%") (printf " ~A ~%" str) (printf "============================================~%~%"))) ;;; A binary tree is one of the following: ;;; --- An EMPTY tree, represented by the ;;; underscore symbol, _ ;;; --- A NON-EMPTY tree, represented by ;;; an instance of the NODE struct, defined below. ;;; A global variable for the EMPTY tree: (define *mt-tree* '_) ;;; A type-checker for the EMPTY tree: (define mt-tree? (lambda (thing) (eq? thing *mt-tree*))) ;;; The NODE Struct ;;; -------------------------------- ;;; FIELDS: LEFTY, a binary tree ;;; DATUM, a piece of data ;;; RIGHTY, a binary tree (define-struct node (lefty datum righty)) ;;; FUNCTIONS provided by DEFINE-STRUCT ;;; ------------------------------------------------ ;;; MAKE-NODE ;;; NODE? ;;; NODE-LEFTY ;;; NODE-RIGHTY ;;; NODE-DATUM ;;; ------------------------------------------------ ;;; NOTE: I'm not showing the mutator functions because ;;; you are not allowed to use them for this assignment! ;;; ------------------------------------------------ ;;; NOTE: A binary tree is a RECURSIVE data structure ;;; because the LEFTY and RIGHTY parts of a NODE ;;; are binary trees. ;;; ========================================== ;;; PRINTING OUT A BINARY TREE ;;; ========================================== ;;; PRINT-N-SPACES ;;; -------------------------------------- ;;; INPUT: N, a non-negative integer ;;; OUTPUT: None ;;; SIDE EFFECT: Prints out N spaces (no newline character) (define print-n-spaces (lambda (n) (cond ((<= n 0) (void)) (#t (printf " ") (print-n-spaces (- n 1)))))) ;;; PRINT-BIN-TREE-HELPER ;;; -------------------------------------- ;;; INPUTS: TREE, a binary tree ;;; INDENT, the indentation amount at which this tree ;;; should be printed ;;; OUTPUT: None ;;; SIDE EFFECT: Displays the contents of the binary tree in ;;; the Interactions Window, indented by the indicated number of spaces. ;;; -------------------------------------- ;;; NOTE: The root node of the tree is shown to the ;;; far left of the display. Deeper nodes in ;;; the tree are shown further to the right. ;;; If you rotate your Interactions Window ;;; by 90 degrees, the tree will look right! (define print-bin-tree-helper (lambda (tree indent) (cond ;; Base Case: The TREE is empty ((mt-tree? tree) ;; Print some spaces to indicate the current depth of the tree (print-n-spaces indent) ;; and then a vertical bar to indicate a LEAF node (printf "|~%")) ;; Recursive Case: The TREE is a NODE... (#t ;; Print the RIGHT sub-tree (at indent + 10) (print-bin-tree-helper (node-righty tree) (+ indent 10)) ;; Print the DATUM (print-n-spaces indent) (printf "[~A]~%" (node-datum tree)) ;; Print the LEFT sub-tree (at indent + 10) (print-bin-tree-helper (node-lefty tree) (+ indent 10)))))) ;;; PRINT-BIN-TREE ;;; ---------------------------------------------- ;;; INPUT: TREE, a binary tree ;;; OUTPUT: None ;;; SIDE EFFECT: Displays the binary tree. ;;; ---------------------------------------------- ;;; This function is a wrapper for PRINT-BIN-TREE-HELPER. (define print-bin-tree (lambda (tree) (newline) ;; Call helper function with indentation initially 0 (print-bin-tree-helper tree 0))) ;;; Making trees using MAKE-NODE is analogous to ;;; building lists using CONS. It's not fun... (define tree1 (make-node *mt-tree* 55 *mt-tree*)) (define tree2 (make-node *mt-tree* 23 *mt-tree*)) (define tree3 (make-node tree1 37 tree2)) (tester '(print-bin-tree tree3)) (define tree10 (make-node *mt-tree* 77 *mt-tree*)) (define tree11 (make-node *mt-tree* 92 *mt-tree*)) (define tree12 (make-node tree10 100 tree11)) (define big-tree (make-node tree3 999 tree12)) (tester '(print-bin-tree big-tree))