4 Splay Trees
(require data/splay-tree) | package: data-lib |
Splay trees are an efficient data structure for mutable dictionaries with totally ordered keys. They were described in the paper “Self-Adjusting Binary Search Trees” by Daniel Sleator and Robert Tarjan in Journal of the ACM 32(3) pp652-686.
A splay-tree is a ordered dictionary (dict? and ordered-dict?).
Operations on splay-trees are not thread-safe. If a key in a splay-tree is mutated, the splay-tree’s internal invariants may be violated, causing its behavior to become unpredictable.
procedure
(make-splay-tree [ ord #:key-contract key-contract #:value-contract value-contract]) → splay-tree? ord : order? = datum-order key-contract : contract? = any/c value-contract : contract? = any/c
> (define splay-tree (make-splay-tree (order 'string-order string? string=? string<?))) > (splay-tree-set! splay-tree "dot" 10) > (splay-tree-set! splay-tree "cherry" 500) > (dict-map splay-tree list) '(("cherry" 500) ("dot" 10))
> (splay-tree-ref splay-tree "dot") 10
> (splay-tree-remove! splay-tree "cherry") > (splay-tree-count splay-tree) 1
> (splay-tree-set! splay-tree 'pear 3) splay-tree-set!: contract violation
expected: string?
given: 'pear
in: the key argument of
(->i
((s splay-tree?)
(key (s) (key-c s))
(v (s) (val-c s)))
(_r void?))
contract from:
<pkgs>/data-lib/data/splay-tree.rkt
blaming: top-level
(assuming the contract is correct)
at: <pkgs>/data-lib/data/splay-tree.rkt:609:2
procedure
(make-adjustable-splay-tree [ #:key-contract key-contract #:value-contract value-contract]) → splay-tree? key-contract : contract? = any/c value-contract : contract? = any/c
> (define splay-tree (make-adjustable-splay-tree)) > (splay-tree-set! splay-tree 3 'apple) > (splay-tree-set! splay-tree 6 'cherry) > (dict-map splay-tree list) '((3 apple) (6 cherry))
> (splay-tree-ref splay-tree 3) 'apple
> (splay-tree-remove! splay-tree 6) > (splay-tree-count splay-tree) 1
procedure
(splay-tree? x) → boolean?
x : any/c
procedure
x : any/c
procedure
(splay-tree-ref s key [default]) → any
s : splay-tree? key : any/c default : any/c = (lambda () (error ....))
procedure
(splay-tree-set! s key value) → void?
s : splay-tree? key : any/c value : any/c
procedure
(splay-tree-remove! s key) → void?
s : splay-tree? key : any/c
procedure
s : splay-tree?
procedure
(splay-tree-iterate-first s) → (or/c #f splay-tree-iter?)
s : splay-tree?
procedure
(splay-tree-iterate-next s iter) → (or/c #f splay-tree-iter?)
s : splay-tree? iter : splay-tree-iter?
procedure
(splay-tree-iterate-key s iter) → any/c
s : splay-tree? iter : splay-tree-iter?
procedure
(splay-tree-iterate-value s iter) → any/c
s : splay-tree? iter : splay-tree-iter?
procedure
(splay-tree-remove-range! s from to) → void?
s : splay-tree? from : any/c to : any/c
This operation takes O(N) time, or O(log N) time if (adjustable-splay-tree? s).
procedure
(splay-tree-contract! s from to) → void?
s : adjustable-splay-tree? from : exact-integer? to : exact-integer?
This operation is only allowed on adjustable splay trees, and it takes O(log N) time.
procedure
(splay-tree-expand! s from to) → void?
s : adjustable-splay-tree? from : exact-integer? to : exact-integer?
This operation is only allowed on adjustable splay trees, and it takes O(log N) time.
procedure
(splay-tree-iterate-least s) → (or/c #f splay-tree-iter?)
s : splay-tree
procedure
(splay-tree-iterate-greatest s) → (or/c #f splay-tree-iter?)
s : splay-tree
procedure
(splay-tree-iterate-least/>? s key) → (or/c #f splay-tree-iter?)
s : splay-tree? key : any/c
procedure
(splay-tree-iterate-least/>=? s key)
→ (or/c #f splay-tree-iter?) s : splay-tree? key : any/c
procedure
(splay-tree-iterate-greatest/<? s key)
→ (or/c #f splay-tree-iter?) s : splay-tree? key : any/c
procedure
(splay-tree-iterate-greatest/<=? s key)
→ (or/c #f splay-tree-iter?) s : splay-tree? key : any/c
procedure
(splay-tree-iter? x) → boolean?
x : any/c
procedure
(splay-tree->list s) → (listof pair?)
s : splay-tree?