So how do you choose? The choice of data storage structure depends on two things, the logical structure of the data and the storage structure (also known as the physical structure).
Logical structure
The logical structure of data, simply understood, refers to the logical relationships between data.
Family Member Relationship Diagram
Figure 1 Family Member Relationship Diagram
Figure 1, for example, shows a family member relationship diagram, from which it can be seen that Zhang Ping, Zhang Hua, and Zhang Qun are brothers whose father is Zhang Liang, and among them, Zhang Ping has two sons, Zhang Jing and Zhang Lei.
The above said, father and son, brother and so on these relationships refer to the logical relationship between the data, assuming that we want to store such a family member relationship graph, not only to store data such as Zhang Ping, Zhang Hua, but also to store the relationship between them, the two are indispensable.
The success of a set of data stored in the computer is measured by the ability to restore its integrity. For example, the membership graph shown in Figure 1, if the stored data can completely recover this membership graph, then the data storage success.
"Many-to-many" Relationship Diagram
Figure 2 "Many-to-many" Relationship Diagram
Logical relationships between data can be subdivided into three categories, "one-to-one", "one-to-many", "one-to-many", "one-to-many", "one-to-many", "one-to-many", "one-to-many", and "one-to-many". ", "one-to-many" and "many-to-many":
"One-to-one": similar to the set {1,2,3,.... ,n}, each data has one and only one neighbor (other than 1) on the left side of the data; similarly, each data has only one neighbor (other than n) on the right side of the data, and this is true for all data, which is said to be logically "one-to-one";
"One-to-many" and "many-to-many":
"Many-to-many "For V1, V2, V3 and V4, they are "many-to-many" relationship;
By learning data structure, we can learn 3 kinds of storage structure to store data. By learning data structures, we can learn that there are 3 kinds of storage structures to store the data of these 3 kinds of logical relationships, in other words:
Linear tables are used to store data with "one-to-one" logical relationships;
Tree structures are used to store data with "one-to-many" relationships;
Tree structures are used to store data with "one-to-many" relationships.
The graph structure is used to store data with "many-to-many" relationships;
Thus, we can analyze the logical relationship between the data to decide which storage structure to use, but the specific use of sequential storage or chained storage, but also through the physical structure of the data to decide.
Storage structure (physical structure)
The storage structure of the data, also known as the physical structure, refers to whether the data is stored centrally or distributed in the physical storage space. Assuming that you want to store data of size 10G, centralized storage is shown in Figure 3a) and decentralized storage is shown in Figure 3b).
Physical storage of data
Figure 3 Physical storage of data
If you choose to centralize the storage, you will use a sequential storage structure; if you choose to centralize the storage, you will use chained storage. The choice depends largely on the state of the storage device and the purpose of the data.
We know that centralized storage (the underlying implementation of the use of arrays) requires the use of a large piece of contiguous physical space, assuming that the size of the data to be stored for the 1G, if there is no storage device on the size of more than 1G of the space, you can not use the sequential storage, at this time we have to choose the chained storage, because the chained storage is a random storage of data, occupying the storage device are relatively small storage space
And, the use of data is different, the choice of storage structure is also different. Centralized storage of data is good for traversal operations on the data at a later stage, while decentralized storage is better for adding or deleting data at a later stage. Therefore, if you need to retrieve a lot of data later (traversal), choose centralized storage; on the contrary, if you need to make further updates to the data later (add or delete), choose decentralized storage.