My favorites | Sign in
Project Home Downloads Wiki Issues Source
Repository:
Checkout   Browse   Changes   Clones    
 
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
\\
\ Copyright (c) 2011, Justin Grant <justin at imagine27 dot com> \
\ All rights reserved. \
\\
\ Redistribution and use in source and binary forms, with or without modification, \
\ are permitted provided that the following conditions are met: \
\\
\ Redistributions of source code must retain the above copyright notice, this list \
\ of conditions and the following disclaimer. \
\ Redistributions in binary form must reproduce the above copyright notice, this \
\ list of conditions and the following disclaimer in the documentation and/or \
\ other materials provided with the distribution. \
\ Neither the name of the <ORGANIZATION> nor the names of its contributors may be \
\ used to endorse or promote products derived from this software without specific \
\ prior written permission. \
\\
\ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND \
\ ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED \
\ WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE \
\ DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR \
\ ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES \
\ (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; \
\ LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY \
\ THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING \
\ NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, \
\ EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. \
\\


(tc +)

(datatype tree-node
Key : number; Val : B;
======================
[Key Val] : tree-node;)

(datatype color
if (element? Color [red black])
_______________________________
Color : color;)

(datatype tree
if (empty? Tree)
________________
Tree : tree;

Color : color; LTree : tree; TreeNode : tree-node; RTree : tree;
================================================================
[Color LTree TreeNode RTree] : tree;)

(define node-key
{tree-node --> number}
[Key Val] -> Key)

(define make-tree-black
{tree --> tree}
[Color A X B] -> [black A X B])

(define member
{tree-node --> tree --> boolean}
X NIL -> false
X [Color A Y B] -> (if (< (node-key X) (node-key Y))
(member X A)
(if (< (node-key Y) (node-key X))
(member X B)
true)))

(define balance
{tree --> tree}
[black [red [red A X B] Y C] Z D] -> [red [black A X B] Y [black C Z D]]
[black [red A X [red B Y C]] Z D] -> [red [black A X B] Y [black C Z D]]
[black A X [red [red B Y C] Z D]] -> [red [black A X B] Y [black C Z D]]
[black A X [red B Y [red C Z D]]] -> [red [black A X B] Y [black C Z D]]
S -> S)

(define insert-
{tree-node --> tree --> tree}
X [] -> [red [] X []]
X [Color A Y B] -> (if (< (node-key X) (node-key Y))
(balance [Color (insert- X A) Y B])
(if (< (node-key Y) (node-key X))
(balance [Color A Y (insert- X B)])
[Color A Y B])))

(define insert
{tree-node --> tree --> tree}
X S -> (make-tree-black (insert- X S)))

(define range
{ number --> number --> (list number) --> (list number) }
X Y Z -> (if (> X Y)
Z
(range X (- Y 1) (cons Y Z))))

(define insert-number-list-into-tree-
{ ((list number) * tree) --> ((list number) * tree) }
(@p Nums Tree) -> (if (empty? Nums)
(@p Nums Tree)
(insert-number-list-into-tree-
(@p (tail Nums)
(insert [(head Nums) (head Nums)] Tree)))))

(define insert-number-list-into-tree
{ (list number) --> tree --> tree }
Nums Tree -> (snd (insert-number-list-into-tree-
(@p Nums Tree))))


\ test functions \

(define run-tests
{ A --> A }
_ -> (do
(let Tree (insert-number-list-into-tree [11 14 15 2 7 1 5 8] [])
(do
(output "tree: ~A ~%12 is a member ? ~A~%8 is a member ? ~A~%"
Tree
(member [12 12] Tree)
(member [8 8] Tree))))
(let Count (round 1E5)
Nums (range 1 Count [])
(do
(output "~%Creating tree with ~A elements ..." Count)
(let Tree (time (insert-number-list-into-tree Nums []))
(do
(output "~%Performing lookups in tree with ~A elements ...~%" Count)
(time (output "666 in tree ? ~A" (member [666 666] Tree)))
(time (output "-1 in tree ? " (member [-1 -1] Tree)))))))
NIL))

(run-tests NIL)


\ tree: [black \
\ [red [black [red [] [1 1] []] [2 2] [red [] [5 5] []]] [7 7] \
\ [black [red [] [8 8] []] [11 11] []]] \
\ [14 14] [black [] [15 15] []]] \
\ 12 is a member ? false \
\ 8 is a member ? true \


\ output \

\ Creating tree with 100000 elements ... \
\ Evaluation took: \
\ 0.600 seconds of real time \
\ 0.577388 seconds of total run time (0.506373 user, 0.071015 system) \
\ [ Run times consist of 0.177 seconds GC time, and 0.401 seconds non-GC time. ] \
\ 96.17% CPU \
\ 1,254,956,000 processor cycles \
\ 168,580,336 bytes consed \
\ \
\ \
\ Performing lookups in tree with 100000 elements ... \
\ 666 in tree ? true \
\ Evaluation took: \
\ 0.000 seconds of real time \
\ 0.000033 seconds of total run time (0.000029 user, 0.000004 system) \
\ 100.00% CPU \
\ 64,176 processor cycles \
\ 0 bytes consed \
\ \
\ -1 in tree ? \
\ Evaluation took: \


\ (174-) tree: [black \
\ [red [black [red [] [1 1] []] [2 2] [red [] [5 5] []]] [7 7] \
\ [black [red [] [8 8] []] [11 11] []]] \
\ [14 14] [black [] [15 15] []]] \
\ 12 is a member ? false \
\ 8 is a member ? true \
\\
\ Creating tree with 100000 elements ... \
\ Evaluation took: \
\ 0.623 seconds of real time \
\ 0.607993 seconds of total run time (0.533755 user, 0.074238 system) \
\ [ Run times consist of 0.258 seconds GC time, and 0.350 seconds non-GC time. ] \
\ 97.59% CPU \
\ 994,122,776 processor cycles \
\ 92,299,840 bytes consed \


\ Performing lookups in tree with 100000 elements ... \
\ 666 in tree ? true \
\ Evaluation took: \
\ 0.000 seconds of real time \
\ 0.000050 seconds of total run time (0.000045 user, 0.000005 system) \
\ 100.00% CPU \
\ 74,160 processor cycles \
\ 32,768 bytes consed \

\ -1 in tree ? \
\ Evaluation took: \
\ 0.000 seconds of real time \
\ 0.000030 seconds of total run time (0.000025 user, 0.000005 system) \
\ 100.00% CPU \
\ 41,664 processor cycles \
\ 0 bytes consed \

\ [] \

\ (175-) true \
\ 0.000 seconds of real time \
\ 0.000027 seconds of total run time (0.000023 user, 0.000004 system) \
\ 100.00% CPU \
\ 50,610 processor cycles \
\ 0 bytes consed \
\ \
\ [] \
\ Evaluation took: \
\ 1.153 seconds of real time \
\ 1.006559 seconds of total run time (0.870915 user, 0.135644 system) \
\ [ Run times consist of 0.252 seconds GC time, and 0.755 seconds non-GC time. ] \
\ 87.34% CPU \
\ 187 forms interpreted \
\ 372 lambdas converted \
\ 2,414,154,330 processor cycles \
\ 210,133,312 bytes consed \
\ \
\ \
\ \
\ typechecked in 0 inferences \
\ loaded \

Change log

6e909cd7801b by Justin Grant jgrant27 on Jun 28, 2011   Diff
Turn on full type checking for Qi Red-
Black Trees example.
Go to: 
Sign in to write a code review

Older revisions

fae1dc41956d by Justin Grant jgrant27 on Jun 26, 2011   Diff
Fix comments in Qi source files.
83668d408476 by Justin Grant jgrant27 on Jun 26, 2011   Diff
Add some qi examples : PI and Red
Black Trees.
All revisions of this file

File info

Size: 7438 bytes, 224 lines
Powered by Google Project Hosting