niemads package
NiemaDS is a Python library that implements useful non-standard data structures.
Module contents
-
class niemads.DisjointSet(initial=None)[source]
Bases: object
DisjointSet
class, implemented using the Up-Tree data structure for amortized O(1) find and union operations
-
add(x)[source]
Add a new element x
to this DisjointSet
as a sentinel node
- Args:
x
: The element to insert
-
find(x)[source]
Return the sentinel node of the element x
. Implements path compression along the search
- Args:
x
: The element to find
- Returns:
The sentinel node of x
-
remove(x)[source]
Remove the element x
from this DisjointSet
- Args:
x
: The element to remove
-
sets()[source]
Return the sets of this DisjointSet
- Returns:
list
of set
: The sets of this DisjointSet
-
union(x, y)[source]
Union the sets containing x
and y
. Implements Union-By-Size
- Args:
x
: One of the two elements whose sets will be unioned
y
: One of the two elements whose sets will be unioned
-
class niemads.MultiwayTrie(initial=None)[source]
Bases: object
MultiwayTrie
class, implemented using TreeSwift
-
add(x)[source]
Add a new element x
to this MultiwayTrie
- Args:
x
: The element to insert
-
extend(xs)[source]
Add each element of xs
to this MultiwayTrie
- Args:
xs
: The elements to insert
-
iter_ascending()[source]
Iterate over the elements of this MultiwayTrie
in ascending order
-
iter_descending()[source]
Iterate over the elements of this MultiwayTrie
in descending order
-
static read_tree_newick(newick)[source]
Read a tree from a Newick string or file
- Args:
newick
(str
): Either a Newick string or the path to a Newick file (plain-text or gzipped)
- Returns:
MultiwayTrie
: The Multiway Trie represented by newick
. If the Newick file has multiple trees (one per line), a list
of Tree
objects will be returned
-
remove(x)[source]
Remove the element x
from this MultiwayTrie
- Args:
x
: The element to remove
-
class niemads.RandomSet(initial=None)[source]
Bases: object
RandomSet
class, which is a set with random (rather than arbitrary) pop
-
add(x)[source]
Add a new element x
to this RandomSet
- Args:
x
: The element to insert
-
pop_random()[source]
Randomly remove and return an element from this RandomSet
- Returns:
A random element from this RandomSet
-
remove(x)[source]
Remove the element x
from this RandomSet
- Args:
x
: The element to remove
-
top_random()[source]
Randomly return an element from this RandomSet
- Returns:
A random element from this RandomSet