Forest
======

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

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

Add node
--------

>>> T=Forest()
>>> T.add_node(0)
>>> T.add_nodes_from(range(0,1))
>>> print T.nodes()
[0]


Add edge
--------
>>> T=Forest()
>>> 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)
>>> T.delete_edge(0,1)
>>> T.has_edge(0,1)
False
>>> T.has_node(0) # isolated now
True
>>> T.has_node(1)
True
>>> T.add_edge(1,2) # silent failure
>>> T.delete_edges_from([(1,2),(1,3),(4,5)])
>>> print T.edges()
[]
>>> print T.nodes()
[0, 1, 2, 3, 4, 5]

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

>>> T=Forest()
>>> 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)
>>> print T.edges()
[(1, 2)]
>>> T.add_node(3)
>>> T.delete_node(4)
>>> print T.nodes()
[1, 2, 3]
>>> print T.edges()
[(1, 2)]

>>> T=Forest()
>>> T.add_edge(1,2)
>>> T.delete_node(2)
>>> T.tree_nodes()
[[1]]
>>> T.delete_node(1)
>>> T.tree_nodes()
[]

Delete edge
-----------
>>> T=Forest()
>>> T.add_path([1,2,3,4])
>>> T.delete_edge(2,3)
>>> T.delete_edge(3,4)
>>> print T.edges()
[(1, 2)]


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


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

>>> T=Forest()
>>> 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 forest
>>> print sorted(T.comp.items())
[(1, 0), (2, 0), (3, 0), (4, 0)]

>>> T.add_node(5)
>>> T.add_edge(4,5)
>>> print sorted(T.edges())
[(1, 2), (2, 3), (3, 4), (4, 5)]
>>> print sorted(T.comp.items())
[(1, 0), (2, 0), (3, 0), (4, 0), (5, 0)]

>>> T.add_path([6,7,8,9])
>>> print sorted(sorted(t) for t in T.tree_nodes())
[[1, 2, 3, 4, 5], [6, 7, 8, 9]]

>>> T.add_edge(5,6) # join paths
>>> print sorted( T.tree_nodes())
[[1, 2, 3, 4, 5, 6, 7, 8, 9]]

>>> T.delete_edge(4,5) # split path
>>> print sorted(sorted(t) for t in T.tree_nodes())
[[1, 2, 3, 4], [5, 6, 7, 8, 9]]

>>> T.add_edge(4,5) # join path
>>> print sorted( T.tree_nodes())
[[1, 2, 3, 4, 5, 6, 7, 8, 9]]


>>> T.delete_edge(5,4) # split path
>>> print sorted(sorted(t) for t in T.tree_nodes())
[[1, 2, 3, 4], [5, 6, 7, 8, 9]]

>>> T.add_edge(5,4) # join path
>>> print sorted( T.tree_nodes())
[[1, 2, 3, 4, 5, 6, 7, 8, 9]]

>>> T.delete_node(5) # split path
>>> print sorted(T.edges())
[(1, 2), (2, 3), (3, 4), (6, 7), (7, 8), (8, 9)]
>>> print sorted(sorted(t) for t in T.tree_nodes())
[[1, 2, 3, 4], [6, 7, 8, 9]]

>>> T.add_node(5) 
>>> print sorted(sorted(t) for t in T.tree_nodes())
[[1, 2, 3, 4], [5], [6, 7, 8, 9]]

>>> T.add_edges_from([(4,5),(5,6)])
>>> print sorted(sorted(t) for t in T.tree_nodes())
[[1, 2, 3, 4, 5, 6, 7, 8, 9]]

>>> T.delete_edges_from([(4,5),(5,6)])
>>> print sorted(sorted(t) for t in T.tree_nodes())
[[1, 2, 3, 4], [5], [6, 7, 8, 9]]




