
    ިsg                     ^    d dl mZ d dlZddgZd	dZ ej                  dd      d	d       Zy)
    )chainN	tree_data
tree_graphc                 Z   | j                         | j                         dz   k7  rt        d      | j                         st        d      t	        j
                  |       st        d      k(  rt	        j                  d      f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                     ||    }t        |      dk(  rg S g }|D ]8  }i |j                  |   	|i} ||      }|r||<   |j                  |       : |S )Nr   )lennodesappend)
nGnbrs	children_childdcadd_childrenchildrenidents
          U/var/www/html/venv/lib/python3.12/site-packages/networkx/readwrite/json_graph/tree.pyr   ztree_data.<locals>.add_childrenF   sw    tt9>I	 	 E01775>05%0AUA&A(Q	      )number_of_nodesnumber_of_edges	TypeErroris_directednxis_weakly_connectedNetworkXErrorr
   )r   rootr   r   r   s     ``@r   r   r      s    h 	a//1A55*++==?,--!!!$455VWW KaggdmJUD(Lq4IJJr   T)graphsreturns_graphc                 (  	 t        j                         		fd|    }| j                  g       }| j                         D ci c]  \  }}|k7  s|k7  st	        |      | }}} 	j
                  |fi |  ||       	S c c}}w )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 ]  }|   }
j                  | |       |j                  	g       }|r	 ||       |j                         D ci c]  \  }}|k7  s|	k7  st        |      | }}} 
j                  |fi |  y c c}}w )N)add_edgegetitemsstradd_node)parentr   datar   grandchildrenkvnodedatar   r   graphr   s           r   r   z tree_graph.<locals>.add_childrenx   s     		.DKENN65) HHXr2MUM2&*jjl"aa5jQ(]A	H  ENN5-H-		.s   B	B	"B	)r   DiGraphr%   r&   r'   r(   )
r*   r   r   r   r   r,   r-   r.   r   r/   s
    ``     @@r   r   r   V   s    @ JJLE
. ;D2&I&*jjlSdaa5jQ(]A	SHSENN4$8$y!L Ts   BBB)idr   )	itertoolsr   networkxr   __all__r   _dispatchabler    r   r   <module>r7      s@     
%KK\ T22 32r   