Updated.
This commit is contained in:
+61
-7
@@ -1,33 +1,36 @@
|
|||||||
|
;; 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)))
|
#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])
|
; - (make-tree [Number MaybeTree MaybeTree])
|
||||||
(define-struct tree [n left right])
|
(define-struct tree [n left right])
|
||||||
; - 'end
|
; - 'end
|
||||||
|
|
||||||
(define sprout (make-tree 0 'end 'end))
|
(define sprout (make-tree 0 'end 'end))
|
||||||
(define seedling (make-tree 5 sprout (make-tree 6 '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 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 good-tree (make-tree 50 sapling (make-tree 100 'end 'end)))
|
||||||
(define broke-tree (make-tree 5 good-tree good-tree))
|
(define broke-tree (make-tree 5 good-tree good-tree))
|
||||||
|
|
||||||
; Tree -> Bool
|
; Tree t -> Bool
|
||||||
; Determines whether a Tree is a binary search tree.
|
; Determines whether a Tree is a binary search tree.
|
||||||
(define (bst? t)
|
(define (bst? t)
|
||||||
(local [; Tree Number -> Bool
|
(local [; Tree t -> Bool
|
||||||
; Recursively checks left and right.
|
; Recursively checks left and right.
|
||||||
(define (recurse t)
|
(define (recurse t)
|
||||||
(or (and (symbol? t) (symbol=? t 'end))
|
(or (and (symbol? t) (symbol=? t 'end))
|
||||||
(and (acc-right (tree-right t) (tree-n t))
|
(and (acc-right (tree-right t) (tree-n t))
|
||||||
(acc-left (tree-left t) (tree-n t)))))
|
(acc-left (tree-left t) (tree-n t)))))
|
||||||
|
|
||||||
; Tree Number -> Bool
|
; Tree t, Number n -> Bool
|
||||||
; Accumulates the current number to check left.
|
; Accumulates the current number to check left.
|
||||||
(define (acc-left t n)
|
(define (acc-left t n)
|
||||||
(or (and (symbol? t) (symbol=? t 'end))
|
(or (and (symbol? t) (symbol=? t 'end))
|
||||||
(and (< (tree-n t) n) (recurse t))))
|
(and (< (tree-n t) n) (recurse t))))
|
||||||
|
|
||||||
; Tree Number -> Bool
|
; Tree t, Number n -> Bool
|
||||||
; Accumulates the current number to check right.
|
; Accumulates the current number to check right.
|
||||||
(define (acc-right t n)
|
(define (acc-right t n)
|
||||||
(or (and (symbol? t) (symbol=? t 'end))
|
(or (and (symbol? t) (symbol=? t 'end))
|
||||||
@@ -38,4 +41,55 @@
|
|||||||
(check-expect (bst? sapling) #t)
|
(check-expect (bst? sapling) #t)
|
||||||
(check-expect (bst? good-tree) #t)
|
(check-expect (bst? good-tree) #t)
|
||||||
(check-expect (bst? broke-tree) #f)
|
(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
|
||||||
|
(list (make-node "a" #t
|
||||||
|
(list (make-node "n" #t (list (make-node "d" #t '()) (make-node "t" #t '())))))
|
||||||
|
(make-node "b" #f
|
||||||
|
(list (make-node "e" #t (list (make-node "e" #t '()) (make-node "t" #t (list (make-node "t" #f (list (make-node "e" #f (list (make-node "r" #t '())))))))))))
|
||||||
|
(make-node "c" #f (list (make-node "a" #f (list (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 [; [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))))
|
||||||
|
|
||||||
|
; 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)))))
|
||||||
|
|
||||||
|
; Node n -> Boolean
|
||||||
|
; Determines whether word is in the node or child nodes.
|
||||||
|
(define (in? n) (in?h n ""))] (ormap in? t)))
|
||||||
|
|
||||||
|
(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)
|
||||||
|
|
||||||
|
; [String] l -> Trie
|
||||||
|
; Generates the trie for a list of strings.
|
||||||
|
(define (strie l)
|
||||||
|
(local [; String w, Trie t -> Trie
|
||||||
|
; Adds word w to a Trie.
|
||||||
|
(define (triadd w t)
|
||||||
|
(if (
|
||||||
|
|
||||||
|
(check-exepct (strie '("and" "bee" "cat" "an" "a" "ant" "better" "bet" "be")) example-trie)
|
||||||
|
|||||||
Reference in New Issue
Block a user