a
    h                     @   s@   d dl mZ d dlZddgZd
ddZejdd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krPtd fd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                    s`   ||  }t |dkrg S g }|D ]:}i |j| |i} ||}|rP||< || q |S )Nr   )lennodesappend)nGZnbrs	children_childdcadd_childrenr   ident P/var/www/auris/lib/python3.9/site-packages/networkx/readwrite/json_graph/tree.pyr   F   s    
ztree_data.<locals>.add_children)Znumber_of_nodesZnumber_of_edges	TypeErrorZis_directednxZis_weakly_connectedZNetworkXErrorr	   )r   rootr   r   r   r   r   r      s    4

)Zgraphsc                    sf   t   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

    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                    sf   |D ]\}| } | | |g }|r6 || fdd| D }j|fi | qd S )Nc                    s*   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>)Zadd_edgegetitemsadd_node)parentr   datar   Zgrandchildrennodedatar   r   graphr   r   r   r   x   s    
z tree_graph.<locals>.add_childrenc                    s*   i | ]"\}}|kr| krt ||qS r   r   r   r   r   r   r           ztree_graph.<locals>.<dictcomp>)r   ZDiGraphr!   r"   r#   )r%   r   r   r   r   r&   r   r'   r   r   V   s     
)r   r   )r   r   )	itertoolsr   Znetworkxr   __all__r   	_dispatchr   r   r   r   r   <module>   s
   
N
