
    h                     j    S r SSKJr  SSKJrJr  SSKrS/r\R                  " SSS9SSS.S	 j5       r
g)
zOperations on trees.    )partial)
accumulatechainN
join_treesT)graphsreturns_graph)label_attributefirst_labelc          	      v  ^ U (       d  [         R                  " S5      $ [        U 6 u  p4[        US   5      " 5       nS USS  5       n[	        [        UTS-   S95      n/ n[        X4U5       HB  u  pnU[	        U	R                  5       5      R                  U
5      -   nUR                  U5        MD     [        [         R                  US9n[        X75       V	Vs/ s H  u  pU" XS9PM     nn	nU H  n	UR                  U	5        M     UR                  T5        UR                  U4S	 jU 5       5        U$ s  snn	f )
a  Returns a new rooted tree made by joining `rooted_trees`

Constructs a new tree by joining each tree in `rooted_trees`.
A new root node is added and connected to each of the roots
of the input trees. While copying the nodes from the trees,
relabeling to integers occurs. If the `label_attribute` is provided,
the old node labels will be stored in the new tree under this attribute.

Parameters
----------
rooted_trees : list
    A list of pairs in which each left element is a NetworkX graph
    object representing a tree and each right element is the root
    node of that tree. The nodes of these trees will be relabeled to
    integers.

label_attribute : str
    If provided, the old node labels will be stored in the new tree
    under this node attribute. If not provided, the original labels
    of the nodes in the input trees are not stored.

first_label : int, optional (default=0)
    Specifies the label for the new root node. If provided, the root node of the joined tree
    will have this label. If not provided, the root node will default to a label of 0.

Returns
-------
NetworkX graph
    The rooted tree resulting from joining the provided `rooted_trees`. The new tree has a root node
    labeled as specified by `first_label` (defaulting to 0 if not provided). Subtrees from the input
    `rooted_trees` are attached to this new root node. Each non-root node, if the `label_attribute`
    is provided, has an attribute that indicates the original label of the node in the input tree.

Notes
-----
Trees are stored in NetworkX as NetworkX Graphs. There is no specific
enforcement of the fact that these are trees. Testing for each tree
can be done using :func:`networkx.is_tree`.

Graph, edge, and node attributes are propagated from the given
rooted trees to the created tree. If there are any overlapping graph
attributes, those from later trees will overwrite those from earlier
trees in the tuple of positional arguments.

Examples
--------
Join two full balanced binary trees of height *h* to get a full
balanced binary tree of depth *h* + 1::

    >>> h = 4
    >>> left = nx.balanced_tree(2, h)
    >>> right = nx.balanced_tree(2, h)
    >>> joined_tree = nx.join_trees([(left, 0), (right, 0)])
    >>> nx.is_isomorphic(joined_tree, nx.balanced_tree(2, h + 1))
    True

   r   c              3   8   #    U  H  n[        U5      v   M     g 7fN)len).0trees     U/var/www/html/env/lib/python3.13/site-packages/networkx/algorithms/tree/operations.py	<genexpr>join_trees.<locals>.<genexpr>P   s     0ZTs4yyZs   N)initial)r	   )r
   c              3   ,   >#    U  H	  nTU4v   M     g 7fr    )r   rootr
   s     r   r   r   g   s     ?YTk4(Ys   )nxempty_graphziptypelistr   nodesindexappendr   convert_node_labels_to_integersupdateadd_nodeadd_edges_from)rooted_treesr	   r
   treesrootsRlengthsfirst_labels	new_rootsr   r   
first_nodenew_rootrelabel	new_treess     `            r   r   r      s0   v ~~a   %LE 	U1XA0U3BZ0G
7K!ODELI"%eL"AJTZZ\ 2 8 8 >>" #B
 
**OG
 "%U!9!9D 	.!9   	  JJ{?Y??Hs   D5)__doc__	functoolsr   	itertoolsr   r   networkxr   __all___dispatchabler   r       r   <module>r8      s@      ' . T204! \ 3\r7   