
    rh|                        S r SSKJr  SSKJr  SSKJr  SSKJr   " S S\R                  5      r	 " S S	\R                  5      r\S
:X  a  SSKr\R                  " 5         gg)z
These are the lowest level tools for working with self-balancing AVL trees.

There's an overhead to creating an AVL tree, but for a large score it is
absolutely balanced by having O(log n) search times.
    )annotations)prebase)TreeException)commonc                      \ rS rSr% SrSrSSSSSS	S
.rS\S'   SS jrS r	S r
S rS rS rS rS rS rS rS rSrg)AVLNode   a  
An AVL Tree Node, not specialized in any way, just contains positions.

>>> position = 1.0
>>> node = tree.core.AVLNode(position)
>>> node
<AVLNode: Start:1.0 Height:0 L:None R:None>
>>> n2 = tree.core.AVLNode(2.0)
>>> node.rightChild = n2
>>> node.update()
>>> node
<AVLNode: Start:1.0 Height:1 L:None R:0>

Nodes can rebalance themselves, but they work best in a Tree.

Please consult the Wikipedia entry on AVL trees
(https://en.wikipedia.org/wiki/AVL_tree) for a very detailed
description of how this data structure works.
)__weakref__balanceheightpositionpayload	leftChild
rightChilda#  
        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 = tree.makeExampleScore()
        >>> scoreTree = tree.fromStream.asTimespans(score, flatten=True,
        ...                    classList=(note.Note, chord.Chord))
        >>> print(scoreTree.debug())
        <OffsetNode 3.0 Indices:0,5,6,12 Length:1>
            L: <OffsetNode 1.0 Indices:0,2,3,5 Length:1>
                L: <OffsetNode 0.0 Indices:0,0,2,2 Length:2>
                R: <OffsetNode 2.0 Indices:3,3,5,5 Length:2>
            R: <OffsetNode 5.0 Indices:6,8,9,12 Length:1>
                L: <OffsetNode 4.0 Indices:6,6,8,8 Length:2>
                R: <OffsetNode 6.0 Indices:9,9,11,12 Length:2>
                    R: <OffsetNode 7.0 Indices:11,11,12,12 Length:1>


        This tree has one more depth on the right than on the left

        >>> scoreTree.rootNode.balance
        1


        The leftChild of the rootNote is perfectly balanced, while the rightChild is off by
        one (acceptable).

        >>> scoreTree.rootNode.leftChild.balance
        0
        >>> scoreTree.rootNode.rightChild.balance
        1


        The rightChild's children are also (acceptably) unbalanced:

        >>> scoreTree.rootNode.rightChild.leftChild.balance
        0
        >>> scoreTree.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.
        a  
        The height of the subtree rooted on this node.

        This property is used to help balance the AVL tree.

        >>> score = tree.makeExampleScore()
        >>> scoreTree = tree.fromStream.asTimespans(score, flatten=True,
        ...              classList=(note.Note, chord.Chord))
        >>> print(scoreTree.debug())
        <OffsetNode 3.0 Indices:0,5,6,12 Length:1>
            L: <OffsetNode 1.0 Indices:0,2,3,5 Length:1>
                L: <OffsetNode 0.0 Indices:0,0,2,2 Length:2>
                R: <OffsetNode 2.0 Indices:3,3,5,5 Length:2>
            R: <OffsetNode 5.0 Indices:6,8,9,12 Length:1>
                L: <OffsetNode 4.0 Indices:6,6,8,8 Length:2>
                R: <OffsetNode 6.0 Indices:9,9,11,12 Length:2>
                    R: <OffsetNode 7.0 Indices:11,11,12,12 Length:1>

        >>> scoreTree.rootNode.height
        3

        >>> scoreTree.rootNode.rightChild.height
        2

        >>> scoreTree.rootNode.rightChild.rightChild.height
        1

        >>> scoreTree.rootNode.rightChild.rightChild.rightChild.height
        0

        Once you hit a height of zero, then the next child on either size should be None

        >>> print(scoreTree.rootNode.rightChild.rightChild.rightChild.rightChild)
        None
        zR
        The content of the node at this point.  Usually a Music21Object.
        a&  
        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. It could be the .sortTuple

        >>> score = tree.makeExampleScore()
        >>> scoreTree = tree.fromStream.asTimespans(score, flatten=True,
        ...            classList=(note.Note, chord.Chord))
        >>> print(scoreTree.rootNode.debug())
        <OffsetNode 3.0 Indices:0,5,6,12 Length:1>
            L: <OffsetNode 1.0 Indices:0,2,3,5 Length:1>
                L: <OffsetNode 0.0 Indices:0,0,2,2 Length:2>
                R: <OffsetNode 2.0 Indices:3,3,5,5 Length:2>
            R: <OffsetNode 5.0 Indices:6,8,9,12 Length:1>
                L: <OffsetNode 4.0 Indices:6,6,8,8 Length:2>
                R: <OffsetNode 6.0 Indices:9,9,11,12 Length:2>
                    R: <OffsetNode 7.0 Indices:11,11,12,12 Length:1>

        >>> scoreTree.rootNode.position
        3.0

        >>> scoreTree.rootNode.leftChild.position
        1.0

        >>> scoreTree.rootNode.rightChild.position
        5.0
        a!  
        The left child of this node.

        After setting the left child you need to do a node update. with node.update()

        >>> score = tree.makeExampleScore()
        >>> scoreTree = tree.fromStream.asTimespans(score, flatten=True,
        ...           classList=(note.Note, chord.Chord))
        >>> print(scoreTree.rootNode.debug())
        <OffsetNode 3.0 Indices:0,5,6,12 Length:1>
            L: <OffsetNode 1.0 Indices:0,2,3,5 Length:1>
                L: <OffsetNode 0.0 Indices:0,0,2,2 Length:2>
                R: <OffsetNode 2.0 Indices:3,3,5,5 Length:2>
            R: <OffsetNode 5.0 Indices:6,8,9,12 Length:1>
                L: <OffsetNode 4.0 Indices:6,6,8,8 Length:2>
                R: <OffsetNode 6.0 Indices:9,9,11,12 Length:2>
                    R: <OffsetNode 7.0 Indices:11,11,12,12 Length:1>

        >>> print(scoreTree.rootNode.leftChild.debug())
        <OffsetNode 1.0 Indices:0,2,3,5 Length:1>
            L: <OffsetNode 0.0 Indices:0,0,2,2 Length:2>
            R: <OffsetNode 2.0 Indices:3,3,5,5 Length:2>
        a  
        The right child of this node.

        After setting the right child you need to do a node update. with node.update()

        >>> score = tree.makeExampleScore()
        >>> scoreTree = tree.fromStream.asTimespans(score, flatten=True,
        ...             classList=(note.Note, chord.Chord))
        >>> print(scoreTree.rootNode.debug())
        <OffsetNode 3.0 Indices:0,5,6,12 Length:1>
            L: <OffsetNode 1.0 Indices:0,2,3,5 Length:1>
                L: <OffsetNode 0.0 Indices:0,0,2,2 Length:2>
                R: <OffsetNode 2.0 Indices:3,3,5,5 Length:2>
            R: <OffsetNode 5.0 Indices:6,8,9,12 Length:1>
                L: <OffsetNode 4.0 Indices:6,6,8,8 Length:2>
                R: <OffsetNode 6.0 Indices:9,9,11,12 Length:2>
                    R: <OffsetNode 7.0 Indices:11,11,12,12 Length:1>

        >>> print(scoreTree.rootNode.rightChild.debug())
        <OffsetNode 5.0 Indices:6,8,9,12 Length:1>
            L: <OffsetNode 4.0 Indices:6,6,8,8 Length:2>
            R: <OffsetNode 6.0 Indices:9,9,11,12 Length:2>
                R: <OffsetNode 7.0 Indices:11,11,12,12 Length:1>

        >>> print(scoreTree.rootNode.rightChild.rightChild.debug())
        <OffsetNode 6.0 Indices:9,9,11,12 Length:2>
            R: <OffsetNode 7.0 Indices:11,11,12,12 Length:1>

        >>> print(scoreTree.rootNode.rightChild.rightChild.rightChild.debug())
        <OffsetNode 7.0 Indices:11,11,12,12 Length:1>
        )r   r   r   r   r   r   zdict[str, str]	_DOC_ATTRNc                T    Xl         X l        SU l        SU l        S U l        S U l        g )Nr   )r   r   r   r   r   r   )selfr   r   s      K/home/james-whalen/.local/lib/python3.13/site-packages/music21/tree/core.py__init__AVLNode.__init__   s(         c                    S nU R                   (       a  U R                   R                  nS nU R                  (       a  U R                  R                  nSR                  U R                  R
                  U R                  U R                  UU5      $ )Nz"<{}: Start:{} Height:{} L:{} R:{}>)r   r   r   format	__class____name__r   )r   lcHeightrcHeights      r   __repr__AVLNode.__repr__   sl    >>~~,,H??--H3::NN##MMKK
 	
r   c                H    U R                   Ul         U R                  Ul        g)a  
move attributes from this node to another in case "removal" actually
means substituting one node for another in the tree.

Subclass this in derived classes

Do not copy anything about height, balance, left or right
children, etc.  By default just moves position and payload.
N)r   r   )r   others     r   moveAttributesAVLNode.moveAttributes  s     r   c                @    SR                  U R                  5       5      $ )a  
Get a debug of the Node:

>>> score = tree.makeExampleScore()
>>> scoreTree = tree.fromStream.asTimespans(score, flatten=True,
...              classList=(note.Note, chord.Chord))
>>> rn = scoreTree.rootNode
>>> print(rn.debug())
<OffsetNode 3.0 Indices:0,5,6,12 Length:1>
    L: <OffsetNode 1.0 Indices:0,2,3,5 Length:1>
        L: <OffsetNode 0.0 Indices:0,0,2,2 Length:2>
        R: <OffsetNode 2.0 Indices:3,3,5,5 Length:2>
    R: <OffsetNode 5.0 Indices:6,8,9,12 Length:1>
        L: <OffsetNode 4.0 Indices:6,6,8,8 Length:2>
        R: <OffsetNode 6.0 Indices:9,9,11,12 Length:2>
            R: <OffsetNode 7.0 Indices:11,11,12,12 Length:1>

)join_getDebugPiecesr   s    r   debugAVLNode.debug  s    $ yy--/00r   c                   / nUR                  [        U 5      5        U R                  (       aL  U R                  R                  5       nUR                  SUS    35        UR	                  S USS  5       5        U R
                  (       aL  U R
                  R                  5       nUR                  SUS    35        UR	                  S USS  5       5        U$ )a  
Return a list of the debugging information of the tree (used for debug):

Called recursively

>>> score = tree.makeExampleScore()
>>> scoreTree = tree.fromStream.asTimespans(score, flatten=True,
...            classList=(note.Note, chord.Chord))
>>> rn = scoreTree.rootNode
>>> rn._getDebugPieces()
['<OffsetNode 3.0 Indices:0,5,6,12 Length:1>',
'\tL: <OffsetNode 1.0 Indices:0,2,3,5 Length:1>',
'\t\tL: <OffsetNode 0.0 Indices:0,0,2,2 Length:2>',
'\t\tR: <OffsetNode 2.0 Indices:3,3,5,5 Length:2>',
'\tR: <OffsetNode 5.0 Indices:6,8,9,12 Length:1>',
'\t\tL: <OffsetNode 4.0 Indices:6,6,8,8 Length:2>',
'\t\tR: <OffsetNode 6.0 Indices:9,9,11,12 Length:2>',
'\t\t\tR: <OffsetNode 7.0 Indices:11,11,12,12 Length:1>']
z	L: r   c              3  ,   #    U  H
  nS U-   v   M     g7f	N .0xs     r   	<genexpr>*AVLNode._getDebugPieces.<locals>.<genexpr>?       :Mq$(M      Nz	R: c              3  ,   #    U  H
  nS U-   v   M     g7fr-   r/   r0   s     r   r3   r4   C  r5   r6   )appendreprr   r'   extendr   )r   result	subResults      r   r'   AVLNode._getDebugPieces&  s    ( d4j!>>668IMME)A,01MM:IabM::??779IMME)A,01MM:IabM::r   c                    U R                   (       a  U R                   R                  OSnU R                  (       a  U R                  R                  OSn[        X5      S-   U l        X!-
  U l        g)aE  
Updates the height and balance attributes of the nodes.

Must be called whenever .leftChild or .rightChild are changed.

Used for the next balancing operation -- does not rebalance itself.

Note that it only looks at its children's height and balance attributes
not their children's. So if they are wrong, this will be too.

Returns None

We create a score with everything correct.

>>> score = tree.makeExampleScore()
>>> scoreTree = tree.fromStream.asTimespans(score, flatten=True,
...             classList=(note.Note, chord.Chord))
>>> n = scoreTree.rootNode
>>> n
<OffsetNode 3.0 Indices:0,5,6,12 Length:1>
>>> n.height, n.balance
(3, 1)

Now let's screw up the height and balance

>>> n.height = 100
>>> n.balance = -2
>>> n.height, n.balance
(100, -2)

But we can fix it with `.update()`

>>> n.update()
>>> n.height, n.balance
(3, 1)

Note that if we were to screw up the balance/height of one of the
child notes of `n` then this would not fix that node's balance/height.
This method assumes that children have the correct information and only
updates the information for this node.

r7   N)r   r   r   maxr   )r   
leftHeightrightHeights      r   updateAVLNode.updateF  sN    V /3nnT^^**"
04doo,,R*2Q6"/r   c                    U R                   nUR                  U l         U R                  5         Xl        UR                  5         U$ )z
Rotates a node left twice.

Makes this node the rightChild of
the former leftChild and makes the former leftChild's rightChild
be the leftChild of this node.

Used during tree rebalancing.

Returns the prior leftChild node as the new central node.
)r   r   rD   r   nextNodes     r   rotateLeftLeftAVLNode.rotateLeftLeftv  s8     >>!,,"r   c                    U R                   R                  5       U l         U R                  5         U R                  5       nU$ )z
Rotates a node left, then right.

Makes this note the rightChild of the former rightChild of the former leftChild node

Used during tree rebalancing.

Returns the former rightChild of the former leftChild node as the new central node.
)r   rotateRightRightrD   rI   rG   s     r   rotateLeftRightAVLNode.rotateLeftRight  s4     88:&&(r   c                    U R                   R                  5       U l         U R                  5         U R                  5       nU$ )z
Rotates a node right, then left.

Makes this note the leftChild of the former leftChild of the former rightChild node

Used during tree rebalancing.

Returns the former leftChild of the former rightChild node as the new central node.
)r   rI   rD   rL   rG   s     r   rotateRightLeftAVLNode.rotateRightLeft  s4     //88:((*r   c                    U R                   nUR                  U l         U R                  5         Xl        UR                  5         U$ )z
Rotates a node right twice.

Makes this node the leftChild of
the former rightChild and makes the former rightChild's leftChild
be the rightChild of this node.

Used during tree rebalancing.

Returns the prior rightChild node as the new central node.
)r   r   rD   rG   s     r   rL   AVLNode.rotateRightRight  s8     ??",,!r   c                   U nU R                   S:  a<  U R                  R                   S:  a  U R                  5       nO\U R                  5       nOKU R                   S:  a;  U R                  R                   S::  a  U R                  5       nOU R                  5       nUR                   S:  d  UR                   S:  a  [        S5      eU$ )zL
Rebalances the subtree rooted on this node.

Returns the new central node.
r7   r   r@   zNSomehow Nodes are still not balanced. node.balance %r must be between -1 and 1)r   r   rL   rP   r   rI   rM   r   )r   nodes     r   	rebalanceAVLNode.rebalance  s     <<!&&!+,,.++-\\B~~%%***,++- <<"q 0`b b r   )r   r   r   r   r   r   N)r   
__module____qualname____firstlineno____doc__	__slots__r   __annotations__r   r   r"   r)   r'   rD   rI   rM   rP   rL   rV   __static_attributes__r/   r   r   r   r      sy    ,	I,^"F4.Qh!I~ hX
 %1(@.0`(&r   r   c                  b    \ rS rSrSrSr\rS rS r	S r
S rS rS	 rS
 rS rS rS rS rSrg)AVLTreei  z
Data structure for working with tree.node.AVLNode objects.

To be subclassed in order to do anything useful with music21 objects.
)r
   rootNodec                    S U l         g rX   rb   r(   s    r   r   AVLTree.__init__  s	    r   c                4   ^ U4S jmT" U R                   5      $ )ab  
Iterates through all the nodes in the position tree in left to right order.

Note that this runs in O(n log n) time in Python, while iterating through
a list runs in O(n) time in C, so this isn't something to do on real datasets.

>>> nodePositions = [0, 2, 4, 6, 8, 10, 12]
>>> avl = tree.core.AVLTree()
>>> for np in nodePositions:
...     avl.createNodeAtPosition(np)
>>> for x in avl:
...     x
<AVLNode: Start:0 Height:0 L:None R:None>
<AVLNode: Start:2 Height:1 L:0 R:0>
<AVLNode: Start:4 Height:0 L:None R:None>
<AVLNode: Start:6 Height:2 L:1 R:1>
<AVLNode: Start:8 Height:0 L:None R:None>
<AVLNode: Start:10 Height:1 L:0 R:0>
<AVLNode: Start:12 Height:0 L:None R:None>

Note: for this example to be stable, we can't shuffle the nodes, since there are
numerous possible configurations that meet the AVLTree constraints, some
of height 2 and some of height 3
c              3     >#    U bT  U R                   b  T" U R                   5       S h  vN   U v   U R                  b  T" U R                  5       S h  vN   g g g  N2 N	7frX   )r   r   )rU   recurses    r   rh   !AVLTree.__iter__.<locals>.recurse  s[     >>-&t~~666
??.&t777 /	  6 8s!   &A A*A AA A rd   )r   rh   s    @r   __iter__AVLTree.__iter__  s    2	8 t}}%%r   c                L   ^^ SUU4S jjmU R                   mT" U5      U l        g)a  
Populate this tree from a sorted list of two-tuples of (position, payload).

This is about an order of magnitude faster (3ms vs 21ms for 1000 items;
31 vs. 300ms for 10,000 items) than running createNodeAtPosition()
for each element in a list if it is
already sorted.  Thus, it should be used when converting a
Stream where .isSorted is True into a tree.

This method assumes that the current tree is empty (or will be wiped) and
that listOfTuples is a non-empty
list where the first element is a unique position to insert,
and the second is the complete payload for that node, and
that the positions are strictly increasing in order.

If any of the conditions is not true, expect to get a dangerously
badly sorted tree that will be useless.

>>> listOfTuples = [(i, str(i)) for i in range(1000)]
>>> listOfTuples[10]
(10, '10')
>>> avlTree = tree.core.AVLTree()
>>> avlTree.rootNode is None
True
>>> avlTree.populateFromSortedList(listOfTuples)
>>> avlTree.rootNode
<AVLNode: Start:500 Height:9 L:8 R:8>

>>> n = avlTree.rootNode
>>> while n is not None:
...    print(n, repr(n.payload))
...    n = n.leftChild
<AVLNode: Start:500 Height:9 L:8 R:8> '500'
<AVLNode: Start:250 Height:8 L:7 R:7> '250'
<AVLNode: Start:125 Height:7 L:6 R:6> '125'
<AVLNode: Start:62 Height:6 L:5 R:5> '62'
<AVLNode: Start:31 Height:5 L:4 R:4> '31'
<AVLNode: Start:15 Height:4 L:3 R:3> '15'
<AVLNode: Start:7 Height:3 L:2 R:2> '7'
<AVLNode: Start:3 Height:2 L:1 R:1> '3'
<AVLNode: Start:1 Height:1 L:0 R:0> '1'
<AVLNode: Start:0 Height:0 L:None R:None> '0'
c                   > U (       d  g[        U 5      S-  nX   nT" US   US   5      nT" U SU 5      Ul        T" XS-   S 5      Ul        UR                  5         U$ )z
Divide and conquer.
N   r   r7   )lenr   r   rD   )subListOfTuplesmidpointmidtuplen	NodeClassrh   s       r   rh   /AVLTree.populateFromSortedList.<locals>.recurse5  sl     #?+q0H&0H(1+x{3A!/)8"<=AK"?a<=#ABALHHJHr   N)returnzAVLNode | None)	nodeClassrb   )r   listOfTuplesrt   rh   s     @@r   populateFromSortedListAVLTree.populateFromSortedList  s%    Z	 	 NN	-r   c                F   ^ ^ UU 4S jmT" T R                   U5      T l         g)a  
creates a new node at position and sets the rootNode
appropriately

>>> avl = tree.core.AVLTree()
>>> avl.createNodeAtPosition(20)
>>> avl.rootNode
<AVLNode: Start:20 Height:0 L:None R:None>

>>> avl.createNodeAtPosition(10)
>>> avl.rootNode
<AVLNode: Start:20 Height:1 L:0 R:None>

>>> avl.createNodeAtPosition(5)
>>> avl.rootNode
<AVLNode: Start:10 Height:1 L:0 R:0>

>>> avl.createNodeAtPosition(30)
>>> avl.rootNode
<AVLNode: Start:10 Height:2 L:0 R:1>
>>> avl.rootNode.leftChild
<AVLNode: Start:5 Height:0 L:None R:None>
>>> avl.rootNode.rightChild
<AVLNode: Start:20 Height:1 L:None R:0>

>>> avl.rootNode.rightChild.rightChild
<AVLNode: Start:30 Height:0 L:None R:None>
c                4  > U c  TR                  U5      $ XR                  :  a)  T" U R                  U5      U l        U R                  5         O8U R                  U:  a(  T" U R                  U5      U l        U R                  5         U b  U R                  5       $ g)z
this recursively finds the right place for the new node
and either creates a new node (if it is in the right place)
or rebalances the nodes above it and tells those nodes how
to set their new roots.
N)rw   r   r   rD   r   rV   )rU   innerPositionrh   r   s     r   rh   -AVLTree.createNodeAtPosition.<locals>.recursec  s     | ~~m44}},!(!G.")$//="I~~''  r   Nrd   r   r   rh   s   ` @r   createNodeAtPositionAVLTree.createNodeAtPositionF  s    :	(*  x8r   c                R    U R                   b  U R                   R                  5       $ g)a
  
Gets string representation of the node tree.

Useful only for debugging its internal node structure.

>>> tsList = [(0, 2), (0, 9), (1, 1), (2, 3), (3, 4),
...           (4, 9), (5, 6), (5, 8), (6, 8), (7, 7)]
>>> tss = [tree.spans.Timespan(x, y) for x, y in tsList]
>>> tsTree = tree.timespanTree.TimespanTree()
>>> tsTree.insert(tss)

>>> print(tsTree.debug())
<OffsetNode 3.0 Indices:0,4,5,10 Length:1>
    L: <OffsetNode 1.0 Indices:0,2,3,4 Length:1>
        L: <OffsetNode 0.0 Indices:0,0,2,2 Length:2>
        R: <OffsetNode 2.0 Indices:3,3,4,4 Length:1>
    R: <OffsetNode 5.0 Indices:5,6,8,10 Length:2>
        L: <OffsetNode 4.0 Indices:5,5,6,6 Length:1>
        R: <OffsetNode 6.0 Indices:8,8,9,10 Length:1>
            R: <OffsetNode 7.0 Indices:9,9,10,10 Length:1>
 )rb   r)   r(   s    r   r)   AVLTree.debugz  s$    , ==$==&&((r   c                4   ^ U4S jmT" XR                   5      $ )zr
Searches for a node whose position is `position` in the subtree
rooted on `node`.

Returns a Node object or None
c                   > Ubw  UR                   U :X  a  U$ UR                  (       a!  XR                   :  a  T" XR                  5      $ UR                  (       a"  UR                   U :  a  T" XR                  5      $ g rX   )r   r   r   )r}   rU   rh   s     r   rh   *AVLTree.getNodeByPosition.<locals>.recurse  s]    ==M1K^^(E"=..AA__)F"=//BBr   rd   r   s     @r   getNodeByPositionAVLTree.getNodeByPosition  s    	 x//r   c                B   ^ U4S jmT" U R                   U5      nUc  gU$ )a  
Gets the first node after `position`.

>>> score = corpus.parse('bwv66.6')
>>> scoreTree = score.asTree(flatten=True)
>>> node1 = scoreTree.getNodeAfter(0.5)
>>> node1
<ElementNode: Start:1.0 <0.20...> Indices:(l:27 *29* r:33) Payload:<music21.note.Note A>>
>>> node2 = scoreTree.getNodeAfter(0.6)
>>> node2 is node1
True

>>> endNode = scoreTree.getNodeAfter(9999)
>>> endNode
<ElementNode: Start:End <0.-5...> Indices:(l:191 *195* r:199)
       Payload:<music21.bar.Barline type=final>>

>>> while endNode is not None:
...     print(endNode)
...     endNodePosition = endNode.position
...     endNode = scoreTree.getNodeAfter(endNodePosition)
<ElementNode: Start:End <0.-5...> Indices:(l:191 *195* r:199)
    Payload:<music21.bar.Barline type=final>>
<ElementNode: Start:End <0.-5...> Indices:(l:196 *196* r:197)
    Payload:<music21.bar.Barline type=final>>
<ElementNode: Start:End <0.-5...> Indices:(l:196 *197* r:199)
    Payload:<music21.bar.Barline type=final>>
<ElementNode: Start:End <0.-5...> Indices:(l:198 *198* r:199)
    Payload:<music21.bar.Barline type=final>>

>>> note1 = score.flatten().notes[30]

Works with sortTuple positions as well:

>>> st = note1.sortTuple()
>>> st
SortTuple(atEnd=0, offset=6.0, priority=0, classSortOrder=20, isNotGrace=1, insertIndex=...)

>>> scoreTree.getNodeAfter(st)
<ElementNode: Start:6.5 <0.20...> Indices:(l:55 *56* r:57)
    Payload:<music21.note.Note D>>
c                   > U c  g S nU R                   U::  a&  U R                  (       a  T" U R                  U5      nU$ XR                   :  a  T" U R                  U5      =(       d    U nU$ rX   r   r   r   )rU   r}   inner_resultrh   s      r   rh   %AVLTree.getNodeAfter.<locals>.recurse  sa    |L}}-$//&tF   .&t~~}EMr   Nrd   r   r   r<   rh   s      @r   getNodeAfterAVLTree.getNodeAfter  s(    V	  1>r   c                L    U R                  U5      nU(       a  UR                  $ g)a  
Gets start position after `position`.

>>> score = corpus.parse('bwv66.6')
>>> scoreTree = score.asTree(flatten=True)
>>> scoreTree.getPositionAfter(0.5).offset
1.0

Returns None if no succeeding position exists.

>>> endPosition = scoreTree.getPositionAfter(9999)
>>> while endPosition is not None:
...     print(endPosition)
...     endPosition = scoreTree.getPositionAfter(endPosition)
SortTuple(atEnd=1, offset=36.0, priority=0, classSortOrder=-5, ...)
SortTuple(atEnd=1, offset=36.0, priority=0, classSortOrder=-5, ...)
SortTuple(atEnd=1, offset=36.0, priority=0, classSortOrder=-5, ...)
SortTuple(atEnd=1, offset=36.0, priority=0, classSortOrder=-5, ...)

Generally speaking, negative positions will usually return 0.0

>>> scoreTree.getPositionAfter(-999).offset
0.0

Unless the Tree is empty in which case, None is returned:

>>> at = tree.core.AVLTree()
>>> at.getPositionAfter(-999) is None
True
N)r   r   r   r   rU   s      r   getPositionAfterAVLTree.getPositionAfter  s$    >   *== r   c                B   ^ U4S jmT" U R                   U5      nUc  gU$ )a7  
Finds the node immediately before position.

>>> score = corpus.parse('bwv66.6')
>>> scoreTree = score.asTimespans()

100 is beyond the end, so it will get the last node in piece.

>>> scoreTree.getNodeBefore(100)
<OffsetNode 36.0 Indices:195,195,199,199 Length:4>

>>> scoreTree.getNodeBefore(0) is None
True
c                   > U c  g S nU R                   U:  a  T" U R                  U5      =(       d    U nU$ XR                   ::  a$  U R                  (       a  T" U R                  U5      nU$ rX   r   )rU   r}   innerResultrh   s      r   rh   &AVLTree.getNodeBefore.<locals>.recurse  sa    |K}}},%doo}EM  --/DNN%dnnmDr   Nrd   r   s      @r   getNodeBeforeAVLTree.getNodeBefore  s'    	 1>r   c                D    U R                  U5      nUc  gUR                  $ )a#  
Gets the start position immediately preceding `position` in this
position-tree.

>>> score = corpus.parse('bwv66.6')
>>> scoreTree = score.asTimespans()
>>> scoreTree.getPositionBefore(100)
36.0

Return None if no preceding position exists.

>>> scoreTree.getPositionBefore(0) is None
True
N)r   r   r   s      r   getPositionBeforeAVLTree.getPositionBefore$  s&     !!(+<}}r   c                B   ^ U4S jmT" U R                   U5      U l         g)a  
Removes a node at `position` and rebalances the tree

Used internally by TimespanTree.

>>> avl = tree.core.AVLTree()
>>> avl.createNodeAtPosition(20)
>>> avl.createNodeAtPosition(10)
>>> avl.createNodeAtPosition(5)
>>> avl.createNodeAtPosition(30)
>>> avl.rootNode
<AVLNode: Start:10 Height:2 L:0 R:1>

Remove node at 30

>>> avl.removeNode(30)
>>> avl.rootNode
<AVLNode: Start:10 Height:1 L:0 R:0>

Removing a node eliminates its payload:

>>> ten = avl.getNodeByPosition(10)
>>> ten.payload = 'ten'
>>> twenty = avl.getNodeByPosition(20)
>>> twenty.payload = 'twenty'

>>> avl.removeNode(10)
>>> avl.rootNode
<AVLNode: Start:20 Height:1 L:0 R:None>
>>> avl.rootNode.payload
'twenty'

Removing a non-existent node does nothing.

>>> avl.removeNode(9.5)
>>> avl.rootNode
<AVLNode: Start:20 Height:1 L:0 R:None>

>>> for n in avl:
...     print(n, n.payload)
<AVLNode: Start:5 Height:0 L:None R:None> None
<AVLNode: Start:20 Height:1 L:0 R:None> twenty
c                  > U GbC  U R                   U:X  a  U R                  (       a  U R                  (       a  U R                  nUR                  (       a  UR                  nUR                  (       a  M  UR                  U 5        T" U R                  UR                   5      U l        U R	                  5         OU R                  =(       d    U R                  n OqU R                   U:  a)  T" U R                  U5      U l        U R	                  5         O8U R                   U:  a(  T" U R                  U5      U l        U R	                  5         U b  U R                  5       $ g rX   )r   r   r   r"   rD   rV   )rU   r}   rH   recurseRemoves      r   r   )AVLTree.removeNode.<locals>.recurseRemoved  s    ==M1~~$//#'??&00'/'9'9H '000 //5*7IZIZ*[#~~@]]]2%24>>=%QDNKKM]]]2&3DOO]&SDOKKM~~''  r   Nrd   )r   r   r   s     @r   
removeNodeAVLTree.removeNode8  s    X	(, &dmmX>r   rd   N)r   rY   rZ   r[   r\   r]   r   rw   r   rj   ry   r   r)   r   r   r   r   r   r   r_   r/   r   r   ra   ra     sR    
I I &D<.|29h40&8t#J<(B?r   ra   __main__N)r\   
__future__r   music21r   music21.exceptions21r   r   SlottedObjectMixinr   ProtoM21Objectra   r   mainTestr/   r   r   <module>r      s`    #  . 
wf'' wxc?g$$ c?N z r   