Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Recursively traverse category tree in Python

I'm new to Python and know there is a much better way to structure a recursive query. Would appreciate someone more advanced taking a look at how best to streamline the below code.

I poured over similar examples on StackOverflow but none had the data structure I'm trying to traverse.

Sample data:

[
    {'categoryId': 100, 'parentId': 0,   'catName': 'Animals & Pet Supplies'},
    {'categoryId': 103, 'parentId': 100, 'catName': 'Pet Supplies'},
    {'categoryId': 106, 'parentId': 103, 'catName': 'Bird Supplies'},
    {'categoryId': 500, 'parentId': 0,   'catName': 'Apparel & Accessories'},
    {'categoryId': 533, 'parentId': 500, 'catName': 'Clothing'},
    {'categoryId': 535, 'parentId': 533, 'catName': 'Activewear'}
]

Python code:

def returnChildren(categoryId):
    cats  = dict()
    results = categories.find( { "parentId" : categoryId } )
    for x in results:
        cats[x['categoryId']] = x['catName']

    return cats

children = returnChildren(cat_id)

#build list of children for this node
for x in children:
    print (x, "-", children[x])
    results = returnChildren(x)
    if (len(results) > 0):
        for y in sorted(results.keys()):
            print(y, "--", results[y])
            sub_results = returnChildren(y)
            if (len(sub_results) > 0):
            for z in sorted(sub_results.keys()):
                print(z, "----", sub_results[z])
                sub_sub_results = returnChildren(z)
                if (len(sub_sub_results) > 0):
                    for a in sorted(sub_sub_results.keys()):
                        print(a, "------", sub_sub_results[a])

This code will generate a tree similar to the below:

100 Animals & Pet Supplies
103 - Pet Supplies
106 -- Bird Supplies
500 Apparel & Accessories
533 - Clothing
535 -- Activewear
like image 433
NOpderbe Avatar asked Mar 06 '26 17:03

NOpderbe


1 Answers

You can use a recursive function to traverse the tree.

In addition it's probably worth to precalculate the parentage tree (it can be useful for other purposes too).

I've wrapped things into a class here – it should be pretty simple to follow.

(EDIT: Instead of the callback thing in the earlier version I've changed things to use a generator instead, to be more Pythonic.)

from collections import defaultdict


class NodeTree:
    def __init__(self, nodes):
        self.nodes = nodes
        self.nodes_by_parent = defaultdict(list)

        for node in self.nodes:
            self.nodes_by_parent[node["parentId"]].append(node)

    def visit_node(self, node, level=0, parent=None):
        yield (level, node, parent)
        for child in self.nodes_by_parent.get(node["categoryId"], ()):
            yield from self.visit_node(child, level=level + 1, parent=node)

    def walk_tree(self):
        """
        Walk the tree starting from the root, returning 3-tuples (level, node, parent).
        """
        for node in self.root_nodes:
            yield from self.visit_node(node)

    @property
    def root_nodes(self):
        return self.nodes_by_parent.get(0, ())


nodes = [
    {"categoryId": 100, "parentId": 0, "catName": "Animals & Pet Supplies"},
    {"categoryId": 103, "parentId": 100, "catName": "Pet Supplies"},
    {"categoryId": 106, "parentId": 103, "catName": "Bird Supplies"},
    {"categoryId": 500, "parentId": 0, "catName": "Apparel & Accessories"},
    {"categoryId": 533, "parentId": 500, "catName": "Clothing"},
    {"categoryId": 535, "parentId": 533, "catName": "Activewear"},
]

tree = NodeTree(nodes)

for level, node, parent in tree.walk_tree():
    print(node["categoryId"], "-" * level, node["catName"])

This code prints, almost like your original,

100  Animals & Pet Supplies
103 - Pet Supplies
106 -- Bird Supplies
500  Apparel & Accessories
533 - Clothing
535 -- Activewear
like image 131
AKX Avatar answered Mar 08 '26 08:03

AKX