Class notes: multi-dimensional arrays, compressed storage of matrices, generalized tables

Linear table -a finite sequence of data elements of the same type.
Expand the types of elements:
(multi-dimensional) arrays -the data elements in the linear table can be linear tables, but all elements have the same type.
Generalized table -The data elements in the linear table can be linear tables, and the types of elements can be different.
Definition of an array: An array is an ordered set of data elements of the same type, each element is constrained by n (n ≥ 1) linear relationships, and the array is called an n-dimensional array.
The characteristics of the array : the element itself can have a certain structure and belong to the same data type; the array is a data collection with a fixed format and number.
Arrays-generalization of linear tables
Two-dimensional arrays are linear tables whose data elements are linear tables.
Basic operation of array
1. Access: Given a set of subscripts, read out the corresponding array elements;
2. Modify: Given a set of subscripts, store or modify the corresponding array elements.
The access and modification operations essentially correspond to only one kind of operation- addressing
arrays do not have insert and delete operations, do not need to reserve space, and are suitable for sequential storage.
The storage structure and addressing of the
array
1. The one-dimensional array sets the subscript range of the one-dimensional array to the closed interval [l, h], each array element occupies c storage units, then the storage address of any element ai can be The formula is determined:
Loc (ai) = Loc (al) + (i-l) × c
Second, the two-dimensional array
1, according to the storage priority row addressing
set the two-dimensional array row number is l1 to h1, the column number is l2 to h2.
The number of elements in front of aij = the number of entire rows × the number of elements in each line + the number of elements in front of aij in this row = (i -l1) × (h2 -l2 + 1) + (j -l2),
Loc (aij) = Loc (al1l2) + ((i-l1) × (h2-l2 + 1) + (j-l2)) × c
2. Addressing for column priority storage
Set the two-dimensional array to n rows and m columns.
Let the array start storage position LOC (0, 0) = a, each element occupies 1 storage unit and the storage address of a [i] [j]: LOC (i, j) = a + (j × n + i) × l
Three, three-dimensional array
The number of elements in each dimension is m1, m2, m3. The subscripts of each dimension start from 0 and are stored in pages / rows / columns. The storage address of the array elements with subscripts i1, i2, i3:
LOC (i1, i2, i3) = a + (i1 × m2 × m3 + i2 × m3 + i3) × l
matrix compressed storage
Special matrix: many elements with the same value in the matrix and their distribution has a certain regularity.
Sparse matrix: There are many zero elements in the matrix.
The basic idea of ​​compressed storage is: ⑴ allocate only one storage space for multiple elements with the same value; ⑵ do not allocate storage space for zero elements.
Special matrix compression storage
1. Symmetric matrix
Features: aij = aji
only stores the elements of the lower triangular part.
Rows and columns are numbered starting from 1: the sequence number of aij in a one-dimensional array = i × (i-1) / 2 + j, because the subscript of a one-dimensional array starts from 0, so the subscript k of aij in a one-dimensional array = i × (i-1) / 2 + j-1
Rows and columns are numbered starting from 0: the sequence number of aij in a one-dimensional array = i × (i + 1) / 2 + j + 1, because the subscript of a one-dimensional array starts from 0, so aij is under the one-dimensional array The index k = i × (i + 1) / 2 + j
For the element aij (i≥j) in the lower triangle, the relationship between the index k and i, j in a one-dimensional array is:
k = i × (i -1) / 2 + j-1.
The element aij in the upper triangle (i <j), because aij = aji, then you can access the element aji corresponding to it, namely: k = j × (j-1) / 2 + i -1.
2. The triangular matrix
stores only the elements of the upper triangle (or lower triangle).
Storage: the lower triangular element, only one constant above the diagonal.
Lower triangular matrix
The corresponding relationship between the subscript k and i, j in a one-dimensional array of any element aij in the matrix:
k = i × (i-1) / 2 + j-1, when i≥j,
k = n × (n + 1) / 2, when i <j.
3. Diagonal matrix (ribbon matrix)
Diagonal matrix: all non-zero elements are concentrated in the strip area centered on the main diagonal, except for the main diagonal and the number of diagonals above and below it Except for the element, all other elements are zero.
Compression storage method one: two-dimensional array method
bts = aij, t = i-1, s = j-i + 1
uses a one-dimensional array to store non-zero elements on the diagonal, with row order as the main order, aij The address k in a one-dimensional array: k = 2i + j-3
The compressed storage of
the sparse matrix represents each non-zero element in the sparse matrix as: (row number, column number, non-zero element value)-ternary Group
defines a triplet:

template <class T>
struct element
{
     int row, col;
     T item
};

Triad table: The set of triplets corresponding to the non-zero elements of the sparse matrix is ​​arranged into a linear table in order of row priority.
Triad sequence table
storage structure definition:

const int MaxTerm=100;     
template <class T>     
struct SparseMatrix     
{    T data[MaxTerm];
     int mu, nu, tu;
};

Cross-linked
list Definition of cross-linked list nodes

template<class T>
class OLNode
{
public:
    int row,col;  
    T element;  
    OLNode<T>*right,*down;
public:
    OLNode(){right=NULL;down=NULL;}
};

Generalized table
Generalized table (list): a finite sequence of n (≥0) table elements, written as: LS = (a0, a1, a2,…, an-1)
LS is the table name, ai is the table element, it It can be a table (called a subtable) or a data element (called an atom). n is the length of the table. The generalized table with n = 0 is an empty table.
The basic concept of the
generalized table Length: the number of direct elements in the
generalized table LS ; depth: the maximum nesting level of parentheses in the generalized table LS.
Table head: When the generalized table LS is not empty, the first element is called the head of the LS;
Table footer: the generalized table composed of the remaining elements except the table head in the generalized table LS.
The difference between the generalized table and the linear table The
components of the linear table are structurally inseparable single elements. The components of the generalized table can be single elements or structured tables. The linear table is a special generalized table. The generalized table does not It must be a linear table, not necessarily a linear structure.
The basic operation of the generalized table
(1) Find the table header GetHead (L): The first element of the non-empty generalized table, which can be a single element or a sub-table
(2) Find the tail of the table GetTail (L): non An empty generalized table is a table composed of elements other than the header element. The end of the table must be
the storage
of a generalized table. Because the types of data elements in the generalized table are not uniform, it is difficult to store them in a sequential storage structure.
Storage structure of generalized tables— Head and tail representation
Table nodes: store generalized tables; element nodes: store single elements
Define node structure

enum Elemtag {Atom, List};  
template <class T>
struct GLNode {
    Elemtag tag;     
    union {       
        T data;        
        struct        
        {
             GLNode *hp, *tp;        
        } ptr;                                 
    }; 
};

The characteristics of the generalized table
are sequential (a direct predecessor and a direct successor),
have a length (= the number of elements in the table), have a
depth (= the number of parentheses in the table),
recursive (you can be your own sub-table), and
can share (Can be shared by other generalized tables)

Published 48 original articles · Like 25 · Visit 2453

Guess you like

Origin blog.csdn.net/qq_43628959/article/details/102791312