Data Structure && Algorithm

Knowledge of common data structures

1. 数组: The most basic data structure, using continuous memory to store numbers. When creating an array, we need to first specify the capacity of the array, and then allocate memory according to the size. Even if we store only one number in the array, we need to pre-allocate memory for all the data. Therefore, the space efficiency of the array is not very good, and there are often free areas that are not fully utilized. ( 可动态分配内存来解决上述问题) Since the memory in the array is continuous, any element can be read/written according to the array subscript O(1) time, so its time efficiency is very high.

2. 字符串: The most basic data structure, using continuous memory to store characters. C/C++中每个字符串都以字符’\0’作为结尾, so that we can easily find the last tail of the string.

3. Linked list: The structure of the linked list is very simple. Several nodes are connected into a chain structure by pointers, and the linked list is a dynamic data structure that needs to operate on pointers, so the linked list is very flexible. When creating a linked list, we don't need to know the length of the linked list. When inserting a node, we just need to allocate memory for the new node, and then adjust the pointer to ensure that the new node is linked into the linked list. Memory allocation is not done once when the linked list is created, but memory is allocated every time a node is added. 由于没有闲置的内存,链表的空间效率比数组高.

4. Tree: Each node has only one parent node except the root node, and the root node has no parent node; all nodes except the leaf node have one or more child nodes, and the leaf node has no child nodes. 父节点和子节点之间用指针链接. 二叉树: It is a special structure of the tree, and each node in the binary tree can only have at most two child nodes. 二叉搜索树: The left child node is always less than or equal to the root node, and the right child node is always greater than or equal to the root node. We can find a node in a binary search tree by value in O(logn) time on average. : Divided into max heap and min heap. The root node has the largest value in a max-heap, and the root node has the smallest value in a min-heap.

5. Stack: The stack is a data structure closely related to recursion. It is widely used in the computer field. For example, the operating system will create a stack for each thread to store the parameters, return addresses and temporary variables of each function when the function is called. wait. The characteristic of the stack is 先进后出that the last element pushed into the stack will be the first to be popped. Usually a stack is a data structure that doesn't consider sorting, and we need O(n) time to find the maximum or minimum value in the stack.

6. Queue: The queue is closely related to the breadth-first traversal algorithm, and the characteristics of the queue are 先进先出.

Similarities and differences between different sorting algorithms?

insert image description here

Knowledge of depth-first search (DFS) and breadth-first search (BFS)

DFS (Depth First Search)

The main steps of depth-first search:
1. Recurse down.
2. Go back up.
深度优先则是以深度为准则,先一条路走到底,即为递归下去。如果没有递归时没有达到目标又无路可走了,那么则退回到上一步的状态,走其他路。即为回溯上来.

The important point of DFS is state backtracking .

insert image description here

BFS (Breadth First Search)

Compared with the depth-first search of a road to the black, 广度优先搜索在面临一个路口时,把所有的岔路口都记下来,然后选择其中一个进入,然后将它的分路情况记录下来,然后再返回来进入另外一个岔路,并重复这样的操作.

The focus of BFS is on the selection and marking of states .
insert image description here

Summary of DFS and BFS

DFS uses a recursive form, using a stack structure, first in last out. The BDS selects the state in the form of a queue, first in first out.

The complexity of DFS is basically the same as that of BFS. The difference is that the way of traversal is different from the starting point of looking at the problem. DFS is suitable for tasks with clear goals, while BFS is suitable for large-scale searches.

From an algorithmic point of view, all situations are exhausted.

Guess you like

Origin blog.csdn.net/toCVer/article/details/126130287