
    Gd                     2    d dl mZ d dlZddgZd	dZ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t          t          | j	        |         
                                |fg                    } ||           |<   |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 ]g}t          t          |j        |                                         	|fg                    } ||          }|r||<   |                    |           h|S )Nr   )lendictr   nodesitemsappend)
nGnbrs	children_childdcadd_childrenr   idents
          Blib/python3.11/site-packages/networkx/readwrite/json_graph/tree.pyr   ztree_data.<locals>.add_childrenF   s    tt99>>I	 	  	 EU175>//11UEN3CDDEEAUA&&A  (Q    )number_of_nodesnumber_of_edges	TypeErroris_directednxis_weakly_connectedNetworkXErrorr   r   r   r   )r   rootr   r   datar   s     `` @r   r   r      s   h 	a//11A555*+++==?? .,---!!$$ 64555VWWW       agdm))++udm_==>>D!\$**DNKr   c                     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getr   add_node)
parentr   r#   r   grandchildrennodedatar   r   graphr   s
         r   r   z tree_graph.<locals>.add_childreny   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   DiGraphr0   r   r1   )r#   r   r   r"   r   r4   r   r5   s    ``   @@r   r   r   X   s    > JLLE
. 
. 
. 
. 
. 
. 
. 
. ;D2&&ISSSSSdjjllSSSHEN4$$8$$$Ly!!!Lr   )r   r   )	itertoolsr   networkxr   __all__r   r   r'   r   r   <module>r;      sd             
%M M M M`2 2 2 2 2 2r   