Tree
=====

Unit tests for Tree Class in tree.py
------------------------------------

>>> from networkx import *
>>> from networkx.tree import *

Add node
--------

>>> T=Tree()
>>> T.add_node(1)
>>> T.add_node(0)
Traceback (most recent call last):
...
NetworkXError: adding single node 0 not allowed in non-empty tree
>>> T.add_nodes_from(range(0,1))
Traceback (most recent call last):
...
NetworkXError: adding single node 0 not allowed in non-empty tree


Add leaf
--------
>>> T=Tree()
>>> T.add_leaf(0,1)
>>> T.has_edge(0,1)
True
>>> T.add_leaves_from([(1,2),(1,3)])
>>> T.has_edge(1,3)
True
>>> T.add_leaf(4,5)
Traceback (most recent call last):
...
NetworkXError: adding edge 4-5 not allowed in tree
>>> T.delete_leaf(0,1)
>>> T.has_edge(0,1)
False
>>> T.add_edge(1,2) # silent failure
>>> T.delete_leaves_from([(1,2),(1,3)])
>>> print T.edges()
[]

Add edge
--------
>>> T=Tree()
>>> T.add_edge(0,1)
>>> T.has_edge(0,1)
True
>>> T.add_edges_from([(1,2),(1,3)])
>>> T.has_edge(1,3)
True
>>> T.add_edge(4,5)
Traceback (most recent call last):
...
NetworkXError: adding edge 4-5 not allowed in tree
>>> T.delete_edge(0,1)
>>> T.has_edge(0,1)
False
>>> T.has_node(0)
False
>>> T.has_node(1)
True
>>> T.add_edge(1,2) # silent failure
>>> T.delete_edges_from([(1,2),(1,3)])
>>> print T.edges()
[]
>>> print T.nodes()
[]

Delete node
-----------

>>> T=Tree()
>>> T.add_path([1,2,3,4])
>>> T.delete_node(5)
Traceback (most recent call last):
...
NetworkXError: node 5 not in graph
>>> T.delete_node(3)
Traceback (most recent call last):
...
NetworkXError: deleting interior node 3 not allowed in tree
>>> T.add_node(3)
>>> T.delete_node(4)
>>> print T.nodes()
[1, 2, 3]
>>> print T.edges()
[(1, 2), (2, 3)]


Delete edge
-----------
>>> T=Tree()
>>> T.add_path([1,2,3,4])
>>> T.delete_edge(2,3)
Traceback (most recent call last):
...
NetworkXError: deleting interior edge 2-3 not allowed in tree
>>> T.delete_edge(3,4)
>>> print T.edges()
[(1, 2), (2, 3)]



Unit tests for RootedTree Class in tree.py
--------------------------------------------

Add node
--------

>>> T=RootedTree(0)
>>> T.add_node(1)
>>> T.add_node(0)
Traceback (most recent call last):
...
NetworkXError: adding single node 0 not allowed in non-empty tree
>>> T.add_nodes_from(range(0,1))
Traceback (most recent call last):
...
NetworkXError: adding single node 0 not allowed in non-empty tree


Add leaf
--------
>>> T=RootedTree(0)
>>> T.add_leaf(0,1)
>>> T.has_edge(0,1)
True
>>> T.add_leaves_from([(1,2),(1,3)])
>>> T.has_edge(1,3)
True
>>> T.add_leaf(4,5)
Traceback (most recent call last):
...
NetworkXError: adding edge 4-5 not allowed in tree
>>> T.delete_leaf(0,1)
>>> T.has_edge(0,1)
False
>>> T.add_edge(1,2) # silent failure
>>> T.delete_leaves_from([(1,2),(1,3)])
>>> print T.edges()
[]

Add edge
--------
>>> T=RootedTree(0)
>>> T.add_edge(0,1)
>>> T.has_edge(0,1)
True
>>> T.add_edges_from([(1,2),(1,3)])
>>> T.has_edge(1,3)
True
>>> T.add_edge(4,5)
Traceback (most recent call last):
...
NetworkXError: adding edge 4-5 not allowed in tree
>>> T.delete_edge(0,1)
>>> T.has_edge(0,1)
False
>>> T.has_node(0)
False
>>> T.has_node(1)
True
>>> T.add_edge(1,2) # silent failure
>>> T.delete_edges_from([(1,2),(1,3)])
>>> print T.edges()
[]
>>> print T.nodes()
[]

Delete node
-----------

>>> T=RootedTree(0)
>>> T.add_path([1,2,3,4])
>>> T.delete_node(5)
Traceback (most recent call last):
...
NetworkXError: node 5 not in graph
>>> T.delete_node(3)
Traceback (most recent call last):
...
NetworkXError: deleting interior node 3 not allowed in tree
>>> T.delete_node(4)
>>> print T.nodes()
[1, 2, 3]
>>> print T.edges()
[(1, 2), (2, 3)]


Delete edge
-----------
>>> T=RootedTree(0)
>>> T.add_path([1,2,3,4])
>>> T.delete_edge(2,3)
Traceback (most recent call last):
...
NetworkXError: deleting interior edge 2-3 not allowed in tree
>>> T.delete_edge(3,4)
>>> print T.edges()
[(1, 2), (2, 3)]


Paths and Parents
-----------------

>>> T=RootedTree(0)
>>> T.add_path([1,2,3,4])
>>> T.add_edge(1,4)
Traceback (most recent call last):
...
NetworkXError: adding edge 1-4 not allowed in tree
>>> print sorted(T.par.items())
[(2, 1), (3, 2), (4, 3)]
>>> T.add_node(5)
Traceback (most recent call last):
...
NetworkXError: adding single node 5 not allowed in non-empty tree

>>> T.add_edge(4,5)
>>> print sorted(T.edges())
[(1, 2), (2, 3), (3, 4), (4, 5)]
>>> T.add_path([6,7,8,9])
Traceback (most recent call last):
...
NetworkXError: adding edge 6-7 not allowed in tree

>>> T.add_edge(5,6)
>>> T.add_path([6,7,8,9])
>>> print T.parent(5)
4
>>> print T.parent(1)
None


Auto conversion
---------------

>>> P=path_graph(4)
>>> T=Tree(P)
>>> print T.edges()
[(0, 1), (1, 2), (2, 3)]
>>> P=cycle_graph(4)
>>> T=Tree(P)
Traceback (most recent call last):
...
NetworkXError: Data cycle_graph(4) is not a tree


