Warning

This documents an unmaintained version of NetworkX. Please upgrade to a maintained version and see the current NetworkX documentation.

networkx.algorithms.tree.operations.join

join(rooted_trees, label_attribute=None)[source]

Returns a new rooted tree with a root node joined with the roots of each of the given rooted trees.

Parameters
  • rooted_trees (list) – A list of pairs in which each left element is a NetworkX graph object representing a tree and each right element is the root node of that tree. The nodes of these trees will be relabeled to integers.

  • label_attribute (str) – If provided, the old node labels will be stored in the new tree under this node attribute. If not provided, the node attribute '_old' will store the original label of the node in the rooted trees given in the input.

Returns

The rooted tree whose subtrees are the given rooted trees. The new root node is labeled 0. Each non-root node has an attribute, as described under the keyword argument label_attribute, that indicates the label of the original node in the input tree.

Return type

NetworkX graph

Notes

Graph, edge, and node attributes are propagated from the given rooted trees to the created tree. If there are any overlapping graph attributes, those from later trees will overwrite those from earlier trees in the tuple of positional arguments.

Examples

Join two full balanced binary trees of height h to get a full balanced binary tree of depth h + 1:

>>> h = 4
>>> left = nx.balanced_tree(2, h)
>>> right = nx.balanced_tree(2, h)
>>> joined_tree = nx.join([(left, 0), (right, 0)])
>>> nx.is_isomorphic(joined_tree, nx.balanced_tree(2, h + 1))
True