Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions tests/test_tree.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,8 @@ def test_children(self):
children = self.tree.children(nid)
for child in children:
self.assertEqual(child in self.tree.all_nodes(), True)
children_from_itr = list(self.tree.children_itr(nid))
self.assertEqual(children, children_from_itr)
try:
self.tree.is_branch("alien")
except NodeIDAbsentError:
Expand Down
7 changes: 7 additions & 0 deletions treelib/tree.py
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,13 @@ def children(self, nid):
"""
return [self[i] for i in self.is_branch(nid)]
Copy link

@crabhi crabhi Mar 20, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

High, just a quick note - this method could now be implemented equivalently as list(self.children_itr(nid)). That might address @caesar0301's comment re. redundancy.


def children_itr(self, nid):
"""
Return the children (Node) of nid as an iterator.
Empty iterator is returned if nid does not exist
"""
return (self[i] for i in self.is_branch(nid))

def contains(self, nid):
"""Check if the tree contains node of given id"""
return True if nid in self._nodes else False
Expand Down