Create a table in the database, with ID, Fatherid, value three fields, you can store a tree.
How to put the table data in the form of a tree, the following little brother with Python simple simulation.
Beginner python, please give us a lot of advice. In addition very grateful http://www.cnblogs.com/lzyzizi/to younger brother's guidance.
Operation Result:
A-1 B-1 C-1 D-1 E-1 E-2 C-2 B-2 C-3 C-4
Source:
#!user/bin/python classNotemodel:def __init__(Self,id,value,fatherid): self. Id=Id Self.value=value Self.fatherid=Fatherid Self.children= [] defAddChild (self,*Child ): Self.children+= ChilddefPrinttree (self,layer):Print ' '*layer +self.value Map (LambdaChild:child.printTree (layer + 1), Self.children)defMain ():#data table simulation, database has Id, value, Fatherid three fields, t1-t10 represents 10 rows of dataT1 = Notemodel (1,'A-1', 0) T2= Notemodel (2,'B-1', 1) T3= Notemodel (3,'B-2', 1) T4= Notemodel (4,'C-1', 2) T5= Notemodel (5,'C-2', 2) T6= Notemodel (6,'C-3', 3) T7= Notemodel (7,'C-4', 3) T8= Notemodel (8,'D-1', 4) T9= Notemodel (9,'E-1', 8) T10= Notemodel (10,'E-2', 8) #querying the database and generating the listList =[t1,t2,t3,t4,t5,t6,t7,t8,t9,t10]#looping lists, binding parent-child relationships, forming a tree forIinchRange (0, Len (list)): forJinchRange (0, Len (list)):ifList[j].fatherid = =List[i]. Id:list[i].addchild (List[j])#Print Treet1.printtree (0)if __name__=="__main__": Main ()
Python simple simulation: storing a tree in a data table