New in version 2.4.
Abjad data structure to work with a sequence whose elements have been grouped into arbitrarily many levels of containment.
Example: a list of pitches that have been grouped into cells that have, in turn, been grouped into groups of cells that have, in turn, been grouped into groups of groups of cells.
abjad> from abjad.tools import sequencetools
Here is a tree:
abjad> sequence = [[0, 1], [2, 3], [4, 5], [6, 7]]
abjad> tree = sequencetools.Tree(sequence)
abjad> tree
Tree([[0, 1], [2, 3], [4, 5], [6, 7]])
abjad> tree.parent is None
True
abjad> tree.children
(Tree([0, 1]), Tree([2, 3]), Tree([4, 5]), Tree([6, 7]))
abjad> tree.depth
3
Here’s an internal node:
abjad> tree[2]
Tree([4, 5])
abjad> tree[2].parent
Tree([[0, 1], [2, 3], [4, 5], [6, 7]])
abjad> tree[2].children
(Tree(4), Tree(5))
abjad> tree[2].depth
2
abjad> tree[2].level
1
Here’s a leaf node:
abjad> tree[2][0]
Tree(4)
abjad> tree[2][0].parent
Tree([4, 5])
abjad> tree[2][0].children
()
abjad> tree[2][0].depth
1
abjad> tree[2][0].level
2
abjad> tree[2][0].position
(2, 0)
abjad> tree[2][0].payload
4
Only leaf nodes carry payload. Internal nodes carry no payload.
Negative levels are available to work with trees bottom-up instead of top-down.
Trees do not yet implement append or extend methods.
New in version 2.4.
Children of node:
abjad> sequence = [[0, 1], [2, 3], [4, 5], [6, 7]]
abjad> tree = sequencetools.Tree(sequence)
abjad> tree[1].children
(Tree(2), Tree(3))
Return tuple of zero or more nodes.
New in version 2.4.
Depth of subtree:
abjad> sequence = [[0, 1], [2, 3], [4, 5], [6, 7]]
abjad> tree = sequencetools.Tree(sequence)
abjad> tree[1].depth
2
Return nonnegative integer.
New in version 2.4.
Improper parentage of node:
abjad> sequence = [[0, 1], [2, 3], [4, 5], [6, 7]]
abjad> tree = sequencetools.Tree(sequence)
abjad> tree[1].improper_parentage
(Tree([2, 3]), Tree([[0, 1], [2, 3], [4, 5], [6, 7]]))
Return tuple of one or more nodes.
New in version 2.4.
Index of node in parent:
abjad> sequence = [[0, 1], [2, 3], [4, 5], [6, 7]]
abjad> tree = sequencetools.Tree(sequence)
abjad> tree[1].index_in_parent
1
Return nonnegative integer.
New in version 2.4.
Level of node:
abjad> sequence = [[0, 1], [2, 3], [4, 5], [6, 7]]
abjad> tree = sequencetools.Tree(sequence)
abjad> tree[1].level
1
Return nonnegative integer.
New in version 2.4.
Negative level of node:
abjad> sequence = [[0, 1], [2, 3], [4, 5], [6, 7]]
abjad> tree = sequencetools.Tree(sequence)
abjad> tree[1].negative_level
-2
Return negative integer.
New in version 2.4.
Position of node relative to root:
abjad> sequence = [[0, 1], [2, 3], [4, 5], [6, 7]]
abjad> tree = sequencetools.Tree(sequence)
abjad> tree[1].position
(1,)
Return tuple of zero or more nonnegative integers.
New in version 2.4.
Proper parentage of node:
abjad> sequence = [[0, 1], [2, 3], [4, 5], [6, 7]]
abjad> tree = sequencetools.Tree(sequence)
abjad> tree[1].proper_parentage
(Tree([[0, 1], [2, 3], [4, 5], [6, 7]]),)
Return tuple of zero or more nodes.
New in version 2.5.
Get next n complete nodes at level from node.
abjad> sequence = [[0, 1], [2, 3], [4, 5], [6, 7]]
abjad> tree = sequencetools.Tree(sequence)
With nonnegative level:
Get next 4 nodes at level 2:
abjad> tree[0][0].get_next_n_complete_nodes_at_level(4, 2)
[Tree(1), Tree(2), Tree(3), Tree(4)]
Get next 3 nodes at level 1:
abjad> tree[0][0].get_next_n_complete_nodes_at_level(3, 1)
[Tree([1]), Tree([2, 3]), Tree([4, 5]), Tree([6, 7])]
With negative level:
Get next 4 nodes at level -1:
abjad> tree[0][0].get_next_n_complete_nodes_at_level(4, -1)
[Tree(1), Tree(2), Tree(3), Tree(4)]
Get next 3 nodes at level -2:
abjad> tree[0][0].get_next_n_complete_nodes_at_level(3, -2)
[Tree([1]), Tree([2, 3]), Tree([4, 5]), Tree([6, 7])]
Trim first node if necessary.
Return list of nodes.
New in version 2.4.
Get next n nodes at level from node.
abjad> sequence = [[0, 1], [2, 3], [4, 5], [6, 7]]
abjad> tree = sequencetools.Tree(sequence)
With nonnegative level:
Get next 4 nodes at level 2:
abjad> tree[0][0].get_next_n_nodes_at_level(4, 2)
[Tree(1), Tree(2), Tree(3), Tree(4)]
Get next 3 nodes at level 1:
abjad> tree[0][0].get_next_n_nodes_at_level(3, 1)
[Tree([1]), Tree([2, 3]), Tree([4, 5])]
Get next node at level 0:
abjad> tree[0][0].get_next_n_nodes_at_level(1, 0)
[Tree([[1], [2, 3], [4, 5], [6, 7]])]
With negative level:
Get next 4 nodes at level -1:
abjad> tree[0][0].get_next_n_nodes_at_level(4, -1)
[Tree(1), Tree(2), Tree(3), Tree(4)]
Get next 3 nodes at level -2:
abjad> tree[0][0].get_next_n_nodes_at_level(3, -2)
[Tree([1]), Tree([2, 3]), Tree([4, 5])]
Trim first node if necessary.
Return list of nodes.
New in version 2.4.
Get node at position:
abjad> sequence = [[0, 1], [2, 3], [4, 5], [6, 7]]
abjad> tree = sequencetools.Tree(sequence)
abjad> tree.get_node_at_position((2, 1))
Tree(5)
Return node.
New in version 2.4.
Get position of descendent relative to node rather than relative to root:
abjad> sequence = [[0, 1], [2, 3], [4, 5], [6, 7]]
abjad> tree = sequencetools.Tree(sequence)
abjad> tree[3].get_position_of_descendant(tree[3][0])
(0,)
Return tuple of zero or more nonnegative integers.
New in version 2.4.
True when node is at level in tree:
abjad> sequence = [[0, 1], [2, 3], [4, 5], [6, 7]]
abjad> tree = sequencetools.Tree(sequence)
abjad> tree[1][1].is_at_level(-1)
True
False otherwise:
abjad> tree[1][1].is_at_level(0)
False
Return boolean.
Predicate works for positive, negative and zero-valued level.
New in version 2.4.
Iterate depth at level:
abjad> sequence = [[0, 1], [2, 3], [4, 5], [6, 7]]
abjad> tree = sequencetools.Tree(sequence)
abjad> for x in tree.iterate_at_level(0): x
...
Tree([[0, 1], [2, 3], [4, 5], [6, 7]])
abjad> for x in tree.iterate_at_level(1): x
...
Tree([0, 1])
Tree([2, 3])
Tree([4, 5])
Tree([6, 7])
abjad> for x in tree.iterate_at_level(2): x
...
Tree(0)
Tree(1)
Tree(2)
Tree(3)
Tree(4)
Tree(5)
Tree(6)
Tree(7)
abjad> for x in tree.iterate_at_level(-1): x
...
Tree(0)
Tree(1)
Tree(2)
Tree(3)
Tree(4)
Tree(5)
Tree(6)
Tree(7)
abjad> for x in tree.iterate_at_level(-2): x
...
Tree([0, 1])
Tree([2, 3])
Tree([4, 5])
Tree([6, 7])
abjad> for x in tree.iterate_at_level(-3): x
...
Tree([[0, 1], [2, 3], [4, 5], [6, 7]])
Return node generator.
New in version 2.4.
Iterate tree depth-first:
abjad> sequence = [[0, 1], [2, 3], [4, 5], [6, 7]]
abjad> tree = sequencetools.Tree(sequence)
abjad> for node in tree.iterate_depth_first(): node
...
Tree([[0, 1], [2, 3], [4, 5], [6, 7]])
Tree([0, 1])
Tree(0)
Tree(1)
Tree([2, 3])
Tree(2)
Tree(3)
Tree([4, 5])
Tree(4)
Tree(5)
Tree([6, 7])
Tree(6)
Tree(7)
Return node generator.
New in version 2.4.
Iterate tree payload:
abjad> sequence = [[0, 1], [2, 3], [4, 5], [6, 7]]
abjad> tree = sequencetools.Tree(sequence)
abjad> for element in tree.iterate_payload():
... element
...
0
1
2
3
4
5
6
7
Return payload generator.
New in version 2.4.
Remove node from tree:
abjad> sequence = [[0, 1], [2, 3], [4, 5], [6, 7]]
abjad> tree = sequencetools.Tree(sequence)
abjad> tree.remove(tree[1])
abjad> tree
Tree([[0, 1], [4, 5], [6, 7]])
Return none.
New in version 2.4.
Remove node and all nodes left of node to root:
abjad> sequence = [[0, 1], [2, 3], [4, 5], [6, 7]]
abjad> tree = sequencetools.Tree(sequence)
abjad> tree[0][0].remove_to_root()
abjad> tree
Tree([[1], [2, 3], [4, 5], [6, 7]])
abjad> tree = sequencetools.Tree(sequence)
abjad> tree[0][1].remove_to_root()
abjad> tree
Tree([[2, 3], [4, 5], [6, 7]])
abjad> tree = sequencetools.Tree(sequence)
abjad> tree[1].remove_to_root()
abjad> tree
Tree([[4, 5], [6, 7]])
Modify in-place to root.
Return none.
New in version 2.5.
Change tree to nested lists:
abjad> sequence = [[0, 1], [2, 3], [4, 5], [6, 7]]
abjad> tree = sequencetools.Tree(sequence)
abjad> tree
Tree([[0, 1], [2, 3], [4, 5], [6, 7]])
abjad> tree.to_nested_lists()
[[0, 1], [2, 3], [4, 5], [6, 7]]
Return list of lists.
x.__delattr__(‘name’) <==> del x.name
Note
Inherited from __builtin__.object
Abjad objects by default do not implement this method.
Raise exception.
Note
Inherited from abctools.AbjadObject
Abjad objects by default do not implement this method.
Raise exception
Note
Inherited from abctools.AbjadObject
Note
Inherited from __builtin__.object
Abjad objects by default do not implement this method.
Raise exception.
Note
Inherited from abctools.AbjadObject
Abjad objects by default do not implement this method.
Raise exception.
Note
Inherited from abctools.AbjadObject
x.__setattr__(‘name’, value) <==> x.name = value
Note
Inherited from __builtin__.object