How to implement infinite pole classification in python tree menu?

data structure:

create table web_class (
    "id" integer NOT NULL PRIMARY KEY AUTOINCREMENT,
    "name" varchar(255) NOT NULL,         --
    "bid" integer,                        --ID
)

Database data:
{id:1, name:" name 1, bid:0}
{id:2, name:" name 2, bid:0}
{id:3, name:" name 3, bid:1}
{id:4, name:" name 4, bid:1}
{id:5, name:" name 5, bid:3}
{id:6, Name:" name 6 minutes, bid:5}
.

the tree menu to be implemented (similar to the following):

[{"bid":0,"id":1,"name":"1",
    "son":[
        {"bid":1,"id":3,"name":"3",
            "son":[
               {"bid":3,"id":5,"name":"5",
                   "son":[
                       {"bid":3,"id":6,"name":"6","son":[]}
                   ]
               }
            ]
        },
        {"bid":1,"id":4,"name":"4","son":[]}
    ]
},
{"bid":0,"id":2,"name":"2","son":[]}]

it would be best if you could explain the principle.

Mar.30,2021

-sharp
l = [{"id":1, "name":'1', "bid":0},
{"id":2, "name":'2', "bid":0},
{"id":3, "name":'3', "bid":1},
{"id":4, "name":'4', "bid":1},
{"id":5, "name":'5', "bid":3},
{"id":6, "name":'6', "bid":5}]
l.insert(0, {"id": 0, "name": "root"}) -sharp
for i in l:
    i["son"] = []


-sharp
index = {l[i]["id"] : i for i in range(len(l))}

for i in range(1, len(l)):
    l[index[l[i]["bid"]]]["son"].append(l[i]) -sharp l[i]bidl[i]
    -sharp bid = l[i]["bid"] bid
    -sharp i0 = index[bid] bidindex
    -sharp l[i0]
    -sharp l[i0]["son"].append(l[i]) 
    

print(l[0]["son"]) -sharp



class BaseCategoryTree(BaseService):

def __init__(self, db):
    self.db = db
    self.category_tree = []

def create_tree(self, parent):
    parent['children'] = []
    for record in self.category_tree:
        if str(record['parent_id']) == str(parent['id']):
            parent['children'].append(self.create_tree(record))
    return parent

def get_category_trees(self, **kwargs):
    """

    :return:
    """
    category_base_svr = ErpBcategoryService(self.db)

    category_list =''

    self.category_tree = [i.to_dict() for i in category_list]
    comment_tree = []
    for record in self.category_tree:
        if str(record['parent_id']) == '0':  -sharp if this is the start of a tree
            comment_tree.append(self.create_tree(record))
    return comment_tree
Menu