Home >Backend Development >PHP Tutorial >php: Tree structure algorithm_PHP tutorial

php: Tree structure algorithm_PHP tutorial

WBOY
WBOYOriginal
2016-07-15 13:22:27734browse

We will encounter such problems in product classification, multi-level tree-structured forums, mailing lists and many other places: How to store multi-level structured data?

In PHP applications, the backend data storage is usually a relational database, which can save large amounts of data and provide efficient data retrieval and update services. However, the basic form of relational data is a criss-crossed 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 with you what I have seen and heard and some practical experiences.

There are basically two common design methods for storing hierarchical data in flat databases:

Adjacency list model (adjacency list model)

Pre-sorted tree traversal algorithm (modified preorder tree traversal algorithm)

I am not a computer major, and I have never learned anything about data structures, so I translated these two names according to their literal meanings. If I am wrong, please let me know. Please give me some advice.

These two things may sound scary, but they are actually very easy to understand. Here I use a simple food directory as our example data. Our data structure is like this:

Food

|

|---Fruit

| |

| |- --Red

| | |

| | |--Cherry

| |

| |---Yellow

| >

|

|--Pork

To take care of those PHP enthusiasts whose English is a mess

Food: food

Fruit: fruit

Red: Red

Cherry: Cherry

Yellow: Yellow

Banana: Banana

Meat: Meat

Beef: Beef

Pork: Pork

http://www.bkjia.com/PHPjc/446964.html

www.bkjia.com

true

http: //www.bkjia.com/PHPjc/446964.html
TechArticle

We will encounter such problems in product classification, multi-level tree-structured forums, mailing lists and many other places. : How to store multi-level structured data? In PHP applications, provide background data...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn