A brief discussion of eight data structures

This article is reproduced from: https://blog.csdn.net/Bb15070047748/article/details/119208588

For more details, please search " " on WeChat前端爱好者 and click me to view .

1.1 Data structure

Data structure is one of the fundamental concepts of computer science and involves how data is organized and stored so that it can be accessed, managed, and manipulated efficiently. A data structure is a way of describing or representing data. It defines how data is stored, relationships, and methods of data manipulation.

Here are some common data structure types:

  1. Tree : This is a non-linear data structure used to represent data with hierarchical relationships. A common example of a tree is a binary tree, where each node has at most two child nodes.
  2. Graph : This is a non-linear data structure used to represent any number of nodes and the connections between them. Graphs can be undirected or directed, can contain cycles, and can be self-connected.
  3. Heap : This is a data structure used to store unique elements. The main operations of collections are adding elements (add) and removing elements (remove).
  4. Hash table/hash table (Hash) : This is a data structure used to implement fast lookup operations. Hash tables map keys into buckets through a hash function, and then lookup and store them in the buckets.
  5. Stack : This is a last-in-first-out (LIFO) data structure that can only be inserted and deleted from one end. The main operations of the stack are push (adding elements) and pop (removing elements).
  6. Queue : This is a first-in-first-out (FIFO) data structure that can add elements from one end and delete elements from the other end. The main operations of the queue are enqueue (adding elements) and
  7. Array : This is a linear data structure that can be viewed as a series of elements of the same type. Arrays are contiguous in memory and elements at any position can be accessed by index.
  8. Likend List : This is a linear data structure consisting of a series of nodes, each node contains a value and a pointer to the next node. A linked list does not require all elements to be contiguous in memory.

These common data structures each have their own advantages and disadvantages, and the appropriate data structure should be selected according to the specific application scenario.

For example, for the storage and access of a large number of elements, arrays and hash tables may be better choices; for data that needs to keep element insertion and deletion operations efficient, linked lists and dynamic arrays may be better choices; for data that needs to keep element search operations For efficient data manipulation, search trees may be a better choice.

Furthermore, data structures and algorithms are often closely related.

Understanding and mastering various data structures and their operation methods can help us design and implement algorithms more effectively and optimize program performance.

1.2 Classification of data structures

1.2.1 Arrangement

gather

Set: In addition to the mutual relationship of "belonging to the same set" between elements in the data structure 别无其他关系;

linear structure

Linear structure: The elements in the data structure have a one-to-one relationship with each other;

tree structure

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

Graphical structure

Graph structure: The elements in the data structure have many-to-many relationships;

1.2.2 Logical structure

Data structures are logically divided into linear structures and nonlinear structures;

Linear structure:

There is 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.

Linear 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.3.1 Array

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. Access the array The elements are accessed through subscripts; array subscripts are accessed starting from 0;

The advantages of arrays are: fast query speed ;

The disadvantages of arrays are : deletion, increase, and deletion 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, the indexes of all subsequent elements need to be adjusted;

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.3.2 Linked list

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 is used to store the memory address of the element, called the data field, and the other is a pointer to the address of the next adjacent node, called the pointer field;

According to the different directions of the linked list, it can be divided into one-way linked list, two-way linked list, circular linked list, etc.;

What we are introducing is a one-way linked list, which is also the most common and simplest of all linked lists;

Linked list node (Node):

Complete linked list:

Advantages of linked lists: adding nodes and deleting nodes is fast;

Add a new element to the linked list:

在单向链表中,新增一个元素最多只会影响上一个节点,比在数组中的新增效率要高的多;

Delete an element from a linked list:

Disadvantages of linked lists:

  • 1) The query speed is slow. The query starts from the head and continues to the tail. If the element happens to be at the end, the query efficiency will be very low;
  • 2) The linked list has the overhead of adding a pointer field to the array, and the memory usage will be relatively large;
总结:数据量较小,需要频繁增加,删除操作的场景,查询操作相对较少;

1.3.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: 先进后出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:

Characteristics of the stack: First in, last out. The stack memory in Java is a stack data structure. The method called first will not pop up the stack until the method called later finishes;

1.3.4 Queue

Queue: Like a stack, a queue is also a linear list. Its restriction is that only insertions are allowed at one end of the table and deletions are allowed at the other end of the table.

The characteristic of the queue is 先进先出that the operation of putting elements from one end is called enqueueing, and taking out elements is called dequeuing;

Characteristics of the queue: first in, first out;

1.3.5 Tree

A tree is a data structure, which is a collection of n (n>=1) limited nodes composed of a hierarchical relationship.

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 be 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 classifications 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.3.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 the tree by calculating the index of the array: from arr[k] one level up, let k equal k/2, and down one level let k equal 2k 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.3.7 Hash table

Hash, also called hash table, is a data structure that is directly accessed based on keys and values.

Map to a position in the collection through key and value, so that the corresponding element in the collection can be quickly found.

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. The remainder result is used as the subscript of the array, and the value is stored. 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.3.8 Figure

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: An edge not only connects two vertices, but also has a direction;
  • Undirected graph: Edges only connect two vertices and have 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 it passes through include Changsha, Nanchang, Hangzhou, Shanghai and other places; then you can also return from Shanghai according to the original route;

After realizing the data structure of 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;

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 with relatively complex and efficient algorithms for storing data, including adjacency matrix, adjacency list, cross linked list, adjacency multiple list, edge set array and other storage structures.

Reference documentation

  • https://blog.csdn.net/Bb15070047748/article/details/119208588

Guess you like

Origin blog.csdn.net/BradenHan/article/details/135258416