Php: tree structure algorithm. In terms of product classification, multi-level tree structure forums, mail lists, and many other places, we all encounter the following problem: how to store multi-level structured data? In PHP applications, the following data product categories, multi-level tree structure forums, mailing lists, and many other places are all faced with the following problem: how to store multi-level structured data?
In PHP applications, relational databases are usually used to store background data. they can store a large amount of data and provide efficient data retrieval and update services. However, the basic form of relational data is an interactive table, which is a flat structure. if you want to store a multi-level tree structure in a relational database, you need to perform reasonable translation work. Next, I will discuss what I have seen and some practical experiences with you.
There are basically two common design methods for storing hierarchical data in a flat database:
Adjacency list model)
Pre-sorted traversal tree algorithm (modified preorder tree traversal algorithm)
I am not a computer professional and have never learned anything about data structures. Therefore, these two names are literally translated by myself. if they are wrong, please give me more advice.
These two things seem very scary and easy to understand. Here I use a simple food directory as our sample data. Our data structure is as follows:
Food
|
| --- Fruit
|
| --- Red
|
| -- Cherry
|
| --- Yellow
|
| -- Banana
|
| --- Meat
|
| -- Beef
|
| -- Pork
To take care of PHP fans who are confused about English
Food: Food
Fruit: Fruit
Red: Red
Cherry: Cherry
Yellow: Yellow
Banana: Bananas
Meat: Meat
Beef: Beef
Pork: Pork
Why? In PHP applications, the number of backend servers is provided...