Expand description
music21’s tree.core: the self-balancing AVL tree its element, offset
and timespan trees are built on.
The steps – placing a node, rotating and rebalancing, finding the node
at, after or before a position, taking one out, building a balanced tree
from a sorted list, counting the elements under each node – are written
once over AvlNodes, so they work on nodes however they are stored.
AvlTree keeps its nodes in a list; the Python package’s nodes are
Python objects music21’s code can reach into.
use music21::tree::core::AvlTree;
let mut tree = AvlTree::new();
for position in [20, 10, 5, 30] {
tree.insert(position, position * 2);
}
assert_eq!(tree.root_position(), Some(&10));
assert_eq!(tree.height(), Some(2));
assert_eq!(tree.position_after(&10), Some(&20));
assert_eq!(tree.position_before(&10), Some(&5));
assert_eq!(tree.get(&30), Some(&60));Structs§
- AvlTree
- An AVL tree of payloads by position, its nodes kept in a list.
- Indices
- Where the elements under a node stand in the tree’s order, as slice
bounds: music21’s
subtreeElementsStartIndex, its payload’s start and stop, andsubtreeElementsStopIndex. - Placement
- One node of a balanced tree being built from a sorted list: the item at
middleof the itemsstart..stop. - Shape
- A node’s height and balance: music21’s
heightandbalance.
Enums§
- Compare
- How a node’s position stands to a position given:
node.position <op> position, as music21 asks it. - Tree
Error - What went wrong in a tree whose nodes were linked by hand.
Traits§
- AvlNodes
- The nodes of an AVL tree, however they are stored: what the steps of this module read and write of them.
- Counted
Nodes - Nodes that count the elements they hold, for
update_indices.
Functions§
- build_
sorted - A balanced tree of
countsorted items, each node made bymakefrom where its item stands: music21’spopulateFromSortedList. Answers the root. - find
- The node at a position: music21’s
getNodeByPosition. - in_
order - Every node under
root, left to right. - insert
- Places a node at a position under
rootunless one is there, answering the new root: music21’screateNodeAtPosition. - node_
after - The first node after a position: music21’s
getNodeAfter. - node_
before - The last node before a position: music21’s
getNodeBefore. - rebalance
- Rebalances the subtree rooted on a node, answering its new centre:
music21’s
rebalance. - remove
- Takes out the node at a position, answering the new root: music21’s
removeNode. A node with two children takes on what the leftmost node of its right subtree holds, and that node goes instead. - rotate_
left_ left - Makes the left child the centre, the node its right child: music21’s
rotateLeftLeft. Answers the new centre. - rotate_
left_ right - The left child rotated right, then the node left: music21’s
rotateLeftRight. - rotate_
right_ left - The right child rotated left, then the node right: music21’s
rotateRightLeft. - rotate_
right_ right - The mirror of
rotate_left_left: music21’srotateRightRight. - update
- Sets a node’s height and balance from its children’s: music21’s
AVLNode.update. - update_
indices - Counts the elements under each node from
nodedown and stores where each node’s stand: music21’supdateIndices.parent_stopis where the elements before the subtree end, if any come before it.