U
    -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krPtd fd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                    sh   ||  }t |dkrg S g }|D ]B}tt|j|  |fg} ||}|rX||< || q |S )Nr   )lendictr   nodesitemsappend)nGZnbrs	children_childdcadd_childrenr   ident c/var/www/html/Darija-Ai-Train/env/lib/python3.8/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   r
   r   )r   rootr   r   datar   r   r   r      s    4

c                    sb   t   fdd |  }| g }fdd|  D }j|f|  || 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                    sb   |D ]X}| } | | |g }|r6 || fdd| D }j|f| 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getr   add_node)parentr   r   r   Zgrandchildrennodedatar   r   graphr   r   r   r   y   s    
z tree_graph.<locals>.add_childrenc                    s*   i | ]"\}}|kr| krt ||qS 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   X   s    
)r   r   )r   r   )	itertoolsr   Znetworkxr   __all__r   r   r   r   r   r   <module>   s   
P