music21.timespans.core¶
AVLNode¶
AVLNode
bases
SlottedObject
AVLNode
methods
AVLNode
instance variables
-
AVLNode.
balance
¶ Returns the current state of the difference in heights of the two subtrees rooted on this node.
This attribute is used to help balance the AVL tree.
>>> score = timespans.makeExampleScore() >>> tree = timespans.streamToTimespanTree(score, flatten=True, ... classList=(note.Note, chord.Chord)) >>> print(tree.debug()) <Node: Start:3.0 Indices:(0:5:6:12) Length:{1}> L: <Node: Start:1.0 Indices:(0:2:3:5) Length:{1}> L: <Node: Start:0.0 Indices:(0:0:2:2) Length:{2}> R: <Node: Start:2.0 Indices:(3:3:5:5) Length:{2}> R: <Node: Start:5.0 Indices:(6:8:9:12) Length:{1}> L: <Node: Start:4.0 Indices:(6:6:8:8) Length:{2}> R: <Node: Start:6.0 Indices:(9:9:11:12) Length:{2}> R: <Node: Start:7.0 Indices:(11:11:12:12) Length:{1}>
This tree has one more depth on the right than on the left
>>> tree.rootNode.balance 1
The leftChild of the rootNote is perfectly balanced, while the rightChild is off by one (acceptable).
>>> tree.rootNode.leftChild.balance 0 >>> tree.rootNode.rightChild.balance 1
The rightChild’s children are also (acceptably) unbalanced:
>>> tree.rootNode.rightChild.leftChild.balance 0 >>> tree.rootNode.rightChild.rightChild.balance 1
You should never see a balance other than 1, -1, or 0. If you do then something has gone wrong.
-
AVLNode.
height
¶ The height of the subtree rooted on this node.
This property is used to help balance the AVL tree.
>>> score = timespans.makeExampleScore() >>> tree = timespans.streamToTimespanTree(score, flatten=True, ... classList=(note.Note, chord.Chord)) >>> print(tree.debug()) <Node: Start:3.0 Indices:(0:5:6:12) Length:{1}> L: <Node: Start:1.0 Indices:(0:2:3:5) Length:{1}> L: <Node: Start:0.0 Indices:(0:0:2:2) Length:{2}> R: <Node: Start:2.0 Indices:(3:3:5:5) Length:{2}> R: <Node: Start:5.0 Indices:(6:8:9:12) Length:{1}> L: <Node: Start:4.0 Indices:(6:6:8:8) Length:{2}> R: <Node: Start:6.0 Indices:(9:9:11:12) Length:{2}> R: <Node: Start:7.0 Indices:(11:11:12:12) Length:{1}>
>>> tree.rootNode.height 3
>>> tree.rootNode.rightChild.height 2
>>> tree.rootNode.rightChild.rightChild.height 1
>>> tree.rootNode.rightChild.rightChild.rightChild.height 0
Once you hit a height of zero, then the next child on either size should be None
>>> print(tree.rootNode.rightChild.rightChild.rightChild.rightChild) None
-
AVLNode.
leftChild
¶ The left child of this node.
After setting the left child you need to do a node update. with node.update()
>>> score = timespans.makeExampleScore() >>> tree = timespans.streamToTimespanTree(score, flatten=True, ... classList=(note.Note, chord.Chord)) >>> print(tree.rootNode.debug()) <Node: Start:3.0 Indices:(0:5:6:12) Length:{1}> L: <Node: Start:1.0 Indices:(0:2:3:5) Length:{1}> L: <Node: Start:0.0 Indices:(0:0:2:2) Length:{2}> R: <Node: Start:2.0 Indices:(3:3:5:5) Length:{2}> R: <Node: Start:5.0 Indices:(6:8:9:12) Length:{1}> L: <Node: Start:4.0 Indices:(6:6:8:8) Length:{2}> R: <Node: Start:6.0 Indices:(9:9:11:12) Length:{2}> R: <Node: Start:7.0 Indices:(11:11:12:12) Length:{1}>
>>> print(tree.rootNode.leftChild.debug()) <Node: Start:1.0 Indices:(0:2:3:5) Length:{1}> L: <Node: Start:0.0 Indices:(0:0:2:2) Length:{2}> R: <Node: Start:2.0 Indices:(3:3:5:5) Length:{2}>
-
AVLNode.
position
¶ The position of this node – this is often the same as the offset of the node in a containing score, but does not need to be.
>>> score = timespans.makeExampleScore() >>> tree = timespans.streamToTimespanTree(score, flatten=True, ... classList=(note.Note, chord.Chord)) >>> print(tree.rootNode.debug()) <Node: Start:3.0 Indices:(0:5:6:12) Length:{1}> L: <Node: Start:1.0 Indices:(0:2:3:5) Length:{1}> L: <Node: Start:0.0 Indices:(0:0:2:2) Length:{2}> R: <Node: Start:2.0 Indices:(3:3:5:5) Length:{2}> R: <Node: Start:5.0 Indices:(6:8:9:12) Length:{1}> L: <Node: Start:4.0 Indices:(6:6:8:8) Length:{2}> R: <Node: Start:6.0 Indices:(9:9:11:12) Length:{2}> R: <Node: Start:7.0 Indices:(11:11:12:12) Length:{1}>
>>> tree.rootNode.position 3.0
>>> tree.rootNode.leftChild.position 1.0
>>> tree.rootNode.rightChild.position 5.0
-
AVLNode.
rightChild
¶ The right child of this node.
After setting the right child you need to do a node update. with node.update()
>>> score = timespans.makeExampleScore() >>> tree = timespans.streamToTimespanTree(score, flatten=True, ... classList=(note.Note, chord.Chord)) >>> print(tree.rootNode.debug()) <Node: Start:3.0 Indices:(0:5:6:12) Length:{1}> L: <Node: Start:1.0 Indices:(0:2:3:5) Length:{1}> L: <Node: Start:0.0 Indices:(0:0:2:2) Length:{2}> R: <Node: Start:2.0 Indices:(3:3:5:5) Length:{2}> R: <Node: Start:5.0 Indices:(6:8:9:12) Length:{1}> L: <Node: Start:4.0 Indices:(6:6:8:8) Length:{2}> R: <Node: Start:6.0 Indices:(9:9:11:12) Length:{2}> R: <Node: Start:7.0 Indices:(11:11:12:12) Length:{1}>
>>> print(tree.rootNode.rightChild.debug()) <Node: Start:5.0 Indices:(6:8:9:12) Length:{1}> L: <Node: Start:4.0 Indices:(6:6:8:8) Length:{2}> R: <Node: Start:6.0 Indices:(9:9:11:12) Length:{2}> R: <Node: Start:7.0 Indices:(11:11:12:12) Length:{1}>
>>> print(tree.rootNode.rightChild.rightChild.debug()) <Node: Start:6.0 Indices:(9:9:11:12) Length:{2}> R: <Node: Start:7.0 Indices:(11:11:12:12) Length:{1}>
>>> print(tree.rootNode.rightChild.rightChild.rightChild.debug()) <Node: Start:7.0 Indices:(11:11:12:12) Length:{1}>
AVLTree¶
AVLTree
methods