2. sequence table data structures and algorithms (Python)

2. order table

In a program, you need a set of (usually some type of) data elements as a whole to manage and use, you need to create the elements of this group, recording their use variables, functions, and so pass into the spread. The number of elements contained in a set of data may change (add or delete elements).

For this demand, the easiest solution is to be a group of elements such as a sequence of elements used in the position and order of the sequence, represented in some meaningful information about the actual application, or represent the data between a relationship.

Such sequence elements organized in a group, we can be abstracted as a linear table . A linear list is a collection of certain elements is also recorded in a sequential relationships between elements. Linear table is one of the most basic data structure, the actual procedure is widely used, it is often used as a basis of complex data structures.

The actual linear form of storage, is divided into two models to achieve:

Sequence table , the elements are sequentially stored in a contiguous memory area, the sequence relationship between elements expressed by their natural storage order.

List , the number of elements stored in a memory block by linking up the configuration.

2.1 sequence table form

The basic form of the sequence table

FIG showing a basic form of the sequence table, the data storage element itself continuously, the size of each element of the memory cell share the same fixed, the standard element of its logical address and the physical address of the storage element (actual memory addresses) can be by starting address storage area Loc (e0) plus the logical address (i-th element) calculates the product of the size of the storage unit (c) is obtained, namely:

Loc(ej) = Loc(e0) + c*j

Therefore, when accessing the specified element does not need to traverse from scratch, can be obtained by calculating the address corresponding to its time complexity is O (1).

If the size of the element is not uniform, the need to use an external form of the element of FIG b, the separate elements of the actual data stored sequentially in the table holding address information, the location of each cell (i.e., links) of the corresponding element. Since the same amount of storage required for each link, the above equation can be calculated from the storage position of the element links, and then follow the link to find data elements that are actually stored. Note that, in Figure b c is the size of data elements longer, but the amount of memory required to store a link address, the amount is generally small.

FIG b in this order is also called an index table to the actual data, which is the simple index structure.

2.2 Architecture and Implementation sequence table

Structure of sequence table

A complete sequence information table comprises two parts, a set of elements in the table, i.e. the data area, the other part is the information to be recorded for proper operation, i.e., information about the overall situation of the table, known as the header information, this part of the information element includes a storage region of the capacity and existing in the current table number of elements of the two.

Table two sequences basically manner

FIG is a one-piece construction, with the element unit storage area for storing the table information in a continuous manner arranged in a storage area, the two portions integrally forming a complete data sequence table object.

Integrated structural integrity and strong, easy to manage. However, since a part of the data element storage area is a table object, the order table is created, a storage area on the fixed element.

Panel b is a separate structure, the object table stored in only the information related to the entire table (i.e., the capacity and the number of elements), the actual data elements stored in another element storage area in a separate, linked with the base table by associating objects.

Replace the element storage area

Integral structure due to the sequential table information storage area and the continuous data region together, the replacement of data area if you want, you can only move the whole, i.e., the entire target sequence table (refer to the configuration information storing area of ​​the sequence table) has changed.

If you want to replace the separation type structure the data area, only the table information area to update the data link address area, and this order table object unchanged.

Elements of the storage area expansion

采用分离式结构的顺序表,若将数据区更换为存储空间更大的区域,则可以在不改变表对象的前提下对其数据存储区进行扩充,所有使用这个表的地方都不必修改。只要程序的运行环境(计算机系统)还有空闲存储,这种表结构就不会因为满了而导致操作无法进行。人们把采用这种技术实现的顺序表称之为动态顺序表,因为其容量可以在使用中动态变化。

扩充的两种策略:

​ (1)每次扩充增加固定数目的存储位置,如每次扩充增加10个元素的位置,这种策略可称为线性增长。

​ 特点:节省空间,但是扩充操作频繁,操作次数多。

​ (2)每次扩充容量加倍,如每次扩充增加一倍存储空间。

​ 特点:减少了扩充操作的执行次数,但可能会浪费空间资源。以空间换时间,推荐的方式。

2.3 顺序表的操作

增加元素

如果所示,为顺序表增加新元素111的三种方式

a. 尾端加入元素,时间复杂度为O(1)

b. 非保序的加入元素(不常见),时间复杂度为O(1)

c. 保序的元素加入,时间复杂度为O(n)

删除元素

a. 删除表尾元素,时间复杂度为O(1)

b. 非保序的元素删除(不常见),时间复杂度为O(1)

c. 保序的元素删除,时间复杂度为O(n)

2.4 Python中的顺序表

Python中的list和tuple两种类型采用了顺序表的实现技术,具有前面讨论的顺序表的所有性质。

tuple是不可变类型,即不变的顺序表,因此不支持改变其内部状态的任何操作,而其他方面,则与list的性质类似。

list的基本实现技术

Python标准类型list就是一种元素个数可变的线性表,可以加入和删除元素,并在各种操作中维持已有元素的顺序(即保序),而且还具有以下行为特征:

  • 基于下标(位置)的高效元素访问和更新,时间复杂度应该是O(1);

    为满足该特征,应该采用顺序表技术,表中元素保存在一块连续的存储区中。

  • 允许任意加入元素,而且在不断加入元素的过程中,表对象的标识(函数id得到的值)不变。

    为满足该特征,就必须能更换元素存储区,并且为保证更换存储区时list对象的标识id不变,只能采用分离式实现技术。

在Python的官方实现中,list就是一种采用分离式技术实现的动态顺序表。这就是为什么用list.append(x) (或 list.insert(len(list), x),即尾部插入)比在指定位置插入元素效率高的原因。

在Python的官方实现中,list实现采用了如下的策略:在建立空表(或者很小的表)时,系统分配一块能容纳8个元素的存储区;在执行插入操作(insert或append)时,如果元素存储区满就换一块4倍大的存储区。但如果此时的表已经很大(目前的阀值为50000),则改变策略,采用加一倍的方法。引入这种改变策略的方式,是为了避免出现过多空闲的存储位置。

Guess you like

Origin www.cnblogs.com/sincere-ye/p/12153129.html