It's hard for me to walk around a tree, and so avoid it like a plague ... usually.
I have a class of this kind (a slightly simplified version here, but functionally the same) as:
class Branch(object): def __init__(self, title, parent=None): self.title = title self.parent = parent
I have a dictionary from a set of Branch instances, each heading as a key:
tree = {'Foo Branch': foo, 'Sub-Foo Branch': sub_foo, 'Bar Branch': bar}
Now I know that there are complex algorithms for efficient workaround (for example, MPTT, etc.), especially for use with database-based projects where efficiency is important. I do not use a database at all, only simple objects in memory.
Given the title of Branch , I need to get a list all the descendants of this branch (children, child children, etc.) from tree , so:
- Would you recommend using complex (for my algo unanswered algorithm as MPTT for efficiency in my case, or is there an easy way to achieve this in one function?
- If so, which one would you recommend, knowing that I am not using a database?
- Can you give an example, or is it much more than I think?
Note This is not homework. I'm not in school. I'm just so bad at algorithms. I used Django MPTT for a project that required DB-stored trees ... but still doesn't understand it very well.
source share