summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSylvain <syt@logilab.fr>2007-05-10 12:49:34 +0200
committerSylvain <syt@logilab.fr>2007-05-10 12:49:34 +0200
commit0f748ab10ca950a6079003cb3a07c81801f7bb94 (patch)
treed6e114051ca4070ddbf35b43a8aca5cc7f66530c
parent5bb88ea775e497e4c8a7d82f4232676ff9456acc (diff)
parentc72d0926d35b214e9b20711161967fc8adff27d4 (diff)
downloadlogilab-common-0f748ab10ca950a6079003cb3a07c81801f7bb94.tar.gz
merge
-rw-r--r--test/unittest_tree.py17
-rw-r--r--tree.py14
2 files changed, 30 insertions, 1 deletions
diff --git a/test/unittest_tree.py b/test/unittest_tree.py
index 598a4a2..2dd955e 100644
--- a/test/unittest_tree.py
+++ b/test/unittest_tree.py
@@ -10,7 +10,8 @@ from logilab.common.tree import *
tree = ('root', (
('child_1_1', (
('child_2_1', ()), ('child_2_2', (
- ('child_3_1', ()),)))),
+ ('child_3_1', ()),
+ )))),
('child_1_2', (('child_2_3', ()),))))
def make_tree(tuple):
@@ -91,12 +92,26 @@ class Node_ClassTest(TestCase):
def test_raise_get_child_by_path_NodeNotFound(self):
self.assertRaises(NodeNotFound, self.o.get_child_by_path, ['child_1_1', 'child_2_11'])
+ def test_known_values_depth_down(self):
+ """
+ return depth of this node in the tree
+ """
+ self.assertEqual(self.o.depth_down(), 4)
+ self.assertEqual(self.o.get_child_by_id('child_2_1',True).depth_down(), 1)
+
def test_known_values_depth(self):
"""
return depth of this node in the tree
"""
self.assertEqual(self.o.depth(), 0)
self.assertEqual(self.o.get_child_by_id('child_2_1',True).depth(), 2)
+
+ def test_known_values_width(self):
+ """
+ return depth of this node in the tree
+ """
+ self.assertEqual(self.o.width(), 3)
+ self.assertEqual(self.o.get_child_by_id('child_2_1',True).width(), 1)
def test_known_values_root(self):
"""
diff --git a/tree.py b/tree.py
index 87f7076..551c2d2 100644
--- a/tree.py
+++ b/tree.py
@@ -163,6 +163,20 @@ class Node :
else :
return 0
+ def depth_down(self):
+ """
+ return depth of the tree from this node
+ """
+ if self.children:
+ return 1 + max([c.depth_down() for c in self.children])
+ return 1
+
+ def width(self):
+ """
+ return the width of the tree from this node
+ """
+ return len(self.leaves())
+
def root(self):
"""
return the root node of the tree