Move add_node and add_edge methods to the Tree base class

This commit is contained in:
Raphaël Bournhonesque 2017-06-11 18:29:28 +02:00
parent 8ff4f512a2
commit d9c567371f

View File

@ -15,6 +15,16 @@ class Tree(object):
def __getitem__(self, item):
return self.nodes[item]
def add_node(self, node, attr_dict=None):
attr_dict = attr_dict or {}
self.nodes[node] = attr_dict
def add_edge(self, u, v, dep=None):
if u not in self.nodes or v not in self.nodes:
raise ValueError("Each node must be defined before adding an edge.")
self.adjacency[u][v] = dep
def number_of_nodes(self):
return len(self)
@ -67,16 +77,6 @@ class Tree(object):
class DependencyPattern(Tree):
def add_node(self, node, attr_dict=None):
attr_dict = attr_dict or {}
self.nodes[node] = attr_dict
def add_edge(self, u, v, dep=None):
if u not in self.nodes or v not in self.nodes:
raise ValueError("Each node must be defined before adding an edge.")
self.adjacency[u][v] = dep
@property
def root_node(self):
if self.number_of_nodes() == 1: