Repository: edkrueger/python-tree

Issue:
change find_steps_from_leaf into find_min_steps_from_leaf, find_max_steps_from_leaf

Comments:

Patch:
diff --git a/resc/tree.py b/resc/tree.py
index a280c8e..647f16a 100644
--- a/resc/tree.py
+++ b/resc/tree.py
@@ -13,7 +13,8 @@ class Tree:
         self.node_id = node_id
         self.children = children
         self.steps_from_root = None
-        self.steps_from_leaf = None
+        self.min_steps_from_leaf = None
+        self.max_steps_from_leaf = None
 
     @classmethod
     def from_dict(cls, tree_dict):
@@ -72,29 +73,42 @@ class Tree:
                 for child in node.children:
                     queue.append((child, steps + 1))
 
-    def _find_steps_from_leaf(self):
+    def _find_steps_from_leaf(self, min_max):
         """Finds and sets steps_from_leaf for each node in a non-circular tree.
-        steps_from_leaf is the minimum number of steps form a leaf."""
+        steps_from_leaf is the minimum / maximum number of steps form a leaf."""
 
         if self.children == []:
-            self.steps_from_leaf = 0
+
+            if min_max == "min":
+                self.min_steps_from_leaf = 0
+            if min_max == "max":
+                self.max_steps_from_leaf = 0
 
         else:
 
             children_steps = []
 
             for child in self.children:
-                child._find_steps_from_leaf()  # pylint: disable=protected-access
-                children_steps.append(child.steps_from_leaf)
 
-            self.steps_from_leaf = min(children_steps) + 1
+                # pylint: disable=protected-access
+                child._find_steps_from_leaf(min_max=min_max)
+
+                if min_max == "min":
+                    children_steps.append(child.min_steps_from_leaf)
+                if min_max == "max":
+                    children_steps.append(child.max_steps_from_leaf)
+
+            if min_max == "min":
+                self.min_steps_from_leaf = min(children_steps) + 1
+            if min_max == "max":
+                self.max_steps_from_leaf = max(children_steps) + 1
 
-    def find_steps_from_leaf(self):
-        """Finds and sets steps_from_leaf for each node.
-        steps_from_leaf is the minimum number of steps form a leaf.
+    def find_steps_from_leaf(self, min_max):
+        """Finds and sets the min/max steps_from_leaf for each node.
+        The  result is set in the min_steps_from_leaf / max_steps_from_leaf attr.
         Only works on non-circular trees."""
 
         if self.is_circular():
             raise ValueError("Cannot call compute_steps_from_root on a circular Tree.")
 
-        self._find_steps_from_leaf()
+        self._find_steps_from_leaf(min_max=min_max)


Test patch:
diff --git a/tests/test_tree.py b/tests/test_tree.py
index 88a5f50..e4ee167 100644
--- a/tests/test_tree.py
+++ b/tests/test_tree.py
@@ -62,14 +62,14 @@ def test_find_steps_from_root():
         circular_tree.find_steps_from_root()
 
 
-def test_find_steps_from_leaf():
+def test_find_min_steps_from_leaf():
     """Tests if Tree.find_steps_from_root."""
 
     tree = Tree.from_dict({"a": {"b": {"d": {}}, "c": {"e": {"f": {}}}}})
-    tree.find_steps_from_leaf()
+    tree.find_steps_from_leaf(min_max="min")
 
     steps_lookup = {}
-    tree.visit_all(lambda e: steps_lookup.update({e.node_id: e.steps_from_leaf}))
+    tree.visit_all(lambda e: steps_lookup.update({e.node_id: e.min_steps_from_leaf}))
     assert steps_lookup["a"] == 2
     assert steps_lookup["b"] == 1
     assert steps_lookup["c"] == 2
@@ -80,4 +80,25 @@ def test_find_steps_from_leaf():
     circular_tree = Tree.from_dict({"a": {"b": {}, "c": {"a": {}}}})
 
     with pytest.raises(ValueError):
-        circular_tree.find_steps_from_leaf()
+        circular_tree.find_steps_from_leaf(min_max="min")
+
+
+def test_find_max_steps_from_leaf():
+    """Tests if Tree.find_steps_from_root."""
+
+    tree = Tree.from_dict({"a": {"b": {"d": {}}, "c": {"e": {"f": {}}}}})
+    tree.find_steps_from_leaf(min_max="max")
+
+    steps_lookup = {}
+    tree.visit_all(lambda e: steps_lookup.update({e.node_id: e.max_steps_from_leaf}))
+    assert steps_lookup["a"] == 3
+    assert steps_lookup["b"] == 1
+    assert steps_lookup["c"] == 2
+    assert steps_lookup["d"] == 0
+    assert steps_lookup["e"] == 1
+    assert steps_lookup["f"] == 0
+
+    circular_tree = Tree.from_dict({"a": {"b": {}, "c": {"a": {}}}})
+
+    with pytest.raises(ValueError):
+        circular_tree.find_steps_from_leaf(min_max="max")