forked from yhx189/rac
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhm1.rkt
125 lines (112 loc) · 3.92 KB
/
hm1.rkt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
#lang plai
(define eight-principles
(list
"Know your rights."
"Acknowledge your sources."
"Protect your work."
"Avoid suspicion."
"Do your own work."
"Never falsify a record or permit another person to do so."
"Never fabricate data, citations, or experimental results."
"Always tell the truth when discussing your work with your instructor."))
(define-type Tree
[leaf]
[node (val number?)
(left Tree?)
(right Tree?)])
(define (smallest g)
;;
(type-case Tree g
[leaf() +inf.0]
[node (val left right) (min val (smallest left) (smallest right))]
))
(define (negate g)
(type-case Tree g
[leaf() (leaf)]
[node(val left right)
(node (- 0 val) (negate left) (negate right))]))
(define (contains? t num)
(type-case Tree t
[leaf() #f]
[node(val left right) (cond
[(equal? num val) #t]
[else (cond
[(contains? left num) #t]
[else (contains? right num)])])]))
(define (sorted? t)
(type-case Tree t
[leaf() #t]
[node(val left right) (cond
[(< (smallest(negate left)) (- 0 val)) #f]
[(< (- 0 val) (smallest(negate right))) #f]
[else #t])]))
(define (cnt t)
(type-case Tree t
[leaf() 0]
[node(val left right) (+ (+ 1 (cnt left)) (cnt right))]))
(define (is-braun? t)
(type-case Tree t
[leaf() #t]
[node(val left right) (cond
[(equal? (is-braun? left) #f) #f]
[(equal? (is-braun? right) #f) #f]
[(< (cnt left) (cnt right)) #f]
[else #t])]))
(define (add-num n t)
(type-case Tree t
[leaf() t]
[node (val left right) (node (+ val n) (add-num n left) (add-num n right))]))
(define (make-sorted-braun n)
(cond
[(equal? 0 n) (node n (leaf) (leaf))]
[(equal? 1 n) (node n (node 0 (leaf) (leaf)) (leaf))]
[else
(cond
[(equal? 0 (remainder n 2))
(node (quotient n 2)
(make-sorted-braun (- (quotient n 2) 1))
(add-num (+ 1 (quotient n 2)) (make-sorted-braun (- (quotient n 2) 1)))
)]
[else (node (+ 1 (quotient n 2))
(make-sorted-braun (quotient n 2))
(add-num (+ 2 (quotient n 2)) (make-sorted-braun (- (quotient n 2) 1)))
)])]))
(print-only-errors)
(test (smallest (node 5 (leaf) (leaf)))
5.0)
(test (negate (node 5 (leaf) (leaf)))
(node -5 (leaf) (leaf)))
(test (contains? (node 5 (leaf) (leaf)) 6) #f)
(test (cnt (node 2
(node 1
(node 0 (leaf) (leaf))
(leaf))
(node 3 (leaf) (leaf))))
4)
(test (is-braun? (node 2
(node 1
(node 0 (leaf) (leaf))
(leaf))
(node 3 (leaf) (leaf))))
#t)
(test (sorted? (leaf))
#t)
(test (is-braun? (node 400
(node 300 (node 200
(node -100 (node -200
(leaf) (leaf))
(node -80 (node -90 (leaf) (leaf))
(node -60 (node -70 (leaf) (leaf))
(node -40 (node -50 (leaf) (leaf)) (leaf)))))
(node 250 (leaf) (leaf)))
(node 350 (leaf) (leaf)))
(node 500 (leaf) (leaf))))
#f)
(test (negate (leaf))
(leaf))
(test (make-sorted-braun 1)
(node 1 (node 0 (leaf) (leaf)) (leaf)))
(test (is-braun? (make-sorted-braun 500))
#t)
(test (sorted? (make-sorted-braun 100))
#t)