
    wg                     n    d Z ddlmZ ddlmZmZ ddlZdgZ ej                  dd      dddd	       Z
y)
zOperations on trees.    )partial)
accumulatechainN
join_treesT)graphsreturns_graph)label_attributefirst_labelc                x   | st        j                  d      S t        |  \  }} t        |d                }d |dd D        }t	        t        |dz               }g }t        |||      D ]B  \  }	}
}|t	        |	j                               j                  |
      z   }|j                  |       D t        t         j                  |      }t        ||      D 	cg c]  \  }	} ||	|       }}	}|D ]  }	|j                  |	        |j                         |j                  fd	|D               |S c c}}	w )
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   2   K   | ]  }t        |        y wN)len).0trees     h/home/mcse/projects/flask/flask-venv/lib/python3.12/site-packages/networkx/algorithms/tree/operations.py	<genexpr>zjoin_trees.<locals>.<genexpr>P   s     0Ts4y0s   N)initial)r	   )r
   c              3   &   K   | ]  }|f 
 y wr    )r   rootr
   s     r   r   zjoin_trees.<locals>.<genexpr>g   s     ?Tk4(?s   )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      sC   v ~~a   %LE5 	U1XA0U3BZ0G
7K!ODELI"%eUL"A #dJTZZ\ 2 8 8 >>"#
 
**OG
 "%UL!9D+ 	+.I   	 JJ{?Y??Hs   D6)__doc__	functoolsr   	itertoolsr   r   networkxr   __all___dispatchabler   r       r   <module>r7      sB      ' . T204! \ 3\r6   