8 Red-Black Trees
(require (planet krhari/pfds:1:2/redblacktrees)) |
No red node has a red child.
Every path from root to an empty node has the same number of black nodes.
(RedBlackTree A) |
(redblacktree comp a ...) → (RedBlackTree A) |
comp : (A A -> Boolean) |
a : A |
Example: |
> (redblacktree < 1 2 3 4 5) |
- : (RBTree Positive-Fixnum) |
#<RBTree> |
In the above example, the red black tree obtained will have 2 as its root and < as the comparison function.
(empty? rbt) → Boolean |
rbt : (RedBlackTree A) |
Examples: |
> (empty? (redblacktree < 1 2 3 4 5 6)) |
- : Boolean |
#f |
> (empty? (redblacktree <)) |
- : Boolean |
#t |
(insert a rbt) → (RedBlackTree A) |
a : A |
rbt : (RedBlackTree A) |
Example: |
> (insert 10 (redblacktree < 1 2 3 4 5 6)) |
- : (RBTree Positive-Fixnum) |
#<RBTree> |
In the above example, insert adds the 10 to (redblacktree < 1 2 3 4 5 6).
(root rbt) → A |
rbt : (RedBlackTree A) |
Examples: |
> (root (redblacktree < 1 2 3 4 5 6)) |
- : Positive-Fixnum |
2 |
> (root (redblacktree <)) |
root: given tree is empty |
In the above example, (root (redblacktree < 1 2 3 4 5 6)), returns 2 which is the root of (redblacktree < 1 2 3 4 5 6).
(member? rbt) → Boolean |
rbt : (RedBlackTree A) |
Examples: |
> (member? 5 (redblacktree < 1 2 3 4 5 6)) |
- : Boolean |
#t |
> (member? 10 (redblacktree < 1 2 3 4 5 6)) |
- : Boolean |
#f |
(delete-root rbt) → (RedBlackTree A) |
rbt : (RedBlackTree A) |
Examples: |
> (delete-root (redblacktree < 1 2 3 4 5 6)) |
- : (RBTree Positive-Fixnum) |
#<RBTree> |
> (delete-root (redblacktree <)) |
delete-root: given tree is empty |
In the above example, (delete-root rbtree), delete the root of (redblacktree < 1 2 3 4 5 6) which happens to be 2 in this tree.
(delete elem rbt) → (RedBlackTree A) |
elem : A |
rbt : (RedBlackTree A) |
Examples: |
> (delete 5 (redblacktree < 1 2 3 4 5 6)) |
- : (RBTree Positive-Fixnum) |
#<RBTree> |
> (delete 10 (redblacktree < 1 2 3 4 5 6)) |
delete: given key not found in the tree |
In the above example, (delete 5 (redblacktree < 1 2 3 4 5 6)), deletes 5 in (redblacktree < 1 2 3 4 5 6).
(redblacktree->list rbt) → (Listof A) |
rbt : (RedBlackTree A) |
Example: |
> (redblacktree->list (redblacktree > 1 2 3 4 5)) |
- : (Listof Positive-Fixnum) |
'(2 4 3 5 1) |
(map comparer func rbt1 rbt2 ...) → (RedBlackTree A) |
comparer : (C C -> Boolean) |
func : (A B ... B -> C) |
rbt1 : (RedBlackTree A) |
rbt2 : (RedBlackTree B) |
Examples: | ||
> (redblacktree->list (map < add1 (redblacktree < 1 2 3 4 5 6))) | ||
- : (Listof Exact-Positive-Integer) | ||
'(4 6 3 5 2 7) | ||
| ||
- : (Listof Exact-Positive-Integer) | ||
'(9 25 4 16 1 36) |
(fold func init rbt1 rbt2 ...) → C |
func : (C A B ... B -> C) |
init : C |
rbt1 : (RedBlackTree A) |
rbt2 : (RedBlackTree B) |
fold currently does not produce correct results when the given function is non-commutative.
Examples: |
> (fold + 0 (redblacktree < 1 2 3 4 5 6)) |
- : Exact-Nonnegative-Integer |
21 |
> (fold * 1 (redblacktree < 1 2 3 4 5 6) (redblacktree < 1 2 3 4 5 6)) |
- : Exact-Positive-Integer |
518400 |
(filter func rbt) → (RedBlackTree A) |
func : (A -> Boolean) |
rbt : (RedBlackTree A) |
Examples: |
> (define rbt (redblacktree < 1 2 3 4 5 6)) |
> (redblacktree->list (filter (λ: ([x : Integer]) (> x 5)) rbt)) |
- : (Listof Positive-Fixnum) |
'(6) |
> (redblacktree->list (filter (λ: ([x : Integer]) (< x 5)) rbt)) |
- : (Listof Positive-Fixnum) |
'(3 2 1 4) |
> (redblacktree->list (filter (λ: ([x : Integer]) (<= x 5)) rbt)) |
- : (Listof Positive-Fixnum) |
'(3 2 4 1 5) |
(remove func rbt) → (RedBlackTree A) |
func : (A -> Boolean) |
rbt : (RedBlackTree A) |
Examples: | ||
| ||
- : (Listof Positive-Fixnum) | ||
'(3 2 4 1 5) | ||
| ||
- : (Listof Positive-Fixnum) | ||
'(5 6) | ||
| ||
- : (Listof Positive-Fixnum) | ||
'(6) |