7 Red-Black Trees
(require pfds/red-black-tree) | package: pfds |
No red node has a red child.
Every path from root to an empty node has the same number of black nodes.
syntax
(RedBlackTree A)
procedure
(redblacktree comp a ...) → (RedBlackTree A)
comp : (A A -> Boolean) a : A
> (redblacktree < 1 2 3 4 5) - : (RBTree Positive-Byte)
#<RBTree>
In the above example, the red black tree obtained will have 2 as its root and < as the comparison function.
procedure
(empty? rbt) → Boolean
rbt : (RedBlackTree A)
> (empty? (redblacktree < 1 2 3 4 5 6)) - : Boolean
#f
> (empty? (redblacktree <)) - : Boolean
#t
procedure
(insert a rbt) → (RedBlackTree A)
a : A rbt : (RedBlackTree A)
> (insert 10 (redblacktree < 1 2 3 4 5 6)) - : (RBTree Positive-Byte)
#<RBTree>
In the above example, insert adds the 10 to (redblacktree < 1 2 3 4 5 6).
procedure
(root rbt) → A
rbt : (RedBlackTree A)
> (root (redblacktree < 1 2 3 4 5 6)) - : Integer [more precisely: Positive-Byte]
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).
procedure
(member? rbt) → Boolean
rbt : (RedBlackTree A)
> (member? 5 (redblacktree < 1 2 3 4 5 6)) - : Boolean
#t
> (member? 10 (redblacktree < 1 2 3 4 5 6)) - : Boolean
#f
procedure
(delete-root rbt) → (RedBlackTree A)
rbt : (RedBlackTree A)
> (delete-root (redblacktree < 1 2 3 4 5 6)) - : (RBTree Positive-Byte)
#<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.
procedure
(delete elem rbt) → (RedBlackTree A)
elem : A rbt : (RedBlackTree A)
> (delete 5 (redblacktree < 1 2 3 4 5 6)) - : (RBTree Positive-Byte)
#<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).
procedure
(redblacktree->list rbt) → (Listof A)
rbt : (RedBlackTree A)
> (redblacktree->list (redblacktree > 1 2 3 4 5)) - : (Listof Positive-Byte)
'(2 4 3 5 1)
procedure
(map comparer func rbt1 rbt2 ...) → (RedBlackTree A)
comparer : (C C -> Boolean) func : (A B ... B -> C) rbt1 : (RedBlackTree A) rbt2 : (RedBlackTree B)
> (redblacktree->list (map < add1 (redblacktree < 1 2 3 4 5 6))) - : (Listof Positive-Index)
'(4 6 3 5 2 7)
> (redblacktree->list (map < * (redblacktree < 1 2 3 4 5 6) (redblacktree < 1 2 3 4 5 6))) - : (Listof Positive-Index)
'(9 25 4 16 1 36)
procedure
(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.
> (fold + 0 (redblacktree < 1 2 3 4 5 6)) - : Integer [more precisely: Nonnegative-Integer]
21
> (fold * 1 (redblacktree < 1 2 3 4 5 6) (redblacktree < 1 2 3 4 5 6)) - : Integer [more precisely: Positive-Integer]
518400
procedure
(filter func rbt) → (RedBlackTree A)
func : (A -> Boolean) rbt : (RedBlackTree A)
> (define rbt (redblacktree < 1 2 3 4 5 6))
> (redblacktree->list (filter (λ: ([x : Integer]) (> x 5)) rbt)) - : (Listof Positive-Byte)
'(6)
> (redblacktree->list (filter (λ: ([x : Integer]) (< x 5)) rbt)) - : (Listof Positive-Byte)
'(3 2 1 4)
> (redblacktree->list (filter (λ: ([x : Integer]) (<= x 5)) rbt)) - : (Listof Positive-Byte)
'(3 2 4 1 5)
procedure
(remove func rbt) → (RedBlackTree A)
func : (A -> Boolean) rbt : (RedBlackTree A)
> (redblacktree->list (remove (λ: ([x : Integer]) (> x 5)) (redblacktree < 1 2 3 4 5 6))) - : (Listof Positive-Byte)
'(3 2 4 1 5)
> (redblacktree->list (remove (λ: ([x : Integer]) (< x 5)) (redblacktree < 1 2 3 4 5 6))) - : (Listof Positive-Byte)
'(5 6)
> (redblacktree->list (remove (λ: ([x : Integer]) (<= x 5)) (redblacktree < 1 2 3 4 5 6))) - : (Listof Positive-Byte)
'(6)