Data Structure: A Complete Collection of Eight Data Structures

Original link: Data Structure: A Complete Collection of Eight Data Structures_Green Water Changliu*z’s Blog-CSDN Blog

Data Structure
1.1 Overview of Data Structure
Data structure is the way computer stores and organizes data; usually, carefully selected data structure can bring higher operation or storage efficiency. The quality of the data structure will directly affect the performance of our program; commonly used data structures are: Array (Array), Stack (Stack), Queue (Queue), Linked List (Linked List), Tree (Tree), Graph (Graph), Heap, Hash, etc.;

1.2 Classification of data structures
1.2.1 Arrangement
1) Set
Set: There is no other relationship between the elements in the data structure except the relationship of "belonging to the same set";

2) Linear structure
Linear structure: The elements in the data structure have a one-to-one relationship;

3) Tree structure
Tree structure: The elements in the data structure have a one-to-many relationship;

4) Graphic structure
Graphic structure: The elements in the data structure have many-to-many relationships;

1.2.2 Logical structure
Data structure is logically divided into linear structure and non-linear structure;

Linear structure: There is one and only one start node and one terminal node, and all nodes have at most one direct predecessor and one direct successor.
Typical linear lists include: linked lists, stacks and queues. Their common feature is the linear relationship between data. Except for the head node and the tail node, each node has a unique predecessor and a unique successor, which is the so-called one-to-one relationship.

Nonlinear structure: Corresponding to the linear structure, the nonlinear structure means that each node can have more than one direct predecessor and direct successor. Common nonlinear structures include: trees, graphs, etc.
1.3 Implementation of data structure
1.2.1
Array: An array is a sequence of ordered elements. The allocation in memory is continuous. The array will assign a subscript (index) to the stored elements. This subscript is An auto-increasing continuous, accessing elements in the array is accessed through subscripts; array subscripts are accessed starting from 0;

The advantages of arrays are: fast query speed;

The disadvantages of arrays are: delete, increase, and delete are slow; since the array assigns an index to each element and the index is self-increasing and continuous, once an element is deleted or added, it is necessary to adjust the indexes of all subsequent elements;
Add a new element at index subscript 40 to 3:

Remove 2 index elements:

Summary: Array queries are fast, additions and deletions are slow, and are suitable for frequent queries and few additions and deletions;

1.2.2 Linked
List: A linked list is composed of a series of nodes (also called elements). The logical order of data elements is realized through the pointer addresses of the linked list. Normally, each node contains two parts. One memory address used to store elements is called the data field, and the other is a pointer pointing to the address of the next adjacent node, called the pointer field. According to the different points of the linked list, it can be divided into one-way linked list, two-way linked list, circular linked list, etc. ; What we introduce in this chapter is a one-way linked list, which is also the most common and simplest of all linked lists;
the nodes of the linked list (Node):

Complete linked list:

Advantages of linked lists: adding and deleting nodes is fast;
adding an element to the linked list:

In a one-way linked list, adding an element will only affect the previous node at most, which is much more efficient than adding an element in an array;

Delete an element from a linked list:

Disadvantages of linked list:
1) The query speed is slow, and the query starts from the head to the end. If the element happens to be at the end, the query efficiency is bound to be very low
; The occupancy will be relatively large;
summary: the amount of data is small, needs to be added frequently, deletion operation scenarios are relatively small, and query operations are relatively few;

1.2.3 Stack
Stack: It is a special linear table that can only be operated on one end of the linear table. Operations are allowed on the top of the stack, but not on the bottom of the stack. The characteristics of the stack are: First in, last out. The operation of putting elements from the top of the stack is called pushing (push), and removing elements is called popping (popping).
Push operation:

Pop operation:

The characteristics of the stack: first in, last out, the stack memory in Java is a stack data structure, the method called first will not be popped (popped) until the method called later ends;

1.2.4 Queue
Queue (Queue): Like a stack, a queue is also a linear table, and its limitation is that it only allows insertion at one end of the table and deletion at the other end of the table. The characteristic of the queue is first-in, first-out. The operation of putting elements from one end is called entering the queue, and taking out the elements is called dequeuing;


Characteristics of the queue: first in, first out;

1.2.5 Tree
A tree is a data structure, which is a collection of hierarchical relationships composed of n (n>=1) limited nodes. It is called a "tree" because it looks like an upside-down tree, that is, it has the roots facing up and the leaves facing down. It has the following characteristics:

1) Each node has 0 or more child nodes;
2) A node without a parent node is called a root node;
3) Each non-root node has one and only one parent node;
4) Except for the root node, each child node can Divided into multiple disjoint subtrees;
5) The right subtree is always larger than the left subtree, and the reading order is from left to right;
there are many kinds of trees, such as balanced binary tree (AVL), red-black tree RBL (RB Tree), B-tree (B-Tree), B+ tree (B+Tree), etc., but they all evolved from binary trees at first;

Characteristics of a binary tree: Each node has at most two subtrees

1.2.6 Heap
Heap: The heap can be regarded as a binary tree implemented with an array, so it does not use parent pointers or child pointers. The heap is sorted according to the "heap attribute", which determines the position of the node in the tree.


Characteristics of the heap: If the position of a node is k, the position of its parent node is [k/2], and the positions of its two child nodes are 2k and 2k+1 respectively. In this way, without using pointers, we can also move up and down in the tree by calculating the index of the array: from arr[k] to one level up, k is equal to k/2, and k is equal to 2k on the next level. Or 2k+1.

The definition of a heap is as follows: a sequence of n elements {k1, k2, ki,..., kn} is called a heap if and only if it satisfies the following relationship;

(ki <= k2i,ki <= k2i+1) or (ki >= k2i,ki >= k2i+1) which satisfies the expression of the former is called a small top heap (small root heap), and which satisfies the expression of the latter is Big top heap (big root heap), obviously the heap data structure we drew above is a big root heap;

Large and small root heap data structure diagram:

Generally speaking, the heap with the largest root node is called the maximum heap or large root heap, and the heap with the smallest root node is called the minimum heap or small root heap. Common heaps include binary heaps, Fibonacci heaps, etc.

1.2.7 Hash table
A hash table (Hash), also called a hash table, is a data structure that is directly accessed based on key and value (key and value). It is mapped to a position in the collection through key and value, so that Quickly find the corresponding element in the set. It uses the feature of arrays to support access according to subscripts, so hash tables are actually an extension of arrays and evolve from arrays.
The hash table first needs to calculate the location of data storage based on the key, which is the subscript of the array index;

HashValue=hash(key)
The hash table converts the Key into an integer number through a fixed algorithm function, the so-called hash function, and then modulates the number to the length of the array, and the remainder result is regarded as the length of the array. Subscript, store the value in the array space with this number as the subscript. This storage space can make full use of the search advantages of the array to find elements, so the search speed is very fast.

In a hash table, there is an array on the left. Each member of the array includes a pointer, pointing to the head of a linked list. Of course, the linked list may be empty or may have many elements. We assign elements to different linked lists based on some characteristics of the elements. Based on these characteristics, we find the correct linked list, and then find the element from the linked list.

1.2.8 Graph
Graph: A graph is a collection of vertices (elements) that are connected through a series of edges to form a graph data structure. Vertices are represented by circles, and edges are the lines between these circles. Vertices are connected by edges.
Graphs are divided into directed graphs and undirected graphs:

Directed graph: the edge not only connects two vertices, but also has a direction;
undirected graph: the edge only connects two vertices and has no other meaning;


For example, we can think of the graph data structure as a map:

We regard the cities in the map as vertices, and the high-speed rail lines as edges; obviously, our map is an undirected graph, taking Changsha to Shanghai as an example, the cities we pass through include Changsha, Nanchang, Hangzhou, Shanghai, etc. ; Then you can return from Shanghai along the original route;

After realizing the data structure of the graph, we can do some complex algorithm calculations on this data structure, such as breadth-first search algorithm, depth-first search algorithm, etc.;

Breadth search: When a vertex is searched, all sub-vertices of this vertex are searched first, and then the sub-vertex of the next sub-vertex is searched;


For example, the picture above: Take Wuhan as an example to conduct a breadth search.

1) First search for cities such as Hefei, Nanchang, Changsha;

2) Search to Nanjing through Hefei;

3) Then search to Hangzhou and Fuzhou through Nanchang,

4) Finally search to Shanghai through Nanjing; complete the graph traversal search;

The reason for not searching to Hangzhou through Nanjing is because Hangzhou has already been searched through Nanchang, and there is no need to search again;

Depth search: When searching for a vertex, first search a sub-vertex of this vertex to the bottom (sub-vertex of sub-vertex of sub-vertex...), then return to the previous level, continue to search the second sub-vertex all the way to the bottom;


For example, the picture above: Taking Wuhan as an example for in-depth search,

1) First search Hefei, Nanjing, Shanghai and other cities;

2) Return to Wuhan and conduct a search for the second sub-vertex, searching for Nanchang, Hangzhou and other places;

3) Return to Nanchang and search for Fuzhou;

4) Return to Wuhan and search Changsha;

Graph is a relatively complex data structure. It has relatively complex and efficient algorithms for storing data. There are storage structures such as adjacency matrix, adjacency list, cross-linked list, adjacency multiple list, and edge set array. That’s all we know this time;

Remember to like it! ! !
——————————————
Copyright statement: This article is an original article by CSDN blogger “Green Water Long Flow*z” and follows the CC 4.0 BY-SA copyright agreement. Please attach the original text when reprinting Links to sources and this statement.
Original link: https://blog.csdn.net/Bb15070047748/article/details/119208588

Guess you like

Origin blog.csdn.net/tck001221/article/details/131982890