def tree_data(G, root, attrs=_attrs):
 if (G.number_of_nodes() != (G.number_of_edges() + 1)):
  raise TypeError('G is not a tree.')
 if (not G.is_directed()):
  raise TypeError('G is not directed.')
 id_ = attrs['id']
 children = attrs['children']
 if (id_ == children):
  raise nx.NetworkXError('Attribute names are not unique.')
 def add_children(n, G):
  nbrs = G[n]
  if (len(nbrs) == 0):
   return []
  children_ = []
  for child in nbrs:
   d = dict(chain(G.node[child].items(), [(id_, child)]))
   c = add_children(child, G)
   if c:
    d[children] = c
   children_.append(d)
  return children_
 data = dict(chain(G.node[root].items(), [(id_, root)]))
 data[children] = add_children(root, G)
 return data

def tree_data ( G , root , attrs = _attrs ) : if ( G . number_of_nodes ( ) != ( G . number_of_edges ( ) + 1 ) ) : raise TypeError ( 'G is not a tree.' ) if ( not G . is_directed ( ) ) : raise TypeError ( 'G is not directed.' ) id_ = attrs [ 'id' ] children = attrs [ 'children' ] if ( id_ == children ) : raise nx . NetworkXError ( 'Attribute names are not unique.' ) def add_children ( n , G ) : nbrs = G [ n ] if ( len ( nbrs ) == 0 ) : return [ ] children_ = [ ] for child in nbrs : d = dict ( chain ( G . node [ child ] . items ( ) , [ ( id_ , child ) ] ) ) c = add_children ( child , G ) if c : d [ children ] = c children_ . append ( d ) return children_ data = dict ( chain ( G . node [ root ] . items ( ) , [ ( id_ , root ) ] ) ) data [ children ] = add_children ( root , G ) return data
return data in tree format that is suitable for json serialization and use in javascript documents .