Data Structure--》Arrays and Generalized Tables: Comprehensive Analysis from Basics to Application

        Data structures provide us with basic tools for organizing and processing data. In this vast field of data structures, arrays and generalized tables are two indispensable and important concepts. As representatives of linear structures, they play an important role in algorithms and applications.

        Whether you are a beginner or an advanced person, this article will provide you with simple, easy-to-understand, practical and feasible knowledge points to help you better grasp the importance of arrays and generalized tables in data structures and algorithms, thereby improving algorithmic problem solving. Ability. Next, let us embark on a wonderful journey of data structures and algorithms.

Table of contents

Array definition

Sequential representation and implementation of arrays

Compressed storage of matrices

Definition of generalized table

Storage structure of generalized table


Array definition

An array is a collection of pairs (subscript values, data element values). For a set of meaningful subscripts in the array, there is a corresponding value. A one-dimensional array corresponds to one subscript value, a two-dimensional array corresponds to two subscript values, and so on.

Array is an ordered sequence composed of n(n>1) data elements of the same data typea_1,a_2,.....a_n, and the sequence must Stored in a memory unit with consecutive addresses. Arrays have the following characteristics:

1) The data elements in the array have the same data type

2) An array is a random access structure. Given a set of subscripts, you can access the corresponding data elements.

3) The number of data elements in the array is fixed.

Abstract data type definition of array:

From the above definition, there are b_1b_2....b_n data elements in the n-dimensional array, each data element is constrained by the n-dimensional relationship a>.

Sequential representation and implementation of arrays

Arrays generally do not perform insertion and deletion operations, which means that once an array is created, the number of elements in the structure and the relationship between elements will not change. Therefore, the sequential storage method is generally used to represent arrays.

The computer's memory structure is a bunch of (linear) address structures. For multi-dimensional arrays, there is an order agreement issue when storing (mapping) them into a one-dimensional memory structure. That is, the array elements must be arranged into a sequence in a certain order, and then this linear sequence must be stored in memory.

A two-dimensional array is the simplest multi-dimensional array. This is an example to illustrate the order agreement issue when a multi-dimensional array is stored (mapped) into a one-dimensional memory structure.

Two storage methods for two-dimensional arrays (row-based and column-based):

for example:

Compressed storage of matrices

        In scientific and engineering computing problems, a matrix is ​​a commonly used mathematical object. When programming in high-level languages, a matrix is ​​usually described as a two-dimensional matrix. This allows random access to its elements and makes various matrix operations very simple.

        For high-order matrices, if the non-zero elements are distributed in a certain regular manner or there are a large number of zero elements in the matrix, if the matrix is ​​still stored using conventional methods, repeated non-zero elements or zero elements may be stored, which will cause a lot of waste of storage space. Therefore, this type of matrix must be compressed and stored.

Note: Multiple identical non-zero elements only allocate one storage space; zero elements do not allocate space.

Next we need to masterspecial matrices and sparse matrices Compressed storage method.

Definition of generalized table

        Generalized tables are the promotion and expansion of linear tables and are widely used in the field of artificial intelligence. We define a linear table as a finite sequence of n(n\geqslant0) elementsa_1,a_2,...a_n. All elements in the sequence have the same data type and can only It is an atomic item (the so-called atomic item can be a number or a structure, which means that it cannot be further divided in structure.) If this restriction on elements is relaxed and they are allowed to have their own structure, the concept of generalized table will be produced.

Generalized list (Lists, also known as list): is given by n(n\geqslant0) Finite sequence composed of elements: LS=(a_1,a_2,...a_n)

Storage structure of generalized table

Since the data elements in the generalized table have different structures, they are usually represented by a chain storage structure, and each data element is represented by a node. Therefore, there are two types of nodes in generalized tables:

The first category istable node: used to represent generalized table items, consisting of a flag field, a header pointer field, and a table tail pointer field. Composition

The first type isAtomic node: used to represent atomic items, consisting of a flag field and an atom's value field.

As long as the generalized table is not empty, it consists of a header and a tail. That is, a certain header and footer uniquely determine a generalized table.

What is a generalized table? Please briefly describe the difference between generalized tables and linear tables?

        Generalized List is a data structure that extends a linear list, allowing elements to be either single values ​​or sublists. Generalized tables are defined recursively and can contain subtables at any nested level, forming a more complex and hierarchically structured data collection.

        Corresponding to the generalized list is the linear list. The linear list only contains single-valued elements, and each element has a successor element, forming a linear structure. Linear tables are one of the simplest and most common data structures. For example, arrays are an implementation of linear tables.

The differences between generalized tables and linear tables mainly include the following points:

1. Element type: The elements of a linear table can only be single values, while the elements of a generalized table can be either single values ​​or sub-tables. This enables generalized tables to represent more complex and hierarchical data relationships.

2. Structural characteristics: A linear list is a linear structure. Each element has a unique successor element, forming a simple sequence. Generalized tables have a more complex hierarchical structure and can contain sub-tables at any nested level, forming a tree structure.

3. Operational flexibility: Since generalized tables have a more complex structure, generalized tables are more flexible than linear tables when operating and processing data. Generalized tables can be traversed and operated recursively and can handle various complex data relationships, while linear tables are relatively simple.

To sum up, a generalized table is a data structure that extends a linear table, allowing elements to be either single values ​​or subtables. Generalized tables have more complex hierarchical structures and operational flexibility, and can better represent and process various complex data relationships.

A generalized table is (a, (a, b), d, e, (a, (i, j), k)). Please draw the linked storage structure of this generalized table:

                +---+         +---+         +---+         +---+
                | a |---+     |   |---+     | d |---+     | e |
                +---+   |     +---+   |     +---+   |     +---+
                        |             |             |
                        |     +---+   |     +---+   |
                        +---->| a |   +---->| b |   |
                        |     +---+   |     +---+   |
                        |             |             |
                        |     +---+   |             |
                        +---->| i |   +-------------+
                              +---+
                              |
                              |
                              |
                              |     +---+
                              +---->| j |
                                    +---+
                                    |
                                    |
                                    |
                                    |
                                    |     +---+
                                    +---->| k |
                                          +---+

Guess you like

Origin blog.csdn.net/qq_53123067/article/details/133607524