o
    ]Zh                     @   sB   d dl mZ d dlZddgZdddZejd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r(t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} ||}|r(||< || q|S )Nr   )lennodesappend)nGZnbrs	children_childdcadd_childrenr   ident Q/var/www/auris/lib/python3.10/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

 T)ZgraphsZreturns_graphc                    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 }|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>)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                    r   r   r   r   r    r   r   r!      s   * ztree_graph.<locals>.<dictcomp>)r   ZDiGraphr"   r#   r$   )r&   r   r   r   r   r'   r   r(   r   r   V   s    
)r   r   )	itertoolsr   Znetworkxr   __all__r   Z_dispatchabler   r   r   r   r   <module>   s    
N