search-tree.scm 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442
  1. ; Copyright (c) 1993-2007 by Richard Kelsey and Jonathan Rees. See file COPYING.
  2. ; Red-Black binary search trees as described in Introduction to Algorithms
  3. ; by Cormen, Leiserson, and Rivest. Look there if you want to understand
  4. ; the algorithm.
  5. ;
  6. ; These are like tables in that the value of a key defaults to #f.
  7. ;
  8. ; (make-search-tree key-= key-<) -> tree
  9. ;
  10. ; (search-tree? value) -> boolean
  11. ;
  12. ; (search-tree-ref tree key) -> value
  13. ;
  14. ; (search-tree-set! tree key value)
  15. ;
  16. ; (search-tree-modify! tree key proc)
  17. ; == (search-tree-set! tree key (proc (search-tree-ref tree key)))
  18. ;
  19. ; (search-tree-max tree) -> key + value
  20. ; (pop-search-tree-max! tree) -> key + value (removes entry)
  21. ;
  22. ; (search-tree-min tree) -> key + value
  23. ; (pop-search-tree-min! tree) -> key + value (removes entry)
  24. ;
  25. ; (walk-search-tree proc tree)
  26. ; applies PROC in order to all key + value pairs with a non-#f value
  27. (define-record-type tree :tree
  28. (make-tree lookup nil root)
  29. search-tree?
  30. (lookup tree-lookup)
  31. (nil tree-nil) ; node marker for missing leaf nodes
  32. (root tree-root set-tree-root!))
  33. (define (make-search-tree = <)
  34. (let ((nil (make-node #f #f #f)))
  35. (set-node-red?! nil #f)
  36. (make-tree (make-lookup = <) nil #f)))
  37. (define-record-type node :node
  38. (really-make-node key value parent red? left right)
  39. node?
  40. (key node-key set-node-key!)
  41. (value node-value set-node-value!)
  42. (parent node-parent set-node-parent!) ; #f in the root node
  43. (red? node-red? set-node-red?!) ; for balancing the tree
  44. (left node-left set-node-left!) ; left and
  45. (right node-right set-node-right!)) ; right subtrees
  46. (define (make-node key value parent)
  47. (really-make-node key value parent #t #f #f))
  48. (define-record-discloser :node
  49. (lambda (node)
  50. (list 'node (node-key node))))
  51. ; Lookup up KEY and return its value.
  52. (define (search-tree-ref tree key)
  53. (call-with-values
  54. (lambda ()
  55. ((tree-lookup tree) tree key))
  56. (lambda (node parent left?)
  57. (if node
  58. (node-value node)
  59. #f))))
  60. ; Adding and modifying entries.
  61. (define (search-tree-set! tree key value)
  62. (search-tree-modify! tree key (lambda (ignore) value)))
  63. (define (search-tree-modify! tree key proc)
  64. (call-with-values
  65. (lambda ()
  66. ((tree-lookup tree) tree key))
  67. (lambda (node parent left?)
  68. (let ((new-value (proc (if node (node-value node) #f))))
  69. (cond ((and node new-value)
  70. (set-node-value! node new-value))
  71. (new-value
  72. (really-insert! tree parent left? (make-node key new-value parent)))
  73. (node
  74. (really-delete! tree node)))))))
  75. ; Min and max entries.
  76. (define (search-tree-max tree)
  77. (real-search-tree-max tree #f))
  78. (define (pop-search-tree-max! tree)
  79. (real-search-tree-max tree #t))
  80. (define (real-search-tree-max tree delete?)
  81. (let ((node (tree-root tree)))
  82. (if node
  83. (let loop ((node node))
  84. (cond ((node-right node)
  85. => loop)
  86. (else
  87. (if delete?
  88. (really-delete! tree node))
  89. (values (node-key node) (node-value node)))))
  90. (values #f #f))))
  91. (define (search-tree-min tree)
  92. (real-search-tree-min tree #f))
  93. (define (pop-search-tree-min! tree)
  94. (real-search-tree-min tree #t))
  95. (define (real-search-tree-min tree delete?)
  96. (let ((node (tree-root tree)))
  97. (if node
  98. (let loop ((node node))
  99. (cond ((node-left node)
  100. => loop)
  101. (else
  102. (if delete?
  103. (really-delete! tree node))
  104. (values (node-key node) (node-value node)))))
  105. (values #f #f))))
  106. (define (walk-search-tree proc tree)
  107. (let recur ((node (tree-root tree)))
  108. (cond (node
  109. (recur (node-left node))
  110. (proc (node-key node) (node-value node))
  111. (recur (node-right node))))))
  112. ; Lookup up an entry. Easy.
  113. ;
  114. ; Hack of checking common case reduced lookup time in a 1000 element search
  115. ; tree by a third.
  116. (define (make-lookup tree-= tree-<)
  117. (if (and (eq? tree-= =)
  118. (eq? tree-< <))
  119. default-lookup
  120. (lambda (tree key)
  121. (let loop ((node (tree-root tree))
  122. (parent #f)
  123. (left? #f))
  124. (cond ((not node)
  125. (values #f parent left?))
  126. ((tree-= (node-key node) key)
  127. (values node #f #f))
  128. ((tree-< key (node-key node))
  129. (loop (node-left node) node #t))
  130. (else
  131. (loop (node-right node) node #f)))))))
  132. (define (default-lookup tree key)
  133. (let loop ((node (tree-root tree))
  134. (parent #f)
  135. (left? #f))
  136. (cond ((not node)
  137. (values #f parent left?))
  138. ((= (node-key node) key)
  139. (values node #f #f))
  140. ((< key (node-key node))
  141. (loop (node-left node) node #t))
  142. (else
  143. (loop (node-right node) node #f)))))
  144. ;----------------------------------------------------------------
  145. ; Little utilities.
  146. ; Parameterized node access
  147. (define (node-child node left?)
  148. (if left?
  149. (node-left node)
  150. (node-right node)))
  151. (define (set-node-child! node left? child)
  152. (if left?
  153. (set-node-left! node child)
  154. (set-node-right! node child)))
  155. ; Empty leaf slots are considered black.
  156. (define (node-black? node)
  157. (not (and node (node-red? node))))
  158. ; The next node (used in REALLY-DELETE!)
  159. (define (successor node)
  160. (cond ((node-right node)
  161. => (lambda (node)
  162. (let loop ((node node))
  163. (cond ((node-left node)
  164. => loop)
  165. (else node)))))
  166. (else
  167. (let loop ((node node) (parent (node-parent node)))
  168. (if (and parent
  169. (eq? node (node-right parent)))
  170. (loop parent (node-parent parent))
  171. parent)))))
  172. ;----------------------------------------------------------------
  173. ; Add NODE as the LEFT? child of PARENT and balance the tree.
  174. (define (really-insert! tree parent left? node)
  175. (if (not parent)
  176. (set-tree-root! tree node)
  177. (set-node-child! parent left? node))
  178. (fixup-insertion! node tree))
  179. ; Balance the tree after NODE has been inserted.
  180. (define (fixup-insertion! node tree)
  181. (let loop ((node node))
  182. (let ((parent (node-parent node)))
  183. (if (and parent (node-red? parent))
  184. (let* ((grand (node-parent parent))
  185. (left? (eq? parent (node-left grand)))
  186. (y (node-child grand (not left?))))
  187. (cond ((node-black? y)
  188. (let* ((node (cond ((eq? node (node-child parent (not left?)))
  189. (rotate! parent left? tree)
  190. parent)
  191. (else node)))
  192. (parent (node-parent node))
  193. (grand (node-parent parent)))
  194. (set-node-red?! parent #f)
  195. (set-node-red?! grand #t)
  196. (rotate! grand (not left?) tree)
  197. (loop node)))
  198. (else
  199. (set-node-red?! parent #f)
  200. (set-node-red?! y #f)
  201. (set-node-red?! grand #t)
  202. (loop grand)))))))
  203. (set-node-red?! (tree-root tree) #f))
  204. ; A B
  205. ; / \ =(rotate! A #f tree)=> / \
  206. ; B k i A
  207. ; / \ <=(rotate! B #t tree)= / \
  208. ; i j j k
  209. (define (rotate! node left? tree)
  210. (let* ((y (node-child node (not left?)))
  211. (y-left (node-child y left?))
  212. (parent (node-parent node)))
  213. (set-node-child! node (not left?) y-left)
  214. (if y-left
  215. (set-node-parent! y-left node))
  216. (replace! parent y node tree)
  217. (set-node-child! y left? node)
  218. (set-node-parent! node y)))
  219. ; Replace CHILD (of PARENT) with NEW-CHILD
  220. (define (replace! parent new-child child tree)
  221. (set-node-parent! new-child parent)
  222. (cond ((eq? child (tree-root tree))
  223. (set-tree-root! tree new-child))
  224. ((eq? child (node-left parent))
  225. (set-node-left! parent new-child))
  226. (else
  227. (set-node-right! parent new-child))))
  228. ; Remove NODE from tree.
  229. (define (really-delete! tree node)
  230. (let* ((y (cond ((or (not (node-left node))
  231. (not (node-right node)))
  232. node)
  233. (else
  234. (let ((y (successor node)))
  235. (set-node-key! node (node-key y))
  236. (set-node-value! node (node-value y))
  237. y))))
  238. (x (or (node-left y)
  239. (node-right y)
  240. (let ((x (tree-nil tree)))
  241. (set-node-right! y x)
  242. x)))
  243. (parent (node-parent y)))
  244. (replace! parent x y tree)
  245. (if (not (node-red? y))
  246. (fixup-delete! x tree))
  247. (let ((nil (tree-nil tree)))
  248. (cond ((node-parent nil)
  249. => (lambda (p)
  250. (if (eq? (node-right p) nil)
  251. (set-node-right! p #f)
  252. (set-node-left! p #f))
  253. (set-node-parent! (tree-nil tree) #f)))
  254. ((eq? nil (tree-root tree))
  255. (set-tree-root! tree #f))))))
  256. (define (fixup-delete! x tree)
  257. (let loop ((x x))
  258. (if (or (eq? x (tree-root tree))
  259. (node-red? x))
  260. (set-node-red?! x #f)
  261. (let* ((parent (node-parent x))
  262. (left? (eq? x (node-left parent)))
  263. (w (node-child parent (not left?)))
  264. (w (cond ((node-red? w)
  265. (set-node-red?! w #f)
  266. (set-node-red?! parent #t)
  267. (rotate! parent left? tree)
  268. (node-child (node-parent x) (not left?)))
  269. (else
  270. w))))
  271. (cond ((and (node-black? (node-left w))
  272. (node-black? (node-right w)))
  273. (set-node-red?! w #t)
  274. (loop (node-parent x)))
  275. (else
  276. (let ((w (cond ((node-black? (node-child w (not left?)))
  277. (set-node-red?! (node-child w left?) #f)
  278. (set-node-red?! w #t)
  279. (rotate! w (not left?) tree)
  280. (node-child (node-parent x) (not left?)))
  281. (else
  282. w))))
  283. (let ((parent (node-parent x)))
  284. (set-node-red?! w (node-red? parent))
  285. (set-node-red?! parent #f)
  286. (set-node-red?! (node-child w (not left?)) #f)
  287. (rotate! parent left? tree)
  288. (set-node-red?! (tree-root tree) #f)))))))))
  289. ; Verify that the coloring is correct
  290. ;
  291. ;(define (okay-tree? tree)
  292. ; (receive (okay? red? count)
  293. ; (let recur ((node (tree-root tree)))
  294. ; (if (not node)
  295. ; (values #t #f 0)
  296. ; (receive (l-ok? l-r? l-c)
  297. ; (recur (node-left node))
  298. ; (receive (r-ok? r-r? r-c)
  299. ; (recur (node-right node))
  300. ; (values (and l-ok?
  301. ; r-ok?
  302. ; (not (and (node-red? node)
  303. ; (or l-r? r-r?)))
  304. ; (= l-c r-c))
  305. ; (node-red? node)
  306. ; (if (node-red? node)
  307. ; l-c
  308. ; (+ l-c 1)))))))
  309. ; okay?))
  310. ;
  311. ;
  312. ;(define (walk-sequences proc list)
  313. ; (let recur ((list list) (r '()))
  314. ; (if (null? list)
  315. ; (proc (reverse r))
  316. ; (let loop ((list list) (done '()))
  317. ; (if (not (null? list))
  318. ; (let ((next (car list)))
  319. ; (recur (append (reverse done) (cdr list)) (cons next r))
  320. ; (loop (cdr list) (cons next done))))))))
  321. ;
  322. ;(define (tree-test n)
  323. ; (let ((iota (do ((i n (- i 1))
  324. ; (l '() (cons i l)))
  325. ; ((<= i 0) l))))
  326. ; (walk-sequences (lambda (in)
  327. ; (walk-sequences (lambda (out)
  328. ; (do-tree-test in out))
  329. ; iota))
  330. ; iota)
  331. ; #t))
  332. ;
  333. ;(define (do-tree-test in out)
  334. ; (let ((tree (make-search-tree = <)))
  335. ; (for-each (lambda (i)
  336. ; (search-tree-set! tree i (- 0 i)))
  337. ; in)
  338. ; (if (not (okay-tree? tree))
  339. ; (breakpoint "tree ~S is not okay" in))
  340. ; (if (not (tree-ordered? tree (length in)))
  341. ; (breakpoint "tree ~S is not ordered" in))
  342. ; (for-each (lambda (i)
  343. ; (if (not (= (search-tree-ref tree i) (- 0 i)))
  344. ; (breakpoint "looking up ~S in ~S lost" i in)))
  345. ; in)
  346. ; (do ((o out (cdr o)))
  347. ; ((null? o))
  348. ; (search-tree-set! tree (car o) #f)
  349. ; (if (not (okay-tree? tree))
  350. ; (breakpoint "tree ~S is not okay after deletions ~S" in out)))))
  351. ;
  352. ;(define (tree-ordered? tree count)
  353. ; (let ((l '()))
  354. ; (walk-search-tree (lambda (key value)
  355. ; (set! l (cons (cons key value) l)))
  356. ; tree)
  357. ; (let loop ((l l) (n count))
  358. ; (cond ((null? l)
  359. ; (= n 0))
  360. ; ((and (= (caar l) n)
  361. ; (= (cdar l) (- 0 n)))
  362. ; (loop (cdr l) (- n 1)))
  363. ; (else #f)))))
  364. ;
  365. ;(define (do-tests tester)
  366. ; (do ((i 0 (+ i 1)))
  367. ; (#f)
  368. ; (tester i)
  369. ; (format #t " done with ~D~%" i)))
  370. ;
  371. ;(define (another-test n)
  372. ; (let ((iota (do ((i n (- i 1))
  373. ; (l '() (cons i l)))
  374. ; ((<= i 0) l))))
  375. ; (walk-sequences (lambda (in)
  376. ; (do ((i 1 (+ i 1)))
  377. ; ((> i n))
  378. ; (let ((tree (make-search-tree = <)))
  379. ; (for-each (lambda (i)
  380. ; (search-tree-set! tree i (- 0 i)))
  381. ; in)
  382. ; (if (not (okay-tree? tree))
  383. ; (breakpoint "tree ~S is not okay" in))
  384. ; (if (not (tree-ordered? tree (length in)))
  385. ; (breakpoint "tree ~S is not ordered" in))
  386. ; (for-each (lambda (i)
  387. ; (if (not (= (search-tree-ref tree i) (- 0 i)))
  388. ; (breakpoint "looking up ~S in ~S lost" i in)))
  389. ; in)
  390. ; (search-tree-set! tree i #f)
  391. ; (if (not (okay-tree? tree))
  392. ; (breakpoint "tree ~S is not okay after deletion ~S"
  393. ; in i))
  394. ; (for-each (lambda (j)
  395. ; (let ((ref (search-tree-ref tree j)))
  396. ; (if (not (eq? ref (if (= j i) #f (- 0 j))))
  397. ; (breakpoint "looking up ~S in ~S lost" i in))))
  398. ; in))))
  399. ; iota)))