+
    ¿Ìj—  ã                   óÒ   € R t ^ RIHt ^ RIt^ RIt^ RIt^ RIt]! ]R4      '       d   ]! ]R4      t	M	]! ]R4      t	 ]
 R t ! R R]4      t ! R	 R
]4      tR#   ] d    R t
 L'i ; i)z+Red-Black tree and plain Binary Tree module)ÚdivisionNÚizip_longestÚzip_longestc                ó"   € V P                  4       # )z3A version of next() for python's that don't have it)Únext)Úiterators   &Úk/home/l0st/web/w4ttz.lol/public_html/toolz/MsgDoxxer/venv/lib/python3.14/site-packages/red_black_set_mod.pyr   r   <   s   € ð �}‰}‹Ðó    c                ó  € V RV^,
          ,          ,           RV^,
           pV\        V4      ,
          R,          pR\        V4      ,          V,           R\        \        P                  ! V4      4      ,          ,           pV# )z*Center a string within a given field widthÚ_Ng       @Ú )ÚlenÚintÚmathÚceil)ÚstringÚfield_use_widthÚfield_avail_widthÚ	field_useÚ	pad_widthÚresults   &&&   r   Úcenterr   E   sf   € à˜# °1Õ!4Õ5Õ5Ð7K¸È!Õ8KÐL€IØ"¤S¨£^Õ3°sÕ:€IØ”3�y“>Õ! IÕ-°´c¼$¿)º)ÀIÓ:NÓ6OÕ0OÕO€FØ€Mr	   c                   ó¼  a € ] tR t^Lt o RtR1tR2R ltR tR tR]	P                  R3R ltR tR	 t]t]R
 4       t]R 4       t]R 4       t]R 4       t]R 4       tR3R ltR tR tR tR tR tR tR tR tR tR t]R 4       t ]R 4       t!R t"R4R lt#R t$R t%R  t&R! t']R" 4       t(]R# 4       t)R$ t*R% t+R& t,R' t-R( t.].t/R) t0R* t1R+ t2R, t3R- t4R. t5R/ t6R0t7V t8R# )5Ú
BinaryTreea>  
A basic binary tree class.  Arbitrary data can be
associated with a tree.  A tree that is root has
parent equal to None; a tree that is leaf has left
and right children that are empty trees (sentinels).
An empty tree has left and right children equal to None.

A tree can have children (or a parent) with equal
data.
Nc                ó6   € Wn         W n        W0n        W@n        R# )a)  
Initialises instance.

@type     key: arbitrary type
@param    key: data
# placeholder
# placeholder
@type    left: L{BinaryTree} or C{None}
@param   left: left child
@type   right: L{BinaryTree} or C{None}
@param  right: right child
@type  parent: L{BinaryTree} or C{None}
@param parent: parent
N©ÚkeyÚleftÚrightÚparent)Úselfr   r   r   r   s   &&&&&r   Ú__init__ÚBinaryTree.__init__[   s   € ð ŒàŒ	ØŒ
ØŽr	   c                ó2   € V '       d   V P                   : # R# )zÕ
Returns a string representation of I{self}, containing
the key in I{self} and left and right children.  If I{self}
is a sentinel the string "sentinel" is returned.

@rtype:  C{str}
@return: string representation
Úsent©r   ©r    s   &r   Ú__repr__ÚBinaryTree.__repr__p   s   € ÷ ØŸ8›8Ð&ár	   c                ó„   €  ! R R\         4      pV! 4       pV P                  VP                  4       VP                  4       # )zZCompute the depth of the tree and the maximum width within the nodes - for pretty printingc                   ó:   a € ] tR t^€t o RtRR ltR tR tRtV t	R# )Ú0BinaryTree._depth_and_field_width.<locals>.maxerzXClass facilitates computing the max depth of the treap (tree) and max width of the nodesc                ó    € Wn         RV n        R# )é   Néÿÿÿÿ©Ú	depth_maxÚfield_width_max)r    Úmaximums   &&r   r!   Ú9BinaryTree._depth_and_field_width.<locals>.maxer.__init__‚   s   € Ø!(”Ø')�Ö$r	   c                ó–   € TpTpW0P                   8”  d   W0n         \        V4      p\        V4      pWpP                  8”  d	   Wpn        R# R# )zGCheck our maximums so far against the current node - updating as neededN)r0   Úreprr   r1   )r    Únoder   ÚdepthÚ	from_leftÚdummyÚ	repr_nodeÚlen_nodes   &&&&&   r   ÚfeedÚ5BinaryTree._depth_and_field_width.<locals>.maxer.feed†   sF   € ð �à!�ØŸ>™>Ô)Ø%*”NÜ  ›J�	Ü˜y›>�Ø×2Ñ2Ô2Ø+3Ö(ñ 3r	   c                ó@   € V P                   ^,           V P                  3# )z"Return the maximums we've computedr/   r&   s   &r   r   Ú7BinaryTree._depth_and_field_width.<locals>.maxer.result”   s   € àŸ™¨Õ*¨D×,@Ñ,@ÐAÐAr	   r/   N)r.   )
Ú__name__Ú
__module__Ú__qualname__Ú__firstlineno__Ú__doc__r!   r<   r   Ú__static_attributes__Ú__classdictcell__©Ú__classdict__s   @r   Úmaxerr+   €   s   ø‡ € Ùjô*ò4÷Bð Br	   rI   )ÚobjectÚdetailed_inorder_traversalr<   r   )r    rI   Úmax_objs   &  r   Ú_depth_and_field_widthÚ!BinaryTree._depth_and_field_width~   s8   € ô	B”Fô 	Bñ0 “'ˆØ×'Ñ'¨¯©Ô5Ø�~‰~ÓÐr	   Tc           
     ó®  € Vf   \        4       pV'       d   VP                  R4       \        V 4      : R\        V 4      : 2pWC9   d   R# VP	                  V4       V P
                  eb   VP                  RV: R\        V P
                  4      : R\        V P
                  4      : R24       V P
                  P                  RW#R7       V P                  eb   VP                  RV: R\        V P                  4      : R\        V P                  4      : R	24       V P                  P                  RW#R7       V'       d   VP                  R
4       R# R# )z(Generate a dot file describing this treeNzdigraph G {
r   z   "z" -> "z" [ label="left" ]
F)ÚinitialÚfile_Úvisitedz" [ label="right" ]
z}
)ÚsetÚwriteÚidr5   Úaddr   Úto_dotr   )r    rP   rQ   rR   Ú	this_nodes   &&&& r   rW   ÚBinaryTree.to_dotœ   sò   € ð Š?Ü“eˆGßØ�K‰K˜Ô(Ü! $žx¬¨d­Ð4ˆ	ØÔÙØ�‰�IÔØ�9‰9Ò Ø�KŠKÃ9ÌbÐQU×QZÑQZÎmÔ]aÐbf×bkÑbkÖ]lÐmÔnØ�I‰I×Ñ U°%ÐÔIØ�:‰:Ò!Ø�KŠKÃIÌrÐRV×R\ÑR\Î~Ô_cÐdh×dnÑdnÖ_oÐpÔqØ�J‰J×Ñ e°5ÐÔJßØ�K‰K˜Öñ r	   c                ó~  €  ! R R\         4      p. pV P                  4       w  r4V^,          p\        V4       F3  pRV^,
          ,          pVP                  V.^V,          ,          4       K5  	  V! V4      pV P	                  VP
                  4       . p^V^,
          ,          V,          p	\        V4       Fo  p^V,          p
Wš,          pRP                  VP                  V,           Uu. uF  p\        WÄV4      NK  	  up4      pVP                  RV,          V,           4       Kq  	  RP                  V4      # u upi )zFormat a tree as a stringc                   ó0   a € ] tR t^²t o RtR tR tRtV tR# )Ú BinaryTree.__str__.<locals>.Descz8Build a pretty-print string during a recursive traversalc                ó   € Wn         R # ©N©Útree)r    Úpretrees   &&r   r!   Ú)BinaryTree.__str__.<locals>.Desc.__init__¶   s   € Ø#–	r	   c                óH   € Tp\        V4      V P                  V,          V&   R# )zAdd a node to the treeN)r5   r`   )r    r6   r   r7   r8   r9   s   &&&&& r   ÚupdateÚ'BinaryTree.__str__.<locals>.Desc.update¹   s    € ð �ä.2°4«j�—	‘	˜%Õ  Ó+r	   r_   N)	r@   rA   rB   rC   rD   r!   rd   rE   rF   rG   s   @r   ÚDescr\   ²   s   ø‡ € ñ Kò$÷9ð 9r	   rf   r   Ú z%2d Ú
)	rJ   rM   ÚrangeÚappendrK   rd   Újoinr`   r   )r    rf   ra   r7   r   Úlevelr   Údescr   ÚwidestÚ	two_levelr   Úxs   &            r   Ú__str__ÚBinaryTree.__str__°   s  € ô	9”6ô 	9ð ˆØ!%×!<Ñ!<Ó!>ÑˆØ˜1ÕˆÜ˜5–\ˆEØ˜O¨aÕ/Õ0ˆFØ�N‰N˜F˜8 a¨5¥jÕ0Ö1ñ "ñ �G‹}ˆØ×'Ñ'¨¯©Ô4ØˆØ�u˜q•yÕ! OÕ3ˆÜ˜5–\ˆEØ˜U�
ˆIØ &Õ 2ÐØ—W‘WÐUY×U^ÑU^Ð_dÖUeÓfÑUeÐPQœf QÐ9JÖKÑUeÑfÓgˆFà�M‰M˜6 E�>¨VÕ3Ö4ñ "ð �y‰y˜Ó Ð ùò gs   Ã*D:
c                óT   € V P                   RJ ;'       d    V P                  RJ '       * # )z‚
A sentinel node evaluates to False.

@rtype:  C{bool}
@return: True if neither left nor right are None,
         otherwise False
N)r   r   r&   s   &r   Ú__nonzero__ÚBinaryTree.__nonzero__Ó   s'   € ð —I‘I Ð%×<Ð<¨$¯*©*¸Ð*<Ô=Ð=r	   c                ó   € V P                   RJ # )zf
Returns True if I{self} is root.

@rtype:  C{bool}
@return: True if I{self} is root, otherwise False
N©r   r&   s   &r   Úis_rootÚBinaryTree.is_rootß   s   € ð �{‰{˜dÐ"Ð"r	   c                ó´   € V P                   '       d   R# W P                  P                  J d   V P                  P                  # V P                  P                  # )zU
Returns the sibling of I{self}.

@rtype:  L{BinaryTree} or C{None}
@return: sibling
N)rx   r   r   r   r&   s   &r   ÚsiblingÚBinaryTree.siblingé   s@   € ð �<�<ˆ<ÙØ—[‘[×%Ñ%Ó%Ø—;‘;×$Ñ$Ð$à—;‘;×#Ñ#Ð#r	   c                ó†   € V '       d9   V P                   P                  V P                  P                  ,           ^,           # ^ # )z„
Return the number of key items / non-sentinel
nodes in the subtree rooted at I{self}.

@rtype:  C{int}
@return: number of elements
)r   Úsizer   r&   s   &r   r~   ÚBinaryTree.sizeù   s*   € ÷ Ø—9‘9—>‘> D§J¡J§O¡OÕ3°aÕ7Ð7Ùr	   c                óŒ   € V '       d<   \        V P                  P                  V P                  P                  4      ^,           # ^ # )zk
Returns the number of levels in the subtree
rooted at I{self}.

@rtype:  C{int}
@return: number of levels
)Úmaxr   Ú
num_levelsr   r&   s   &r   r‚   ÚBinaryTree.num_levels  s0   € ÷ Ü�t—y‘y×+Ñ+¨T¯Z©Z×-BÑ-BÓCÀaÕGÐGÙr	   c                ó(   € V P                   ^,
          # )z„
Returns the height of the subtree rooted at
I{self}.  This is the number of levels - 1.

@rtype:  C{int}
@return: number of levels
)r‚   r&   s   &r   ÚheightÚBinaryTree.height  s   € ð �‰ Õ"Ð"r	   c                óø   € V '       d`   V P                  V P                  VR7      pV P                  P                  V4      Vn        V P                  P                  V4      Vn        V# V P                  VR7      pV# )zÁ
Returns a shallow copy of the subtree rooted
at I{self} (does not copy data).

@type  parent: L{BinaryTree} or C{None}
@param parent: parent
@rtype:        L{BinaryTree}
@return:       a copy
)r   r   rw   )Ú	__class__r   r   Úcopyr   )r    r   r‰   s   && r   r‰   ÚBinaryTree.copy  sc   € ÷ Ø—>‘> d§h¡h°v�>Ó>ˆDØŸ	™	Ÿ™ tÓ,ˆDŒIØŸ™Ÿ™¨Ó.ˆDŒJð ˆð —>‘>¨�>Ó0ˆDØˆr	   c                ó>  € V P                   '       Ed   V P                  '       d�   \        P                  ! 4       R8  d:   V P                   P                  pVP                  V n        VP                  4        R# V P                  P                  pVP                  V n        VP                  4        R# V P                   pVP                  VP                   VP                  uV n        V n         V n        W P                   n        W P                  n        R# V P                  '       dc   V P                  pVP                  VP                   VP                  uV n        V n         V n        W P                   n        W P                  n        R# R;V n        ;V n         V n        R# )z©
Deletes a node from the subtree rooted at I{self},
but does not delete the subtree rooted at node.  To
delete a node on the basis of its data use the
I{remove} method.
ç      à?N)r   r   Úrandomr2   r   Údel_nodeÚminimumr   ©r    r6   s   & r   rŽ   ÚBinaryTree.del_node0  s
  € ð �9�9‰9Ø�z�zˆzô —=’=“? SÔ(ØŸ9™9×,Ñ,�DØ#Ÿx™x�D”Hà—M‘M–OàŸ:™:×-Ñ-�DØ#Ÿx™x�D”Hà—M‘M–Oà—y‘y�Ø26·(±(¸D¿I¹IÀtÇzÁzÐ/�”˜$œ) T¤Zà#'—	‘	Ô Ø$(—
‘
Ö!Ø�Z�ZˆZØ—:‘:ˆDØ.2¯h©h¸¿	¹	À4Ç:Á:Ð+ˆDŒH�d”i ¤à#�I‰IÔØ $�J‰JÖð 15Ð4ˆDŒHÐ4�t”y 4¦:r	   c                ó”  € V P                  V4      pV'       g:   Wn        V P                  VR7      V P                  VR7      uVn        Vn        RV3# VP                  V8X  d   Wn        RV3# \
        P
                  ! 4       R8  d"   \        P                  VP                  VR7      # \        P                  VP                  VR7      # )zã
Adds a node containing I{key} to the subtree
rooted at I{self}, returning the added node.

@type  key: arbitrary type
@param key: key
placeholder
placeholder
@rtype:      L{BinaryTree}
@return:     (replaced flag, added node)
rw   FTrŒ   r%   )Úfindr   rˆ   r   r   r�   r   rV   ©r    r   r6   s   && r   rV   ÚBinaryTree.addV  s    € ð �y‰y˜‹~ˆßØŒHà$(§N¡N¸$ NÓ$?ÀÇÁÐW[ÀÓA\Ð!ˆDŒI�t”zØ˜4�=Ð Ø�X‰X˜Œ_ØŒHà˜$�<Ðä�}Š}‹ Ô$Ü!—~‘~ d§i¡i°S�~Ó9Ð9ä!—~‘~ d§j¡j°c�~Ó:Ð:r	   c                ón   € V P                  V4      pV'       d   VP                  4        R# \        R4      h)a  
Removes a node containing I{key} from the
subtree rooted at I{self}, raising an error
if the subtree does not contain I{key}.

@type        key: arbitrary type
@param       key: data
@raise ValueError: if key is not contained in the
                   subtree rooted at I{self}
útree.remove(x): x not in treeN)r“   rŽ   Ú
ValueErrorr”   s   && r   ÚremoveÚBinaryTree.remover  s)   € ð �y‰y˜‹~ˆßØ�M‰MŽOäÐ<Ó=Ð=r	   c                ó|   € V P                   pV'       d   VP                  pVP                  4        V# \        R4      h)zÓ
Removes minimum node from subtree rooted at I{self},
raising an exception if the subtree does not have any
nodes.

@raise ValueError: if key is not contained in the
                   subtree rooted at I{self}
r—   )r�   r   rŽ   r˜   ©r    r6   r   s   &  r   Ú
remove_minÚBinaryTree.remove_minƒ  ó5   € ð �|‰|ˆßð —X‘XˆFØ�M‰MŒOØˆMäÐ<Ó=Ð=r	   c                ó|   € V P                   pV'       d   VP                  pVP                  4        V# \        R4      h)zÓ
Removes maximum node from subtree rooted at I{self},
raising an exception if the subtree does not have any
nodes.

@raise ValueError: if key is not contained in the
                   subtree rooted at I{self}
r—   )r2   r   rŽ   r˜   rœ   s   &  r   Ú
remove_maxÚBinaryTree.remove_max–  rŸ   r	   c                óN   €  V P                  V4       R#   \         d     R# i ; i)z©
Discards a node containing I{key} from the subtree
rooted at I{self} (without raising an error if the key
is not present).

@type  key: arbitrary type
@param key: data
N)r™   r˜   ©r    r   s   &&r   ÚdiscardÚBinaryTree.discard©  s%   € ð	Ø�K‰K˜ÖøÜô 	Úð	ús   ‚ •$£$c                óÌ   € V '       d\   V P                  V4      pV'       dC   VP                  P                  V4      VP                  P                  V4      ,           ^,           # ^ # )z¨
Returns the number of occurrences of I{key} in
the subtree rooted at I{self}.

@type  key: arbitrary type
@param key: data
@rtype:      C{int}
@return:     data count
)r“   r   Úcountr   r”   s   && r   r¨   ÚBinaryTree.count·  sF   € ÷ Ø—9‘9˜S“>ˆDßØ—y‘y—‘ sÓ+¨d¯j©j×.>Ñ.>¸sÓ.CÕCÀaÕGÐGÙr	   c                ó¼   € \        V 4      \        V4      ,           p\        P                  ! V4       V P                  4       pV F  pVP	                  V4       K  	  V# )aP  
Returns a tree with elements from the subtree
rooted at I{self} and I{iterable}.  I{iterable}
may be any iterable, not necessarily another
tree.

@type  iterable: any iterable type
@param iterable: an iterable object
@rtype:          L{BinaryTree}
@return:         tree containing the elements in I{self}
                 and iterable
)Úlistr�   Úshufflerˆ   rV   )r    ÚiterableÚitemsr`   Úitems   &&   r   Ú__add__ÚBinaryTree.__add__Ç  sH   € ô �T“
œT (›^Õ+ˆÜ�Š�uÔØ�~‰~ÓˆÛˆDØ�H‰H�TŽNñ àˆr	   c                ó|   € \        V4      p\        P                  ! V4       V F  pV P                  V4       K  	  R# )zŒ
Adds the elements in I{iterable} to the
subtree rooted at I{self}.

@type  iterable: any iterable type
@param iterable: an iterable object
N)r«   r�   r¬   rV   )r    r­   r®   r¯   s   &&  r   Ú__iadd__ÚBinaryTree.__iadd__Û  s.   € ô �X“ˆÜ�Š�uÔÛˆDØ�H‰H�TŽNó r	   c                óÈ   € V '       dZ   V P                   V8X  d   V # WP                   8  d   V P                  P                  V4      # V P                  P                  V4      # V # )a?  
Finds and returns a node containing I{key} in the
subtree rooted at I{self}.  If I{key} is not in
the tree, then a sentinel in the location where
I{key} can be inserted is returned.

@type  key: arbitrary type
@param key: data
@rtype:      L{BinaryTree}
@return:     node containing key, or sentinel
             node
)r   r   r“   r   r¤   s   &&r   r“   ÚBinaryTree.findè  sK   € ÷ Ø�x‰x˜3ŒØ�Ø—x‘x”Ø—y‘y—~‘~ cÓ*Ð*à—z‘z—‘ sÓ+Ð+Øˆr	   c                ó,  € V P                   '       d   V P                   P                  # V P                  '       g   R# W P                  P                  J d   V P                  # Y P                  r!V'       d   VP                   VJ d   Y"P                  r!K&  V# )z‰
Returns the predecessor of I{self}, or None if
self has no predecessor.

@rtype:  L{BinaryTree} or None
@return: predecessor of I{self}
N)r   r2   r   r   ©r    Úcurrentr   s   &  r   ÚpredecessorÚBinaryTree.predecessorþ  sh   € ð �9�9ˆ9Ø—9‘9×$Ñ$Ð$à—;—;�;ÙØ—{‘{×(Ñ(Ó(Ø—{‘{Ð"à"&¯©˜ß §¡°Ó!7Ø&,¯m©mšVØ�r	   c                ó,  € V P                   '       d   V P                   P                  # V P                  '       g   R# W P                  P                  J d   V P                  # Y P                  r!V'       d   VP                   VJ d   Y"P                  r!K&  V# )zƒ
Returns the successor of I{self}, or None if
self has no successor.

@rtype:  L{BinaryTree} or None
@return: successor of I{self}
N)r   r�   r   r   r¸   s   &  r   Ú	successorÚBinaryTree.successor  sh   € ð �:�:ˆ:Ø—:‘:×%Ñ%Ð%à—;—;�;ÙØ—{‘{×'Ñ'Ó'Ø—{‘{Ð"à"&¯©˜ß §¡°Ó!8Ø&,¯m©mšVØ�r	   c              #  ó
  "  € V '       dw   V P                   '       d&   V P                   P                  4        F  pVx € K	  	  V x € V P                  '       d(   V P                  P                  4        F  pVx € K	  	  R# R# R# 5i)z—
Returns a generator of nodes in the subtree
rooted at I{self} in sorted order of node keys.

@rtype:  C{generator}
@return: generator of sorted nodes
N)r   Úin_orderr   r�   s   & r   rÀ   ÚBinaryTree.in_order*  sc   é € ÷ Ø�y�yˆyØ ŸI™I×.Ñ.Ö0�DØ”Jñ 1àŠJØ�z�zˆzØ ŸJ™J×/Ñ/Ö1�DØ”Jó 2ñ ñ ùs   ‚B�:BÁ+Bc                ó.  € V P                   '       d+   V P                   P                  W^,           V^,          4       V! W P                  W#4       V P                  '       d4   V P                  P                  W^,           V^,          ^,           4       R# R# )z1Do an inorder traversal - with lots of parametersN)r   rK   r   r   )r    Úvisitr7   r8   s   &&&&r   rK   Ú%BinaryTree.detailed_inorder_traversal;  sf   € à�9�9ˆ9Ø�I‰I×0Ñ0°À½	À9ÈqÅ=ÔQÙˆd—H‘H˜eÔ/Ø�:�:ˆ:Ø�J‰J×1Ñ1°%À½ÀIÐPQÅMÐTUÕDUÖVñ r	   c                ó0   € R V P                  4        4       # )z˜
Returns a generator of the node keys in
the subtree rooted at I{self} in sorted
order.

@rtype:  C{generator}
@return: generator of sorted node values
c              3   ó8   "  € T F  qP                   x € K  	  R # 5ir^   r%   )Ú.0Úns   & r   Ú	<genexpr>Ú&BinaryTree.__iter__.<locals>.<genexpr>L  s   é € Ð/™˜!—–›ùs   ‚)rÀ   r&   s   &r   Ú__iter__ÚBinaryTree.__iter__C  s   € ñ 0˜tŸ}™}œÓ/Ð/r	   c              #  ó¾   "  € V '       dQ   V P                   P                  4        F  pVx € K	  	  V x € V P                  P                  4        F  pVx € K	  	  R# R# 5i)z§
Returns a generator of nodes in the subtree
rooted at I{self} in reverse sorted order
of node keys.

@rtype:  C{generator}
@return: generator of reverse sorted nodes
N)r   Ú
in_reverser   r�   s   & r   rÎ   ÚBinaryTree.in_reverseN  sL   é € ÷ ØŸ
™
×-Ñ-Ö/�Ø”
ñ 0àŠJØŸ	™	×,Ñ,Ö.�Ø”
ó /ñ	 ùs   ‚AAc              #  ó
  "  € V '       dw   V x € V P                   '       d&   V P                   P                  4        F  pVx € K	  	  V P                  '       d(   V P                  P                  4        F  pVx € K	  	  R# R# R# 5i)z�
Returns a generator of the nodes in the
subtree rooted at I{self} in preorder.

@rtype:  C{generator}
@return: generator of nodes in preorder
N)r   Ú	pre_orderr   r�   s   & r   rÑ   ÚBinaryTree.pre_order^  sc   é € ÷ ØŠJØ�y�yˆyØ ŸI™I×/Ñ/Ö1�DØ”Jñ 2à�z�zˆzØ ŸJ™J×0Ñ0Ö2�DØ”Jó 3ñ ñ ùs   ‚B¡6BÁ+Bc              #  ó  "  € V '       du   V P                   '       d&   V P                   P                  4        F  pVx € K	  	  V P                  '       d&   V P                  P                  4        F  pVx € K	  	  V x € R# R# 5i)z‘
Returns a generator of the nodes in the
subtree rooted at I{self} in postorder.

@rtype:  C{generator}
@return: generator of nodes in postorder
N)r   Ú
post_orderr   r�   s   & r   rÔ   ÚBinaryTree.post_ordero  s^   é € ÷ Ø�y�yˆyØ ŸI™I×0Ñ0Ö2�DØ”Jñ 3à�z�zˆzØ ŸJ™J×1Ñ1Ö3�DØ”Jñ 4àŒJñ ùs   ‚B�6BÁ-Bc                óJ   € T pVP                   '       d   VP                   pK   V# )zÍ
Returns a node for which the node key is minimum
in the subtree rooted at I{self}.  Always returns
the minimum-valued node with no left child.

@rtype:  L{BinaryTree}
@return: node with minimum key value
)r   r�   s   & r   r�   ÚBinaryTree.minimum€  s    € ð ˆØ�i�iˆiØ—9‘9ŠDØˆr	   c                óJ   € T pVP                   '       d   VP                   pK   V# )zÎ
Returns a node for which the node key is maximum
in the subtree rooted at I{self}.  Always returns
the maximum-valued node with no right child.

@rtype:  L{BinaryTree}
@return: node with maximum key value
)r   r�   s   & r   r2   ÚBinaryTree.maximum�  s    € ð ˆØ�j�jˆjØ—:‘:ŠDØˆr	   c                ó2   € V P                   pVP                  # )z%
Return the miniumum key in the tree
)r�   r   r�   s   & r   Úfind_minÚBinaryTree.find_minž  ó   € ð �|‰|ˆØ�x‰xˆr	   c                ó2   € V P                   pVP                  # )z%
Return the maxiumum key in the tree
)r2   r   r�   s   & r   Úfind_maxÚBinaryTree.find_max¥  rÝ   r	   c                óî   € V '       g   V'       g   R# V '       d[   V'       dS   V P                   P                  VP                   4      ;'       d&    V P                  P                  VP                  4      # R# )z´
Two binary trees are similar if they have the
same structure.

@rtype:  C{bool}
@return: True if the subtrees rooted at I{self}
         and I{other} are similar, otherwise False
TF)r   Ú
is_similarr   ©r    Úothers   &&r   râ   ÚBinaryTree.is_similar¬  sH   € ÷ ŸEÙß—EØ—9‘9×'Ñ'¨¯
©
Ó3×ZÐZ¸¿
¹
×8MÑ8MÈeÏkÉkÓ8ZÐZÙr	   c                ó0  € V '       g   V'       g   R# V '       d|   V'       dt   V P                   VP                   8H  ;'       dS    V P                  P                  VP                  4      ;'       d&    V P                  P                  VP                  4      # R# )zÜ
Two binary trees are equivalent if they are similar
and corresponding nodes contain the same keys.

@rtype:  C{bool}
@return: True if the subtrees rooted at I{self}
         and I{other} are equivalent, otherwise False
TF)r   r   Úis_equivalentr   rã   s   &&r   rç   ÚBinaryTree.is_equivalent»  sm   € ÷ ŸEÙß—Eà—‘˜EŸI™IÑ%÷ 6ð 6à—	‘	×'Ñ'¨¯
©
Ó3÷6ð 6ð —
‘
×(Ñ(¨¯©Ó5ð	ñ r	   c                ó   € \        V P                  4       VP                  4       4       FQ  w  r#Vf   Ru # Vf    ^# VP                  VP                  8  d   Ru # VP                  VP                  8”  g   KP   ^# 	  ^ # )a‚  
Returns C{0} if the subtrees rooted at
I{self} and I{other} contain equal keys.
I{other} need not be similar to I{self}.

@type  other: L{BinaryTree}
@param other: tree being compared to I{self}
@rtype:       C{int}
@return:      0 if the subtrees rooted at I{self}
              and I{other} do contain equal values,
              -1 if self < other.
              1 if self > other.
r.   )ÚMY_ZIP_LONGESTrÀ   r   )r    rä   Ú	self_nodeÚ
other_nodes   &&  r   Ú__cmp__ÚBinaryTree.__cmp__í  sg   € ô &4°D·M±M³OÀUÇ^Á^ÓEUÖ%VÑ!ˆIØÒ Ø’	ØÒ#ÚØ—‘ §¡Ô/Ø’	Ø—‘ §¡Ö/Úñ &Wñ r	   c                ó*   € V P                  V4      ^ 8H  # )aa  
Returns C{True} if the subtrees rooted at
I{self} and I{other} contain equal keys.
I{other} need not be similar to I{self}.

@type  other: L{BinaryTree}
@param other: tree being compared to I{self}
@rtype:       C{bool}
@return:      True if the subtrees rooted at I{self}
              and I{other} contain equal values, otherwise
              False
©Úcmprã   s   &&r   Ú__eq__ÚBinaryTree.__eq__	  ó   € ð �x‰x˜‹ !Ñ#Ð#r	   c                ó*   € V P                  V4      ^ 8g  # ©ao  
Returns C{True} if the subtrees rooted at
I{self} and I{other} do not contain equal keys.
I{other} need not be similar to I{self}.

@type  other: L{BinaryTree}
@param other: tree being compared to I{self}
@rtype:       C{bool}
@return:      True if the subtrees rooted at I{self}
              and I{other} do not contain equal values,
              otherwise False
rð   rã   s   &&r   Ú__neq__ÚBinaryTree.__neq__  rô   r	   c                ó*   € V P                  V4      ^ 8  # rö   rð   rã   s   &&r   Ú__lt__ÚBinaryTree.__lt__'  s   € ð �x‰x˜‹ Ñ"Ð"r	   c                ó*   € VP                  V 4      ^ 8  # ©é    rð   rã   s   &&r   Ú__gt__ÚBinaryTree.__gt__6  s   € Ø�y‰y˜‹ Ñ"Ð"r	   c                ó*   € V P                  V4      ^ 8*  # rý   rð   rã   s   &&r   Ú__le__ÚBinaryTree.__le__9  ó   € Ø�x‰x˜‹ !Ñ#Ð#r	   c                ó*   € V P                  V4      ^ 8¬  # rý   rð   rã   s   &&r   Ú__ge__ÚBinaryTree.__ge__<  r  r	   c                ó6   € \        V P                  V4      4      # )zé
Returns true if I{key} is stored in any node in
the subtree rooted at I{self}.

@type  key: arbitrary type
@param key: data
@rtype:      C{bool}
@return:     True if I{key} in subtree rooted at
             I{self}, otherwise False
)Úboolr“   r¤   s   &&r   Ú__contains__ÚBinaryTree.__contains__?  s   € ô �D—I‘I˜c“NÓ#Ð#r	   )r   r   r   r   r   ©NNNNr^   )rþ   rþ   )9r@   rA   rB   rC   rD   Ú	__slots__r!   r'   rM   ÚsysÚstdoutrW   rq   rt   Ú__bool__Úpropertyrx   r{   r~   r‚   r…   r‰   rŽ   rV   r™   r�   r¡   r¥   r¨   r°   r³   r“   rº   r½   rÀ   rK   rË   rÎ   rÑ   rÔ   r�   r2   rÛ   rß   râ   rç   rí   rñ   rò   r÷   rú   rÿ   r  r  r
  rE   rF   rG   s   @r   r   r   L   s–  ø‡ € ñ	ð 3€Iôò*ò ð< "¨¯©¸Tô ò(!!òF>ð €Hàñ#ó ð#ð ñ$ó ð$ð ñ
ó ð
ð ñ
ó ð
ð ñ#ó ð#ôò$#5òL;ò8>ò">ò&>ò&òò ò(òð, ñó ðð* ñó ðò*ô"Wò	0òò ò"ð" ñó ðð ñó ðòòòòòdð4 €Cò$ò$ò#ò#ò$ò$÷$ð $r	   r   c                   óð   a a€ ] tR tRt oRt]P                  R,           tRV 3R llt]R 4       t	]	P                  R 4       t	]R 4       tR tRV 3R lltV 3R	 ltR
 tR tR tR tR tR tR tRtVtV ;t# )ÚRedBlackTreeiM  a¯  
A binary tree with an extra attribute, is_red.  Colour (red
or black) is used to maintain a reasonably balanced tree.
Changes to the tree structure (rotations) are carried out
in such a way that the name bound to the initial empty
tree will always refer to the root node.  The data stored
in a given node may change, as well as its connectivity.
e.g.

>>> import trees
>>> tree = trees.RedBlackTree()
>>> tree.add(2)
2 False None None
>>> tree.key        # tree is bound to root node
2
>>> tree.add(-1)
-1 True None None
>>> tree.add(1)     # tree rotations are performed at this point
1 False -1 2
>>> tree.key        # the root node has changed (i.e. its key has been changed)
1
>>>

c                ó>   <€ \         \        V `  WW44       RV n        R# )a  
Initialises instance.

@type     key: arbitrary type
@param    key: data
@type    left: L{RedBlackTree} or C{None}
@param   left: left child
@type   right: L{RedBlackTree} or C{None}
@param  right: right child
@type  parent: L{RedBlackTree} or C{None}
@param parent: parent
FN)Úsuperr  r!   Úis_red)r    r   r   r   r   rˆ   s   &&&&&€r   r!   ÚRedBlackTree.__init__j  s   ø€ ô 	Œl˜DÑ*¨3°eÔDØˆŽr	   c                ó$   € V P                   '       * # )zp
Returns True if node colour is black.

@rtype:  C{bool}
@return: True if node colour is black, otherwise False
©r  r&   s   &r   Úis_blackÚRedBlackTree.is_blackz  s   € ð —;‘;ŒÐr	   c                ó   € V'       * V n         R# )z
Sets node colour.
Nr  )r    Úvals   &&r   r  r  „  s   € ð
 ”gˆŽr	   c                óŠ  € V P                   '       dP   V P                   P                  '       d   V P                   P                  ^,           # V P                   P                  # V P                  '       dP   V P                  P                  '       d   V P                  P                  ^,           # V P                  P                  # ^ # )zD
@rtype:  C{int}
@return: black height of subtree rooted at I{self}
)r   r  Úblack_heightr   r&   s   &r   r  ÚRedBlackTree.black_height‹  sƒ   € ð �9�9ˆ9Ø�y‰y×!×!Ð!Ø—y‘y×-Ñ-°Õ1Ð1à—y‘y×-Ñ-Ð-Ø�Z�ZˆZØ�z‰z×"×"Ð"Ø—z‘z×.Ñ.°Õ2Ð2à—z‘z×.Ñ.Ð.ár	   c                ój   € V '       d+   V P                   '       d   RpMRpV P                  : RV: 2# R# )zí
Returns a string representation of I{self}, containing
the key in I{self}, the colour, and the key in left and
right children.  If I{self} is a sentinel the string
"sentinel" is returned.

@rtype:  C{str}
@return: string representation
ÚredÚblkr   r$   )r  r   )r    Úcolor_strings   & r   r'   ÚRedBlackTree.__repr__ž  s,   € ÷ Ø�{�{ˆ{Ø$‘à$�Ø"ŸhœhªÐ5Ð5ár	   c                óP   <€ \         \        V `  V4      pV P                  Vn        V# )zÄ
Returns a shallow copy of the subtree rooted
at I{self} (does not copy key).

@type  parent: L{RedBlackTree} or C{None}
@param parent: parent
@rtype:        L{RedBlackTree}
@return:       a copy
)r  r  r‰   r  )r    r   r‰   rˆ   s   && €r   r‰   ÚRedBlackTree.copy±  s&   ø€ ô ”\ 4Ñ-¨fÓ5ˆØ—k‘kˆŒØˆr	   c                ór   <€ \         \        V `  VR7      w  r#V'       g   RVn        VP	                  4       pV# )a+  
Adds a node containing I{key} to the subtree
rooted at I{self}, returning the node which
contains I{key}.  Note: the returned node might
be an existing node due to key swaps on rotations.

@type  key: arbitrary type
@param key: data
@rtype:      L{RedBlackTree}
@return:     node containing I{key}
r%   T)r  r  rV   r  Ú
fix_insert)r    r   Úreplacedr6   rˆ   s   &&  €r   rV   ÚRedBlackTree.add¿  s7   ø€ ô !¤¨tÑ8¸SÐ8ÓAÑˆßØˆDŒKØ—?‘?Ó$ˆDØˆr	   c                ól  € V P                   f
   RV n        V # V P                   P                  '       d   V # V P                   P                  pVP                  '       dH   RV P                   n        RVn        V P                   P                   pRVn        VP	                  4        V # V P                   P                   pV P                   P
                  V J VP
                  V P                   J 8X  g-   V P                  4        V P                   P                  4        V# V P                   P                  4        V # )ag  
Carries out tree rotations, key swaps and recolourings
to ensure that the red / black properties of the tree
are preserved on node insertion. The returned node contains
the same value as I{self} (but might not be the inserted node
due to key swaps / rotations).

@rtype:  L{RedBlackTree}
@return: node containing key originally
         contained in I{self}
T)r   r  r{   r  r)  r   Úrotate)r    ÚsibÚpars   &  r   r)  ÚRedBlackTree.fix_insertÑ  så   € ð �;‰;ÒØ ˆDŒMØˆKà�;‰;××ÐØˆKà�k‰k×!Ñ!ˆØ�:�:ˆ:Ø#'ˆD�K‰KÔ ØˆCŒLØ—+‘+×$Ñ$ˆCØˆCŒJØ�N‰NÔØˆKà�k‰k× Ñ ˆØ—‘× Ñ  DÐ(¨c¯h©h¸$¿+¹+Ð.EÔFØ�K‰KŒMØ�K‰K×ÑÔ ØˆJà�K‰K×ÑÔ ØˆKr	   c                ó�  € W P                   P                  J dØ   V P                   P                  V P                  uV n        V P                   n        V P                  V P                   n        V P                   V P                   P                  n         V P                  V n        V P                   P                  V n        V ;V P                  n         V P                  n         W P                   n        R# V P                   P                  V P                  uV n        V P                   n        V P                  V P                   n        V P                   V P                   P                  n         V P                  V n        V P                   P                  V n        V ;V P                  n         V P                  n         W P                   n        R# )zí
Rotates I{self} with I{self.parent}.  Left / right rotations
are performed as dictated by the parent-child relationship.
The colours of I{self} and I{self.parent} are implicity swapped
(as their values are swapped during the rotation).
N)r   r   r   r   r&   s   &r   r-  ÚRedBlackTree.rotate÷  s  € ð —;‘;×#Ñ#Ó#Ø(,¯©¯©¸¿¹Ð%ˆDŒH�d—k‘k”oà#Ÿy™yˆD�K‰KÔØ&*§k¡kˆD�K‰K×ÑÔ#ØŸ
™
ˆDŒIØŸ™×*Ñ*ˆDŒJØ37Ð7ˆD�I‰IÔ˜tŸz™zÔ0Ø $�K‰KÖà(,¯©¯©¸¿¹Ð%ˆDŒH�d—k‘k”oà $§
¡
ˆD�K‰KÔØ'+§{¡{ˆD�K‰K×ÑÔ$ØŸ™ˆDŒJØŸ™×(Ñ(ˆDŒIØ37Ð7ˆD�I‰IÔ˜tŸz™zÔ0Ø#�K‰KÖr	   c                óœ  € V P                   '       dø   V P                  '       d}   \        P                  ! 4       R8  d   V P                   P                  pMV P                  P                  pVP
                  V P
                  uV n        Vn        VP                  4        R# V P                   pVP
                  VP                   VP                  uV n        V n         V n        W P                   n        W P                  n        RV n        R# V P                  '       dj   V P                  pVP
                  VP                   VP                  uV n        V n         V n        W P                   n        W P                  n        RV n        R# R;V n        ;V n         V n        V P                  '       d
   RV n        R# RV n        V P                  4        R# )z¨
Deletes a node from the subtree rooted at I{self},
but does not delete the subtree rooted at node.  To
delete a node on the basis of its key use the
I{remove} method.
rŒ   TN)r   r   r�   r2   r�   r   rŽ   r   r  r  Úfix_delr�   s   & r   rŽ   ÚRedBlackTree.del_node  s1  € ð �9�9ˆ9Ø�z�zˆzô —=’=“? SÔ(ØŸ9™9×,Ñ,‘DàŸ:™:×-Ñ-�DØ%)§X¡X¨t¯x©xÐ"�”˜$œ(à—‘–à—y‘y�Ø26·(±(¸D¿I¹IÀtÇzÁzÐ/�”˜$œ) T¤Zà#'—	‘	Ô Ø$(—
‘
Ô!Ø $�–Ø�Z�ZˆZØ—:‘:ˆDØ.2¯h©h¸¿	¹	À4Ç:Á:Ð+ˆDŒH�d”i ¤à#�I‰IÔØ $�J‰JÔØ ˆDŽMð 15Ð4ˆDŒHÐ4�t”y 4¤:à�{�{ˆ{Ø $�–à $�”à—‘–r	   c                óì  € V P                   f   R# V P                  pVP                  '       d   VP                  4        V P                  pV P                   P                  '       dn   VP                  '       d\   VP
                  P                  '       d@   VP                  P                  '       d$   RVn        V P                   P                  4        R# V P                   P                  '       de   VP                  '       dS   VP
                  P                  '       d7   VP                  P                  '       d   RVn        RV P                   n        R# VP                  '       dä   W P                   P
                  J dT   VP                  P                  '       d8   VP
                  P                  '       d   VP
                  P                  4        MkW P                   P                  J dS   VP
                  P                  '       d7   VP                  P                  '       d   VP                  P                  4        V P                  pVP                  4        RV P                   P                  n        R# )z2
Maintains red / black property on node
deletion.
NT)r   r{   r  r-  r  r   r   r4  )r    r.  s   & r   r4  ÚRedBlackTree.fix_del?  s  € ð �;‰;ÒÙØ�l‰lˆà�:�:ˆ:Ø�J‰JŒLØ—,‘,ˆCð —‘×$×$Ð$Ø——�Ø—‘×!×!Ð!Ø—	‘	×"×"Ð"àˆCŒJØ�K‰K×ÑÔ!Ùð —‘×"×"Ð"Ø——�Ø—‘×!×!Ð!Ø—	‘	×"×"Ð"àˆCŒJØ#'ˆD�K‰KÔ Ùà�<�<ˆ<Ø—{‘{×'Ñ'Ó'¨C¯I©I×,>×,>Ð,>À3Ç8Á8Ç?Ç?À?Ø—‘—‘Õ!ØŸ™×*Ñ*Ó*¨s¯x©x×/@×/@Ð/@ÀSÇYÁY×EU×EUÐEUØ—	‘	× Ñ Ô"Ø—,‘,ˆCà�
‰
ŒØ'+ˆ�‰×ÑÖ$r	   c                óˆ  € . pV P                  4        F‡  pVP                  '       g$   VP                  '       g   VP                  V4       VP                  '       g   KL  VP                  P
                  '       d   VP                  P
                  '       d   K†   R# 	  ^ pV'       d›   V^ ,          pVP                  '       d*   VP
                  '       d
   V^,          pVP                  pK;  VR,           FI  p^ pVP                  '       d*   VP
                  '       d
   V^,          pVP                  pK;  WC8X  d   KH   R# 	  R# )z¾
Checks whether the subtree rooted at I{self} is a valid
red black tree U{http://en.wikipedia.org/wiki/Red_black_tree}.

@rtype:  C{bool}
@return: True if the tree is valid, otherwise False
F:r-   NNT)rÀ   r   r   rj   r  r  r   )r    Ú
leaf_nodesr6   r  r…   s   &    r   ÚcheckÚRedBlackTree.checkk  sâ   € ð ˆ
Ø—M‘M–OˆDØ—9—9�9 T§Z§Z ZØ×!Ñ! $Ô'Ø�{�{Š{ØŸ	™	×*×*Ð*¨t¯z©z×/B×/BÒ/BÚ ñ $ð ˆßØ˜a•=ˆDØ—+—+�+Ø—=—=�=Ø  AÕ%�LØ—{‘{’Ø" 2Ÿ˜�Ø�Ø—k—k�kØ—}—}�}Ø !�˜ØŸ;™;’DØÖ-Ú ñ 'ñ r	   c                ó   € V P                   # )z.Return the number of elements in the container)r~   r&   s   &r   Ú__len__ÚRedBlackTree.__len__°  s   € ð �y‰yÐr	   c                ó2   € ^ pV  F  pTpV^,          pK  	  V# )z?Compute the length of the tree using a slow-but-accurate method© )r    r¨   Úelementr9   s   &   r   Ú	_slow_lenÚRedBlackTree._slow_lenµ  s(   € ð ˆãˆGØˆEØ�Q�JŠEñ ð ˆr	   )r  r  r   r   r   r  r  r^   )r@   rA   rB   rC   rD   r   r  r!   r  r  Úsetterr  r'   r‰   rV   r)  r-  rŽ   r4  r:  r=  rB  rE   rF   Ú__classcell__)rˆ   rH   s   @@r   r  r  M  sœ   ù‡ € ñð2 ×$Ñ$ |Õ3€I÷ð  ñó ðð ‡_�_ñó ðð ñó ðò$÷&õò$$òL$ò8*òX*,òXòJ÷
	ò 	r	   r  )rD   Ú
__future__r   r  r   r�   Ú	itertoolsÚhasattrÚgetattrrê   r   Ú	NameErrorr   rJ   r   r  r@  r	   r   Ú<module>rK     s�   ðñ  2õ:  ã 
Û Û Û á
ˆ9�n×%Ò%Ù˜Y¨Ó7�Ná˜Y¨Ó6€NðÙòô~$�ô ~$ôBq�:ö qøðe ô ôðús   ¼A Á
A&Á%A&