
    \Mh                     b    d dl mZ d dlZddgZddZ ej        dd	          dd
            ZdS )    )chainN	tree_data
tree_graphidchildrenc                    |                                  |                                 dz   k    rt          d          |                                 st          d          t	          j        |           st          d          k    rt	          j        d          fdi | j        |         | ||           iS )a  Returns data in tree format that is suitable for JSON serialization
    and use in JavaScript documents.

    Parameters
    ----------
    G : NetworkX graph
       G must be an oriented tree

    root : node
       The root of the tree

    ident : string
        Attribute name for storing NetworkX-internal graph data. `ident` must
        have a different value than `children`. The default is 'id'.

    children : string
        Attribute name for storing NetworkX-internal graph data. `children`
        must have a different value than `ident`. The default is 'children'.

    Returns
    -------
    data : dict
       A dictionary with node-link formatted data.

    Raises
    ------
    NetworkXError
        If `children` and `ident` attributes are identical.

    Examples
    --------
    >>> from networkx.readwrite import json_graph
    >>> G = nx.DiGraph([(1, 2)])
    >>> data = json_graph.tree_data(G, root=1)

    To serialize with json

    >>> import json
    >>> s = json.dumps(data)

    Notes
    -----
    Node attributes are stored in this format but keys
    for attributes must be strings if you want to serialize with JSON.

    Graph and edge attributes are not stored.

    See Also
    --------
    tree_graph, node_link_data, adjacency_data
       zG is not a tree.zG is not directed.zG is not weakly connected.z5The values for `id` and `children` must be different.c                     ||          }t          |          dk    rg S g }|D ]=}i |j        |         	|i} ||          }|r||<   |                    |           >|S )Nr   )lennodesappend)
nGnbrs	children_childdcadd_childrenr   idents
          b/var/www/html/test/jupyter/venv/lib/python3.11/site-packages/networkx/readwrite/json_graph/tree.pyr   ztree_data.<locals>.add_childrenF   s    tt99>>I	 	  	 E0175>05%00AUA&&A  (Q    )number_of_nodesnumber_of_edges	TypeErroris_directednxis_weakly_connectedNetworkXErrorr   )r   rootr   r   r   s     ``@r   r   r      s    h 	a//11A555*+++==?? .,---!!$$ 64555VWWW       KagdmJUD(LLq4I4IJJJr   T)graphsreturns_graphc                     t          j                    fd|          }|                     g           }fd|                                 D             } j        |fi |  ||           S )a  Returns graph from tree data format.

    Parameters
    ----------
    data : dict
        Tree formatted graph data

    ident : string
        Attribute name for storing NetworkX-internal graph data. `ident` must
        have a different value than `children`. The default is 'id'.

    children : string
        Attribute name for storing NetworkX-internal graph data. `children`
        must have a different value than `ident`. The default is 'children'.

    Returns
    -------
    G : NetworkX DiGraph

    Examples
    --------
    >>> from networkx.readwrite import json_graph
    >>> G = nx.DiGraph([(1, 2)])
    >>> data = json_graph.tree_data(G, root=1)
    >>> H = json_graph.tree_graph(data)

    See Also
    --------
    tree_data, node_link_data, adjacency_data
    c                     |D ]s}|	         }                     | |           |                    g           }|r ||           	fd|                                D             } j        |fi | td S )Nc                 N    i | ]!\  }}|k    |k    t          |          |"S  str.0kvr   r   s      r   
<dictcomp>z4tree_graph.<locals>.add_children.<locals>.<dictcomp>   s9       "aa5jjQ(]]A]]]r   )add_edgegetitemsadd_node)
parentr   datar   grandchildrennodedatar   r   graphr   s
         r   r   z tree_graph.<locals>.add_childrenx   s     		. 		.DKENN65))) HHXr22M 3UM222    &*jjll  H EN5--H----		. 		.r   c                 N    i | ]!\  }}|k    |k    t          |          |"S r&   r'   r)   s      r   r-   ztree_graph.<locals>.<dictcomp>   s2    SSSdaa5jjQ(]]A]]]r   )r   DiGraphr/   r0   r1   )r3   r   r   r    r   r5   r   r6   s    ``   @@r   r   r   V   s    @ JLLE
. 
. 
. 
. 
. 
. 
. 
. ;D2&&ISSSSSdjjllSSSHEN4$$8$$$Ly!!!Lr   )r   r   )	itertoolsr   networkxr   __all__r   _dispatchabler   r&   r   r   <module>r=      s             
%KK KK KK KK\ T2222 2 2 322 2 2r   