From 34cee3e9108d7ad4a153f11c88f7a0b0222c948f Mon Sep 17 00:00:00 2001 From: Jacob Date: Thu, 25 Sep 2025 23:34:02 -0400 Subject: [PATCH] Updated. --- trees/main.rkt | 27 +++++++++++++++--- trees/main.rkt~ | 74 ------------------------------------------------- 2 files changed, 23 insertions(+), 78 deletions(-) delete mode 100644 trees/main.rkt~ diff --git a/trees/main.rkt b/trees/main.rkt index 0fd0bbe..fef0b0d 100644 --- a/trees/main.rkt +++ b/trees/main.rkt @@ -1,7 +1,7 @@ ;; The first three lines of this file were inserted by DrRacket. They record metadata ;; about the language level of this file in a form that our tools can easily process. #reader(lib "htdp-intermediate-reader.ss" "lang")((modname main) (read-case-sensitive #t) (teachpacks ()) (htdp-settings #(#t constructor repeating-decimal #f #t none #f () #f))) -#|; A MaybeTree is one of +; A MaybeTree is one of ; - (make-tree [Number MaybeTree MaybeTree]) (define-struct tree [n left right]) ; - 'end @@ -41,7 +41,6 @@ (check-expect (bst? sapling) #t) (check-expect (bst? good-tree) #t) (check-expect (bst? broke-tree) #f) -|# ; A Trie is a [Node] @@ -90,6 +89,26 @@ (local [; String w, Trie t -> Trie ; Adds word w to a Trie. (define (triadd w t) - (if ( + (local [; String c, String wrest, Trie t -> Trie + ; Helper function to insert a chacater (and the rest of the word) to the Trie. + (define (insert-char c wrest t) + (cond [(empty? t) + (list (make-node c ; Node character. + (string=? wrest "") ; Mark whether Node's a word end. + (if (string=? wrest "") ; If more left in the word, recurse. + '() + (triadd wrest '()))))] + [(string=? (node-c (first t)) c) ; Character matches -- return existing Trie Node + whatever added if necessary. + (cons (make-node c + (or (node-e (first t)) + (string=? wrest "")) + (if (string=? wrest "") + (node-l (first t)) + (triadd wrest (node-l (first t))))) ; Recurse. + (rest t))] + [else + (cons (first t) (insert-char c wrest (rest t)))]))] + (insert-char (substring w 0 1) (substring w 1) t)))] + (foldl triadd '() l))) -(check-exepct (strie '("and" "bee" "cat" "an" "a" "ant" "better" "bet" "be")) example-trie) +(check-expect (strie '("and" "bee" "cat" "an" "a" "ant" "better" "bet" "be")) example-trie) diff --git a/trees/main.rkt~ b/trees/main.rkt~ deleted file mode 100644 index 0dccaa1..0000000 --- a/trees/main.rkt~ +++ /dev/null @@ -1,74 +0,0 @@ -#reader(lib "htdp-intermediate-reader.ss" "lang")((modname main) (read-case-sensitive #t) (teachpacks ()) (htdp-settings #(#t constructor repeating-decimal #f #t none #f () #f))) - -#| -; A MaybeTree is one of -; - (make-tree [Number MaybeTree MaybeTree]) -(define-struct tree [n left right]) -; - 'end - -(define sprout (make-tree 0 'end 'end)) -(define seedling (make-tree 5 sprout (make-tree 6 'end 'end))) -(define sapling (make-tree 10 - (make-tree 6 sprout 'end) - (make-tree 12 'end 'end))) -(define good-tree (make-tree 50 sapling (make-tree 100 'end 'end))) -(define broke-tree (make-tree 5 good-tree good-tree)) - -; Tree t -> Bool -; Determines whether a Tree is a binary search tree. -(define (bst? t) - (local [; Tree t -> Bool - ; Recursively checks left and right. - (define (recurse t) - (or (and (symbol? t) (symbol=? t 'end)) - (and (acc-right (tree-right t) (tree-n t)) - (acc-left (tree-left t) (tree-n t))))) - - ; Tree t, Number n -> Bool - ; Accumulates the current number to check left. - (define (acc-left t n) - (or (and (symbol? t) (symbol=? t 'end)) - (and (< (tree-n t) n) (recurse t)))) - - ; Tree t, Number n -> Bool - ; Accumulates the current number to check right. - (define (acc-right t n) - (or (and (symbol? t) (symbol=? t 'end)) - (and (> (tree-n t) n) (recurse t))))] - (recurse t))) - -(check-expect (bst? sprout) #t) -(check-expect (bst? sapling) #t) -(check-expect (bst? good-tree) #t) -(check-expect (bst? broke-tree) #f) -|# - -; A Trie is a [Node] - -; A Node is a -; (make-node [String c Boolean e [Node] l]) -; and represents a prefix tree. 'c' represents the node character, 'e' is a -; flag for if the node marks the end of a word in the tree, and 'l' is the list -; of child nodes. -(define-struct node [c e l]) - -(define example-trie - '((make-node "a" #t - ((make-node "n" #t ((make-node "d" #t ()) (make-node "t" #t ()))))) - (make-node "b" #f - ((make-node "e" #t ((make-node "e" #t ()) - (make-node "t" #t ((make-node "t" #f ((make-node "e" #f ((make-node "r" #t ()))))))))))) - (make-node "c" #f ((make-node "a" #f ((make-node "t" #t ()))))))) - -; String w, Trie t -> Boolean -; Determines whether the given word is in the given Trie. -(define (in-trie? w t) - (local [; [String]) - -(check-expect (in-trie? "cat" example-trie) #t) -(check-expect (in-trie? "ca" example-trie) #f) -(check-expect (in-trie? "catch" example-trie) #f) -(check-expect (in-trie? "better" example-trie) #t) -(check-expect (in-trie? "bee" example-trie) #t) -(check-expect (in-trie? "beee" example-trie) #f) -