;; The first three lines of this file were inserted by DrScheme. They record metadata ;; about the language level of this file in a form that our tools can easily process. #reader(lib "htdp-advanced-reader.ss" "lang")((modname |31.3|) (read-case-sensitive #t) (teachpacks ((lib "draw.ss" "teachpack" "htdp") (lib "arrow.ss" "teachpack" "htdp") (lib "gui.ss" "teachpack" "htdp"))) (htdp-settings #(#t constructor repeating-decimal #t #t none #f ((lib "draw.ss" "teachpack" "htdp") (lib "arrow.ss" "teachpack" "htdp") (lib "gui.ss" "teachpack" "htdp"))))) (define-struct node (left right)) ;A binary-tree (bt) is either ;1. empty or ;2. (make-node l r) ;where l,r are binary-trees (bt). ; ;height-1 : bt -> number ;Determines the height (depth) of a binary tree using structural recursion. (define (height-1 abt) (cond [(empty? abt) 0] [else (+ (max (height-1 (node-left abt)) (height-1 (node-right abt))) 1)])) ;height-2 : bt -> number ;Determines the height (depth) of a binary tree using an accumulator. (define (height-2 abt) (local ;; the accumulator represents the number of nodes passed in going from abt to bt0 ((define (height-aux bt0 accumulator) (cond [(empty? bt0) accumulator] [else (max (height-aux (node-left bt0) (+ 1 accumulator)) (height-aux (node-right bt0) (+ 1 accumulator)))]))) (height-aux abt 0))) (define bt1 (make-node (make-node empty (make-node empty empty)) empty)) (height-1 bt1) (height-2 bt1)