common data structures

 1. Linear structure

 

1. Simplest structure: Linear table

     Linear tables have the following characteristics:

   - has one and only one "first element";

   - has one and only one "last element";

   - Except for the last element, all other elements have unique successor elements;

   - Except for the first element, all other elements have a unique preceding element;

   For linear tables, the following operations can be performed:

   - Add Nodes - Insert Nodes - Delete Nodes - Find Nodes - Traverse Nodes - Count Nodes

 

2. First-in, first-out structure: queue

      A queue is a special kind of linear table. The queue processes data according to the "First In First Out" (FIFO) principle, and only allows delete operations at the front end of the table and insert operations at the back end of the table. The end of the insertion operation is called the tail of the queue, and the section where the deletion is performed is called the head of the queue. When there are no elements in the queue, it is called an empty queue.

      The operation of the data structure of the queue is very simple, mainly in the following categories:

     -Initialize queue: Create a queue.

     - Enqueue: Add an element to the end of the queue.

     - Dequeue: Take out the element at the head of the queue, delete the element at the same time, and make the next element the head of the queue.

     - Get the first element of the queue: take out the element at the head of the queue, do not delete the element (the head of the queue is still the element)

     - Get queue length: Calculate the number of elements in the queue according to the head and tail of the queue.

 

Circular queue (a special kind of queue):


 

 3. First-in, last-out structure: stack

       The stack is a special form of linear table, which is different from the "first in, first out" of the queue. The stack processes data according to the principle of "Last In First Out" (LIFO).

       There are only two basic operations on the stack:

       - Push: Save the data to the top of the stack. Before performing this operation, modify the top of the stack pointer. Move it up by one element, and then save the data to the position pointed to by the top of the stack pointer.

      - Pop: Pop the data at the top of the stack, and then modify the pointer at the top of the stack to point to the next element in the stack.

 

Second, the tree structure

 

The concept of tree:


 

   The concept of binary tree:

      - In a binary tree, the total number of nodes at level i is at most 2i-1 nodes.

      - A binary tree of depth k has at most 2k-1 nodes (k>=1), and at least k nodes.

      - For a binary tree, if the number of leaf nodes is n0, and the total number of nodes with degree 2 is n2, then n0=n2+1.

      - The depth k of a complete binary tree with n nodes is: k=[log2n]+1.

 

      - If each node of a complete binary tree with n nodes is stored in order, for any node i, there is the following relationship.

 

If i!=1, the number of its parent node is i/2;

If 2*i<=n, the number of the root node of its left subtree is 2*i; if 2*i>n, there is no left subtree.

If 2*i+1<n, then its right subtree root node number is 2*i+1; if 2*i+1>n, then there is no right subtree.

 

    Traverse a binary tree:

      Preorder traversal (DLR): It is called preorder traversal, that is, the root node is visited first, then the left subtree is traversed in preorder, and finally the right subtree is traversed in preorder.

      Preorder traversal (LDR): called in-order traversal, the left subtree is traversed in inorder first, that is, the root node is visited, and then the right subtree is traversed in inorder.

      Preorder traversal (LRD): It is called pre-root order traversal, that is, the left subtree is traversed first, then the right subtree is traversed in postorder, and finally the root node is visited.

      Traversal by layer: Traversing by the layers of the binary tree can more intuitively obtain the result of the traversal from the graph.

 

The concept of binary tree:



 



 

Threaded binary tree:



 

Representation of a threaded binary tree:


 

 

Optimal binary tree (Huffman tree):



 

Huffman code:


 

 

 

3. Network Relationship: Diagram

 

1. Definition of graph:

Undirected and directed graphs:

 

Undirected graph:



 

Directed graph:



 

 

2. Graph storage:

 

Adjacency matrix and adjacency list:

Adjacency Matrix:

Adjacency list:


 

3. Traversal of the graph

 

       Breadth-first traversal:

         Breadth-first traversal is similar to hierarchical traversal of a tree. The specific process is as follows:

      ---(1) Select an unvisited vertex Vi from the isTrav array and mark it as visited.

      ---(2) Then visit all the unvisited adjacencies of Vi in turn, and mark them as visited.

      ---(3) Perform breadth-first traversal from these adjacent points until all vertices in the graph that have a path to Vi have been visited. 

 

      ---(4) Repeat steps 1 to 3 until all vertices have been visited.

 

       Depth-first traversal:

         The depth-first traversal method is similar to the preorder traversal of a tree.

      ---(1) Select an unvisited vertex Vi from the isTrav array and mark it as visited.

      ---(2) Then start a depth-first traversal from an unvisited neighbor of Vi.

      ---(3) Repeat step 2 until all vertices in the graph with the same path as Vi have been visited.

      ---(4) Repeat steps 1-3 until all vertices have been visited.

 

 

Guess you like

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