Skip to main content

Module core

Module core 

Source
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, and subtreeElementsStopIndex.
Placement
One node of a balanced tree being built from a sorted list: the item at middle of the items start..stop.
Shape
A node’s height and balance: music21’s height and balance.

Enums§

Compare
How a node’s position stands to a position given: node.position <op> position, as music21 asks it.
TreeError
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.
CountedNodes
Nodes that count the elements they hold, for update_indices.

Functions§

build_sorted
A balanced tree of count sorted items, each node made by make from where its item stands: music21’s populateFromSortedList. 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 root unless one is there, answering the new root: music21’s createNodeAtPosition.
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’s rotateRightRight.
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 node down and stores where each node’s stand: music21’s updateIndices. parent_stop is where the elements before the subtree end, if any come before it.