o
    ˷e^                     @   s4   d dl mZ d dlZddgZd	ddZd	ddZdS )
    )chainN	tree_data
tree_graphidchildrenc                    s   |   |  d krtd|  stdt| std|dur9ddl}d}|j|tdd	 |d
 |d krBt	d fdd t
t| j|  |fg} || |< |S )a5  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

    attrs : dict
        A dictionary that contains two keys 'id' and 'children'. The
        corresponding values provide the attribute names for storing
        NetworkX-internal graph data. The values should be unique. Default
        value: :samp:`dict(id='id', children='children')`.

        If some user-defined graph data use these attribute names as data keys,
        they may be silently dropped.

        .. deprecated:: 2.6

           The `attrs` keyword argument is replaced by `ident` and `children`
           and will be removed in networkx 3.0

    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.Nr   a  
The `attrs` keyword argument of tree_data is deprecated
and will be removed in networkx 3.0.
It is replaced with explicit `ident` and `children` keyword arguments.
To make this warning go away and ensure usage is forward
compatible, replace `attrs` with `ident` and `children,
for example:

    >>> tree_data(G, root, attrs={'id': 'foo', 'children': 'bar'})

should instead be written as

    >>> tree_data(G, root, ident='foo', children='bar')

The default values of 'id' and 'children' will not change.   
stacklevelr   r   z5The values for `id` and `children` must be different.c                    sh   ||  }t |dkrg S g }|D ]!}tt|j|  |fg} ||}|r,||< || q|S )Nr   )lendictr   nodesitemsappend)nGnbrs	children_childdcadd_childrenr   ident Y/var/www/ideatree/venv/lib/python3.10/site-packages/networkx/readwrite/json_graph/tree.pyr   k   s   
ztree_data.<locals>.add_children)number_of_nodesnumber_of_edges	TypeErroris_directednxis_weakly_connectedwarningswarnDeprecationWarningNetworkXErrorr   r   r   r   )r   rootattrsr   r   r"   msgdatar   r   r   r   	   s&   B

c           	         s   t  |durddl}d}|j|tdd |d |d  fdd	 |  }| g }fd
d|  D }j|fi |  || S )a  Returns graph from tree data format.

    Parameters
    ----------
    data : dict
        Tree formatted graph data
    attrs : dict
        A dictionary that contains two keys 'id' and 'children'. The
        corresponding values provide the attribute names for storing
        NetworkX-internal graph data. The values should be unique. Default
        value: :samp:`dict(id='id', children='children')`.

        .. deprecated:: 2.6

           The `attrs` keyword argument is replaced by `ident` and `children`
           and will be removed in networkx 3.0

    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
    Nr   a  
The `attrs` keyword argument of tree_graph is deprecated
and will be removed in networkx 3.0.
It is replaced with explicit `ident` and `children` keyword arguments.
To make this warning go away and ensure usage is
forward compatible, replace `attrs` with `ident` and `children,
for example:

    >>> tree_graph(data, attrs={'id': 'foo', 'children': 'bar'})

should instead be written as

    >>> tree_graph(data, ident='foo', children='bar')

The default values of 'id' and 'children' will not change.r   r	   r   r   c                    sf   |D ].}| } | | |g }|r || fdd| D }j|fi | qd S )Nc                    *   i | ]\}}|kr| krt ||qS r   str.0kvr   r   r   r   
<dictcomp>   s    $z4tree_graph.<locals>.add_children.<locals>.<dictcomp>)add_edgegetr   add_node)parentr   r)   r   grandchildrennodedatar   r   graphr   r   r   r      s   
z tree_graph.<locals>.add_childrenc                    r*   r   r+   r-   r1   r   r   r2      s   * ztree_graph.<locals>.<dictcomp>)r    DiGraphr"   r#   r$   r4   r   r5   )	r)   r'   r   r   r"   r(   r&   r   r8   r   r9   r   r   }   s   )
)Nr   r   )	itertoolsr   networkxr    __all__r   r   r   r   r   r   <module>   s
    
t