Detailed explanation of the most commonly used data structures for brushing questions: set (set), stack (stack), queue (deque\queue), hash table (hashmap), heap (heap)

When power button brush title, in addition to the most basic list (array), string manipulation, often used collection, stack, queue (deque), hash table, heap five auxiliary data structure problem solving, below It is a detailed explanation of the principle characteristics of these data structures, and the usage in python.

Time complexity of each data structure method:
https://blog.csdn.net/weixin_44414948/article/details/113934407

1. Set

Principle : It is the set concept learned in the mathematics class. There are collections, unions, and differences. The composition will not be repeated. . .

Features :

1. Disorder
2. Determinism (it is not allowed to access subscripts and modify elements during programming)
3. Non-repetitiveness (usually used for list de-duplication)

Python implementation :

# 定义
s = set()

# 增,用法类似于list.append()
s.add()

# 删
s.pop(1)  # 删除指定索引的元素,不传参则随机删除一个
s.remove(x)  # 删除指定值的元素,必须传参
s.clear()  # 清空set
s.discard()  # 和remove类似,不传参不会报错

# 改
# 和字符串一样,不允许中途修改元素的操作,不支持下标索引的方式
s[0] # 这类的操作是不允许的

# 查
for s_i in s: # 使用for in 形式遍历,下标的遍历不允许

# 逻辑运算
s1 & s2  # 求两集合的交集
s1 | s2  # 求两集合的并集
s1 - s2  # 求两集合的差集

Usage: List de-duplication

A = [1, 3, 2, 3, 2, 4, 5, 6]
A = set(A)
print(A)  # 结果为去重后的A,即[1, 3, 2, 4, 5, 6],顺序随机

For example:

Power button brushing questions: 448. Find all the missing numbers in the array (set difference method)
https://blog.csdn.net/weixin_44414948/article/details/113803271

2. Stack

Insert picture description here
Principle: A
stack is essentially a container, storing elements, much like a list.

Features:
first in, first out, last in first out (as opposed to queues)

Python implementation:
Since the stack is very similar to the list, you can use the list as a stack when you brush questions, use the pop() method to pop the stack, and the append() method to push the stack. It is enough to be skilled in these brushing questions.

# 定义
stack = [1, 2, 3, 4]

# 入栈
stack.append(5)

# 出栈
stack.pop() # 默认删除最后一个元素,即最后入栈的 5

For example:

Power button brushing questions: 20. Effective brackets (use the stack for matching)
https://blog.csdn.net/weixin_44414948/article/details/114088834

3. Queue (deque\queue)

Principle:
Similar to queuing in real life, its elements can be seen as people lining up.

Features:
First in, first out, last in, last out (both sides of the deque can be in and out)

Python implementation:
Generally, you use the double-ended queue deque in the collections library to master the enqueue and dequeue operations.

import collections
# 定义
queue = collections.deque()

# 入队
queue.append(1)  # 在队列右侧加入
queue.appendleft(2)  # 在队列左侧加入

# 出队
queue.pop()  # 右侧出队
queue.popleft()  # 左侧出队

For example:

1. Likou brushing questions: 101. Symmetrical binary tree (queue)
https://blog.csdn.net/weixin_44414948/article/details/113748957
2. Likou brushing questions: Sword refers to Offer 32-III. From top to bottom Print Binary Tree III (simple list, queue)
https://blog.csdn.net/weixin_44414948/article/details/113783424

4. Hash table (hashmap)

Principle:
A data structure that is directly accessed based on the key value. In other words, it accesses the record by mapping the key code value to a location in the table to speed up the search. It is the dictionary dict in python.

Features: It
can be searched by key-value pairs, with fast search speed and high efficiency.

Python implementation:
The basic data type of the dict that comes with python is a hash table. Generally, the built-in dict() is not used when writing questions, but collections.defaultdict() is used , mainly because the dictionary type of the library allows for non-existent Key to add elements , the following is the basic operation method of the dictionary.

The true meaning of defaultdict realizes a kind of global initialization, accessing any key will not throw a KeyError exception;
defaultdict(int): initialized to 0
defaultdict(float): initialized to 0.0
defaultdict(str): initialized to ”
defaultdict(list ): Initialized to [], the most commonly used list when brushing questions

# 定义
import collections

s = [('yellow',1),('blue', 2), ('yellow', 3), ('blue', 4), ('red', 1)]
d = collections.defaultdict(list)
for k, v in s:
    d[k].append(v)  #将【值v】加入进【字典d】的【键k】中
a=sorted(d.items()) #排序
print(a)

For example:

1. Likou brushing questions: 697. Degree of array (classic hash table question)
https://blog.csdn.net/weixin_44414948/article/details/113881244
2. Likou brushing questions: 1128. Equivalent domino pair Quantity (hash table + permutation and combination)
https://blog.csdn.net/weixin_44414948/article/details/113177535

5. Heap

Principle: The
stack is a last-in-first-out data structure, and the heap is a sorted tree-shaped data structure, and each node has a value.

Characteristics:
The characteristic of the heap is that the value of the root node is the smallest (or largest), and the two trees of the root node are also a heap. Because of this characteristic of the heap, it is often used to implement priority queues.

For those of us who are writing questions, we only need to know that the heap is a data structure that can be automatically sorted by itself . Students who are interested in the built-in add and pop methods can take a look at the bottom of the expanded knowledge.

Python implementation: You
only need to be familiar with the characteristics of the heap to brush the questions, and you can use python's heap definition, addition, and deletion.
Use python's heapq library to define the heap.

import heapq
# 将列表转为堆
L1 = [4, 5, 1, 6, 2, 7, 3, 8]
heapq.heapify(L1)

# 堆添加元素
h = []
heapq.heappush(h, 6)  # 注意此处 h 为列表

# 堆删除最小元素,即最上面的元素
L1 = [4, 5, 1, 6, 2, 7, 3, 8]
heapq.heapify(L1)
heapq.heappop(L1)

For example:

Power button brush question: 5638. The maximum number of apples (heap, this question is not difficult to pile up)
https://blog.csdn.net/weixin_44414948/article/details/111824441

The following is the extended knowledge of the heap:

————————————————————————————————
The steps of adding elements to the heap and automatically sorting are shown in the figure below. The goal is to Find a suitable location for the new element in the heap and insert it . Proceed as follows:

(1) First insert the element at the bottom of the heap. In the implementation of the array, this is the position after the last element in the array
—————————————————————— ————————————
(2) Then, enter a loop, as long as the value of the new element is less than the value of its parent node, the loop will let the new element "walk" up the heap and move the new element The element exchanges with its parent node. When this process stops (either the new element is greater than or equal to its parent node, or the top node is reached), the new element is in its proper position.

Insert picture description here
The code for the built-in add element add() is as follows:

    def add(self, item):
        self._size += 1
        self._heap.append(item)
        curPos = len(self._heap) - 1
        while curPos > 0:
            parent = (curPos - 1) // 2
            parentItem = self._heap[parent]
            if parentItem <= item:
                break
            else:
                self._heap[curPos] = self._heap[parent]
                self._heap[parent] = item
                curPos = parent

Delete operation pop:
The goal of deletion is to return the elements in the node after deleting the root node, and adjust the positions of other nodes to maintain heap attributes. The following is the delete operation strategy:

(1) First, save the pointers of the top element and the bottom element in the heap, and move the element from the bottom of the heap to the top
—————————————————————— ————————————
(2) Go down from the top of the pile and move the smallest element one layer up until it reaches the bottom of the pile

Insert picture description here
The code for the built-in element pop() is as follows:

    def pop(self):
        if self.isEmpty():
            raise Exception("Heap is empty")
        self._size -= 1
        topItem = self._heap[0]
        bottomItem = self._heap.pop(len(self._heap) - 1)
        if len(self._heap) == 0:
            return bottomItem
           
        self._heap[0] = bottomItem
        lastIndex = len(self._heap) - 1
        curPos = 0
        while True:
            leftChild = 2 * curPos + 1 
            rightChild = 2 * curPos + 2
            if leftChild > lastIndex:
                break
            if rightChild > lastIndex:
                maxChild = leftChild;
            else:
                leftItem  = self._heap[leftChild]
                rightItem = self._heap[rightChild]
                if leftItem < rightItem:
                    maxChild = leftChild
                else:
                    maxChild = rightChild
            maxItem = self._heap[maxChild]
            if bottomItem <= maxItem:
                break
            else:
                self._heap[curPos] = self._heap[maxChild]
                self._heap[maxChild] = bottomItem
                curPos = maxChild
        return topItem

Reference source for expanding knowledge: https://blog.csdn.net/dta0502/article/details/80834787

Guess you like

Origin blog.csdn.net/weixin_44414948/article/details/114285060