"Data Structures and Algorithms (Python Language Description)" Chapter 3, My Understanding

Chapter 3 Linear Tables

Linear tables have two basic representations: sequential tables and linked tables.
I understand linearity : data elements are points on a line. Knowing the value of one data element, you can get the next data element. There is a feeling of police handling a case. The clues are linked together, and there is a sequential relationship.

1. Sequence table

The way of expression is that the linear relationship between data elements is represented by the storage order of data elements in memory (implicit)

What is implicit expression?

That is, the data itself does not know who is next, but the next data is behind me. These data indirectly express the linear relationship between them through their successive positions in a certain storage block .

Why organize data structures (store data) this way?

The advantage is that when accessing one of the data elements through subscripts, the time complexity is O(1), which is constant time, in short, fast, fast. How to do this is related to how it is represented; because the data relationship is represented by position, as long as the offset address of the pointer data element is given, the pointer can accurately point to the data you want by using simple four arithmetic operations. element.

Now let’s talk about the disadvantages :
1. Because of this way of expression, the size of the storage block must be determined at the beginning. The book also mentions a solution and explains the risks.
2. For the deletion and insertion of data elements, if the order of data elements needs to be guaranteed, the time complexity is O(n).

Note : Disadvantages and advantages are relative to the data structure you want to implement, that is, a disadvantage may become a point in other data structures. For example , if the sequence table is used in a data structure that needs to dynamically add data elements (such as Python's List), the above shortcomings are true; but if it is used in a data-sensitive data structure, its shortcomings can protect the data to a certain extent (such as Python's Tuple).

2. Link table

  • The elements of the table are stored in a batch of independent storage blocks (called table nodes).
  • It is guaranteed that from any node in the composition table structure, its related next node can be found.
  • The relationship between the previous node and the next node is displayed in the form of a link.

The book says very clearly that linked list is a very flexible data organization technology. There are many different ways to implement linked list, including singly linked list, simple deformation of singly linked list, circular linked list, doubly linked list, etc. These linked lists are based on your Data organization needs to be designed and present, so it is very flexible .

Consider only the case of a singly linked list:

We know that the way a data structure is represented determines the operation methods used on it. The data elements of the linked list are scattered in the storage units of the memory, and the previous data element points to the next data element through the link, so the problem is that when I want to know one of the data elements, I must start from the first one. Starting from the node, traverse all the data elements before the data element I want. The time required is related to the number of data elements, so the time complexity is O(n). It can be seen that compared with the sequence table, this time is much different. Yes, this is a disadvantage (relatively speaking) of singly linked lists.

Then the advantage is also related to this form of expression, that is, as long as the memory is not full, there is no limit to the length of the table, and only constant time is required for node operations (deletion, insertion), and the link can be modified.


Mainly for personal understanding, welcome questions and corrections, thank you!

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326770180&siteId=291194637