
    h                     \    S SK Jr  S SKrSS/rS	S jr\R                  " SSS9S	S j5       rg)
    )chainN	tree_data
tree_graphc                 v  ^^^ U R                  5       U R                  5       S-   :w  a  [        S5      eU R                  5       (       d  [        S5      e[        R
                  " U 5      (       d  [        S5      eTT:X  a  [        R                  " S5      eUUU4S jm0 U R                  U   ETUTT" X5      0E$ )ai  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                    > X   n[        U5      S:X  a  / $ / nU H<  n0 UR                  U   ET	U0EnT" XA5      nU(       a  XeT'   UR                  U5        M>     U$ )Nr   )lennodesappend)
nGnbrs	children_childdcadd_childrenchildrenidents
          T/var/www/html/env/lib/python3.13/site-packages/networkx/readwrite/json_graph/tree.pyr   tree_data.<locals>.add_childrenF   sl    tt9>I	E01775>05%0AU&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4IJJr   T)graphsreturns_graphc                 6  ^^^^	 [         R                  " 5       m	UUU	U4S jmU T   nU R                  T/ 5      nU R                  5        VVs0 s H!  u  pVUT:w  d  M  UT:w  d  M  [	        U5      U_M#     nnnT	R
                  " U40 UD6  T" X45        T	$ s  snnf )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                 4  > U H  nUT   nT
R                  X5        UR                  T	/ 5      nU(       a  T" X45        UR                  5        VVs0 s H!  u  pVUT:w  d  M  UT	:w  d  M  [        U5      U_M#     nnnT
R                  " U40 UD6  M     g s  snnf )N)add_edgegetitemsstradd_node)parentr   datar   grandchildrenkvnodedatar   r   graphr   s           r   r    tree_graph.<locals>.add_childrenx   s    DKENN6) HHXr2MU2&*jjl&2daa5j	Q(]	A	l   NN5-H- s   B!B)B)r   DiGraphr&   r'   r(   r)   )
r+   r   r   r    r   r-   r.   r/   r   r0   s
    ``     @@r   r   r   V   s    @ JJLE
. 
. ;D2&I&*jjlSldaa5j	Q(]	A	lHS	NN4$8$!L Ts   BB%B)idr   )	itertoolsr   networkxr   __all__r   _dispatchabler    r   r   <module>r9      s>     
%KK\ T22 32r   