/usr/lib/goo/mods/cols/treap.goo is in goo 0.155-14.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 | ;;
;; Copyright 2002 by Neelakantan Krishnaswami
;;
;; Permission is hereby granted, free of charge, to any person obtaining
;; a copy of this software and associated documentation files (the
;; "Software"), to deal in the Software without restriction, including
;; without limitation the rights to use, copy, modify, merge, publish,
;; distribute, sublicense, and/or sell copies of the Software, and to
;; permit persons to whom the Software is furnished to do so, subject to
;; the following conditions:
;;
;; The above copyright notice and this permission notice shall be
;; included in all copies or substantial portions of the Software.
;;
;; THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
;; EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
;; MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
;; IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY CLAIM, DAMAGES OR
;; OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
;; ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
;; OTHER DEALINGS IN THE SOFTWARE.
;;
;; ============
;; Author: Neel Krishnaswami <neelk@alum.mit.edu>
;; Date: 7-Aug-2002
;; Version 0.2
;;
;; A randomized treap is just about the simplest balanced binary tree
;; algorithm there is. Both insertion and deletion routines are a few
;; dozen lines, and it has excellent performance. Algorithmically,
;; they offer O(log n) average-case insertion, lookup and deletion.
;; In practice, they compare favorably even with more sophisticated
;; data structures like red-black trees or 2-3 trees, because they
;; have so little code that their constant factors are great.
;;
;; The implementation here is based on the algorithm described in
;; Aragon and Seidel's paper, "Randomized Search Trees". The Citeseer
;; link is <http://citeseer.nj.nec.com/seidel96randomized.html>.
;; Changes to 0.2 from 0.1
;; * Wrote some test routines
;; * Found a balance-related bug in add-key and fixed it.
;; * Added protocol for custom treaps
;; * Added a method for key-test, so it give the right equality function
;; * Fixed an argument transposition in remove-tree
;; IMPORTS
(use goo)
(use random) ;; This is just for testing -- <treap> is deterministic!
;; Usage notes:
;;
;; Some performance notes:
;;
;; add-key -- O(log n) expected time
;; del -- O(log n) expected time
;; elt -- O(log n) expected time
;; nul? -- O(1)
;; len -- O(n)
;; mem? -- O(n)
;;
;; The default <treap> uses '<' as the comparison routine. You can
;; replace it with a custom comparison by subclassing <treap> and
;; overriding 'treap-cmp'. Eg:
;;
;; (dc <my-treap> (<treap>))
;; (dm treap-cmp (t|<my-treap> => <fun>) my-cmp)
;;
;; Wishlist:
;;
;; 1. It would be nice to keep track of the number of successful
;; insertions in the <treap> record, so that we could offer an O(1)
;; 'len' method.
;;
;; 2. Clean up the error reporting. I just use 'error' to signal
;; any errors, because I don't what the condition hierarchy looks
;; like.
;;
;; 3. Some tests would be nice. I wrote this in an afternoon and a
;; bit, testing at the command prompt. Goo is still new enough that
;; it's possible that things I thought were goo errors are actually
;; my bugs.
;;
;; Internal implementation of binary trees. The only unusual one is
;; the 'pri' field. It holds a bunch of random numbers, and the
;; insertion/deletion are constrained to make sure that the pri fields
;; are in heap order. This is what makes randomized treaps balance.
(dc <tree> (<any>))
(dc <tree-nil> (<tree>))
(dc <tree-node> (<tree>))
(dp lft (<tree-node> => <tree>))
(dp pri (<tree-node> => <int>))
(dp key (<tree-node> => <any>))
(dp val (<tree-node> => <any>))
(dp rgt (<tree-node> => <tree>))
(df tree-node (l|<tree> n|<int> k|<any> v|<any> r|<tree> => <tree-node>)
(new <tree-node> lft l pri n key k val v rgt r))
(dv tree-nil (new <tree-nil>))
(dc <treap> (<map> <col.>))
(dp seed (<treap> => <int>))
(dp tree (<treap> => <tree>))
;; We immediately implement the collection protocol, because goo will
;; crash if we try to build the nul collection without these methods
;; existing. That's why they aren't down with the rest of the <col>
;; protocol implementation.
(dc <treap-enum> (<enum>))
(dp tree-list (<treap-enum> => <lst>))
(dm enum (x|<treap> => <treap-enum>)
(def lst (if (== tree-nil (tree x)) '() (list (tree x))))
(new <treap-enum> tree-list lst))
(dm fin? (x|<treap-enum> => <log>)
(nul? (tree-list x)))
(dm nxt (x|<treap-enum> => <treap-enum>)
(def lst (tree-list x))
(def l (lft (head lst)))
(def r (rgt (head lst)))
(def next-lst (tail lst))
(def next-lst (if (== r tree-nil) next-lst (pair r next-lst)))
(def next-lst (if (== l tree-nil) next-lst (pair l next-lst)))
(new <treap-enum> tree-list next-lst))
(dm now (x|<treap-enum> => <any>)
(val (head (tree-list x))))
(dm now-key (x|<treap-enum> => <any>)
(key (head (tree-list x))))
;; Here's a small linear congruential rng. The two properties that
;; make it suitable in this case are a) it's frigging fast, and b) it
;; doesn't repeat.
;;
;; This rng probably isn't all that hot if you have small <treaps>,
;; because there's a decent chance of getting short increasing runs.
;; That would reduce the balanced-ness of small collections....
(dv shift (pow 2 26))
(df rand (x|<int> => <int>)
(mod (+ 1 (* 5 x)) shift))
;; treap-cmp
;; Now we add a method to get the treap's comparison method. The
;; default method uses the generic < comparison operator. Subclasses
;; should override this method.
(dm treap-cmp (t|<treap> => <fun>)
<)
;; Now let's define the unique nul treap. Down below it will
;; get wrapped around the 'nul' method, but internally it
;; gets used directly.
(dv nul-treap (new <treap> seed 48637761 tree tree-nil))
;; rot-l and rot-r stand for 'rotate left' and 'rotate right'. These
;; are pretty standard binary tree manipulations, though to truly
;; understand them, you need a nifty little diagram, which ASCII can't
;; do.
(df rot-r (t|<tree-node> => <tree-node>)
(let ((a (lft (lft t)))
(n (pri (lft t)))
(xk (key (lft t)))
(xv (val (lft t)))
(b (rgt (lft t)))
(m (pri t))
(yk (key t))
(yv (val t))
(c (rgt t)))
(tree-node a n xk xv (tree-node b m yk yv c))))
(df rot-l (t|<tree-node> => <tree-node>)
(let ((a (lft t))
(n (pri t))
(xk (key t))
(xv (val t))
(b (lft (rgt t)))
(m (pri (rgt t)))
(yk (key (rgt t)))
(yv (val (rgt t)))
(c (rgt (rgt t))))
(tree-node (tree-node a n xk xv b) m yk yv c)))
;; Now we can define the heart of this library, the add-tree and
;; remove-tree methods, which insert and remove elements into a tree
;; while preserving the balance conditions. The basic idea behind
;; inserts is to insert the element as for unbalanced trees, and then
;; take a random number (that's p) for the new node's priority. Then
;; you do the heap-ordering shuffle (which takes log n time), and you
;; statistically likely to have a balanced tree. Tres cool.
(dm add-tree (t|<tree-nil> p|<int> k|<any> v|<any> <|<fun> => <tree-node>)
(tree-node tree-nil p k v tree-nil))
(dm add-tree (t|<tree-node> p-new|<int> k-new|<any> v-new|<any> <|<fun>
=> <tree-node>)
(if (< k-new (key t))
(let ((l-new (add-tree (lft t) p-new k-new v-new <)))
(let ((t* (tree-node l-new (pri t) (key t) (val t) (rgt t))))
(if (< (pri t) (pri l-new))
(rot-r t*)
t*)))
(if (< (key t) k-new)
(let ((r-new (add-tree (rgt t) p-new k-new v-new <)))
(let ((t* (tree-node (lft t) (pri t) (key t) (val t) r-new)))
(if (< (pri t) (pri r-new))
(rot-l t*)
t*)))
;; (tree-node (lft t) p-new k-new v-new (rgt t)))))
(tree-node (lft t) (pri t) k-new v-new (rgt t)))))
;; Deletion routine. It's short, but you should look at Seidel &
;; Aragon for details. To understand this you really need the picture.
(dm remove-root (t|<tree-nil> => <tree>)
(error "remove-tree; key not in tree!"))
(dm remove-root (t|<tree-node> => <tree>)
(def l* (lft t))
(def k* (key t))
(def v* (val t))
(def p* (pri t))
(def r* (rgt t))
(cond
( (== l* tree-nil) r*)
( (== r* tree-nil) l*)
( t
(if (< (pri r*) (pri l*))
(seq
(def t* (rot-r t))
(def r** (remove-root (rgt t*)))
(tree-node (lft t*) (pri t*) (key t*) (val t*) r**))
(seq
(def t* (rot-l t))
(def l** (remove-root (lft t*)))
(tree-node l** (pri t*) (key t*) (val t*) (rgt t*)))))))
(dm remove-tree (t|<tree-nil> k|<any> <|<fun> => <tree>)
(error "remove-tree: key not in tree!" ))
(dm remove-tree (t|<tree-node> k|<any> <|<fun> => <tree>)
(def l* (lft t))
(def k* (key t))
(def v* (val t))
(def p* (pri t))
(def r* (rgt t))
(cond
( (< k k*)
(tree-node (remove-tree l* k <) p* k* v* r*))
( (< k* k)
(tree-node l* p* k* v* (remove-tree r* k <)))
( #t
(remove-root t))))
;; The usual accessors.
;; add-key adds a binding to a treap
(dm add-key (t|<treap> k|<any> v|<any> => <treap>)
(let ((p (rand (seed t))))
(new <treap> tree (add-tree (tree t) p k v (treap-cmp t)) seed p)))
;; del removes a binding.
(dm del (t|<treap> k|<any> => <treap>)
(new <treap> tree (remove-tree (tree t) k (treap-cmp t)) seed (seed t)))
;; elt does a lookup, and cow goes moo.
(dm elt (t|<treap> k|<any> => <any>)
(let ((< (treap-cmp t)))
(loc ((search (t|<tree> => <any>)
(cond
( (< k (key t))
(search (lft t)))
( (< (key t) k)
(search (rgt t)))
(#t
(val t)))))
(search (tree t)))))
;; nul is the Goo-standard interface, so we'll support that.
(dm nul (t|(t= <treap>) => <treap>)
nul-treap)
;; col is the standard collection constructor.
(dm col (t|(t= <treap>) key-vals|... => <treap>)
(def n (len key-vals))
(unless (even? n)
(error "col: unbalanced key/val pairs"))
(loc ((loop (i|<int> new-treap|<treap> => <treap>)
(if (< i (1st (trunc/ n 2)))
(seq
(def k (elt key-vals (* 2 i)))
(def v (elt key-vals (+ 1 (* 2 i))))
(loop (+ i 1) (add-key new-treap k v)))
new-treap)))
(loop 0 nul-treap)))
;; key-test -- we override key-test so that the enumeration methods
;; will do the right things.
(dm key-test (t|<treap> => <fun>)
(let ((< (treap-cmp t)))
(fun (a|<any> b|<any> => <log>)
(and (< a b) (< b a)))))
;;
;; Some simple tests to check that the balancing routines work. To use
;; the tests
;;
(dm binary-tree? (t|<tree-nil> <|<fun> => <log>)
#t)
(dm binary-tree? (t|<tree-node> <|<fun> => <log>)
(def l (lft t))
(def r (rgt t))
(cond
( (and (== l tree-nil) (== r tree-nil))
#t)
( (== l tree-nil)
(and (< (key t) (key r)) (binary-tree? r <)))
( (== r tree-nil)
(and (< (key l) (key t)) (binary-tree? l <)))
( #t
(and (< (key l) (key t))
(< (key t) (key r))
(binary-tree? l <)
(binary-tree? r <)))))
(dm heap? (t|<tree-nil> => <log>)
#t)
(dm heap? (t|<tree-node> => <log>)
(def l (lft t))
(def r (rgt t))
(cond
( (and (== l tree-nil) (== r tree-nil))
#t)
( (== l tree-nil)
(and (> (pri t) (pri r)) (heap? r)))
( (== r tree-nil)
(and (> (pri t) (pri l)) (heap? l)))
( #t
(and (> (pri t) (pri r))
(> (pri t) (pri l))
(heap? l)
(heap? r)))))
(df treap-test (t|<treap> => <log>)
(def h? (heap? (tree t)))
(def b? (binary-tree? (tree t) (treap-cmp t)))
(if h?
(msg out "Passed heap-ness check\n")
(msg out "FAILED heap-ness check\n"))
(if b?
(msg out "Passed binary-tree-ness check\n")
(msg out "FAILED binary-tree-ness check\n"))
(and h? b?))
;; random-treap builds a treap with randomly chosen keys. This makes
;; it convenient to write torture tests for insertion.
(df random-treap (n m)
(fold (fun (acc i) (add-key acc (random m) #f))
(nul <treap>)
(below n)))
;; To support printf-style debugging.
(dm print-tree (t|<treap>)
(print-tree (tree t)))
(dm print-tree (t|<tree-nil>)
(msg out "nil"))
(dm print-tree (t|<tree-node>)
(msg out "(")
(msg out "lft: ") (print-tree (lft t))
(msg out " pri: %=" (pri t))
(msg out " key: %=" (key t))
(msg out " val: %=" (val t))
(msg out " rgt: ")
(print-tree (rgt t))
(msg out ")"))
;; EXPORTS
;;
;; We only export 3 names, because we mostly just override existing
;; collection protocols. jrb has sure found a comfortable set of
;; collection routines.
(export <treap>)
(export add-key) ;; for <map>s parallel to <add>.
(export treap-cmp)
;; Exports for testing
(export treap-test)
(export random-treap)
(export print-tree)
|