8.18
top
← prev up next →

Leftist TreesπŸ”— i

Jon Zeppieri <zeppieri@gmail.com>

Leftist trees are immutable priority queues.

procedure

( leftist-tree <=?[xs])leftist-tree?

xs:sequence? ='()
Makes a new tree containing the elements of xs, ordered by <=?.

Examples:
> (define empty-tree-of-strings(leftist-tree string<=? ))
> (define non-empty-tree-of-strings(leftist-tree string<=? '("once""upon""a""time")))
> empty-tree-of-strings

#<leftist-tree [empty]>

> non-empty-tree-of-strings

#<leftist-tree [count=4; min="a"]>

procedure

( leftist-tree? x)boolean?

x:any/c
Returns #t if x is a leftist tree, #f otherwise.

Examples:

#t

> (leftist-tree? "I can haz treaz?")

#f

Returns #t if t is empty, #f otherwise.

Examples:

#t

> (leftist-tree-empty? (leftist-tree-add t"κατέβην χθὲς εἰς Πειραιᾶ μετὰ Γλαύκωνος τοῦ Ἀρίστωνος..."))

#f

Returns the number of elements in t.

Examples:

0

> (leftist-tree-count (leftist-tree-add-all t(enum->liststring/e10)))

10

procedure

( leftist-tree-add tx)leftist-tree?

x:any/c
Functionally extends t by adding x and returns the extended tree.

Examples:
> (define non-empty(leftist-tree-add empty"Hello world!"))

Functionally extends t by adding all elements of the sequence xs and returns the extended tree.

Examples:
> (define words'("some pig!""terrific""radiant""humble"))
> (leftist-tree-add-all emptywords)

#<leftist-tree [count=4; min="humble"]>

procedure

( leftist-tree-min t)any/c

Returns the least element in t according to the tree’s ordering. If the tree is empty, an exception is raised.

Examples:
> (define words'("some pig!""terrific""radiant""humble"))

"humble"

Functionally removes the least element in t and returns the fresh tree.

Examples:
> (define words'("some pig!""terrific""radiant""humble"))
> (define full(leftist-tree-add-all emptywords))

4

3

procedure

( leftist-tree->list t)(listof any/c )

Returns an ordered list of the elements of t.

Examples:
> (define words'("some pig!""terrific""radiant""humble"))
> (define full(leftist-tree-add-all emptywords))

'("humble" "radiant" "some pig!" "terrific")

procedure

( in-leftist-tree t)sequence?

Returns a sequence that generates the elements of t in order.

Examples:
> (define words'("some pig!""terrific""radiant""humble"))
> (define full(leftist-tree-add-all emptywords))
> (for ([word(in-leftist-tree full)])
(displayln word))

humble

radiant

some pig!

terrific

top
← prev up next →

AltStyle γ«γ‚ˆγ£γ¦ε€‰ζ›γ•γ‚ŒγŸγƒšγƒΌγ‚Έ (->γ‚ͺγƒͺγ‚ΈγƒŠγƒ«) /