This commit is contained in:
2025-10-10 01:06:59 -04:00
parent 49c69a495f
commit 3fb80bd5e0
6 changed files with 39 additions and 173 deletions
+37 -31
View File
@@ -13,30 +13,28 @@
(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))
(define deeply-broke-tree (make-tree 5 (make-tree 3 (make-tree 2 sprout sprout) 'end) (make-tree 10 (make-tree 7 'end 'end) 'end)))
(define deeply-broke-tree
(make-tree 10
(make-tree 6 'end (make-tree 11 'end 'end))
(make-tree 12 'end 'end)))
; 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)))))
(local [; Convenient form of greater than.
(define (<2 a b c)
(cond [(and (symbol? a) (symbol? c)) #t]
[(symbol? a) (< (- b 1) b c)]
[(symbol? c) (< a b (+ b 1))]
[else (< a b c)]))
; 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)))
; Recursively checks left & right with lower & upper bounds.
(define (recurse t lower upper)
(cond [(symbol? t) (symbol=? t 'end)]
[else (and (<2 lower (tree-n t) upper)
(recurse (tree-left t) lower (tree-n t))
(recurse (tree-right t) (tree-n t) upper))]))]
(recurse t 'quite-little 'very-big)))
(check-expect (bst? sprout) #t)
(check-expect (bst? sapling) #t)
@@ -63,20 +61,27 @@
; String w, Trie t -> Boolean
; Determines whether the given word is in the given Trie.
(define (in-trie? w t)
(local [; [Node] l, String wsf -> Boolean
; Basically ormap of in?h on list of nodes but no lambda so this exists.
(define (childs? l wsf)
(and (not (empty? l))
(or (in?h (first l) wsf) (childs? (rest l) wsf))))
(local [(define ln (string-length w))
; Node n, String wsf -> Boolean
; Helper for in?. Accumulates word so far.
(define (in?h n wsf)
(or (and (string=? (string-append wsf (node-c n)) w) (node-e n)) (childs? (node-l n) (string-append wsf (node-c n)))))
; undernode?: l: [Node], i: [Index] → Bool
; Whether it's in the node's list of nodes.
(define (undernode? l i)
(cond [(empty? l) #f]
[else (or (atnode? (first l) i)
(undernode? (rest l) i))]))
; Node n -> Boolean
; Determines whether word is in the node or child nodes.
(define (in? n) (in?h n ""))] (ormap in? t)))
; atnode?: n: Node, i: Index → Bool
; Check if node has the character at index i in the word, and continue down if it does.
(define (atnode? n i)
(if (>= i ln)
#f
(if (string=? (node-c n) (substring w i (+ i 1)))
(if (= (+ i 1) ln) ; If we've hit the end of the word.
(node-e n)
(undernode? (node-l n) (+ i 1)))
#f)))]
(undernode? t 0)))
(check-expect (in-trie? "cat" example-trie) #t)
(check-expect (in-trie? "ca" example-trie) #f)
@@ -84,6 +89,7 @@
(check-expect (in-trie? "better" example-trie) #t)
(check-expect (in-trie? "bee" example-trie) #t)
(check-expect (in-trie? "beee" example-trie) #f)
(check-expect (in-trie? "" example-trie) #f)
; [String] l -> Trie
; Generates the trie for a list of strings.