What is a heap? What is a stack? What are the differences and connections between them?

Author: ordinary
link: https: //www.zhihu.com/question/19729973/answer/433057897
Source: know almost
copyrighted by the author. For commercial reproduction, please contact the author for authorization, and for non-commercial reproduction, please indicate the source.

It is often said in the data structure that the stack , what is the difference between the stack? What are the good uses of these data structures in algorithm thinking?

First of all, we usually end up serving plates. We all know that plates are generally stacked one by one, and we compare plates to data, and the stacked plates are very much like a stack .

 

 

Last-out of such a structure is a stack data structure, the data stored in the first memory when we a, taking e, d, c, b, a then b, c, d, e.

The same heap is also a special type of data structure. Unlike the stack, the memory of the heap is managed by the local language system environment (such as python virtual machine) and the stack is automatically allocated by the system when applying for memory , and the system Allocation is also limited in size. After this size is exceeded, a memory overflow error will be reported, similar to the character overflow defined below.

 

 

The heap memory is generally allocated and managed by the language runtime environment , such as the Python virtual machine to control the memory and runtime environment:

 

 

So the memory can be combined and associated, so that it is not easy to be limited by the system

 

 

The heap is different from the stack in data access. The stack is first in and out, and the heap is a priority queue (not a queue, the heap is usually an array object that can be regarded as a tree.), The so-called priority The queue takes out elements according to their priority. For example, generally at the dinner table, no matter if you come first, then the grandparents should first move the chopsticks, then the parents, then you, like this:

 

 

With such priority enjoyment, its strict logical data structure is defined as follows:

 

 

With this definition, let's try to use the heap data structure to do a sort called heap sort . We first get a set of elements that need to be sorted:

16,7,3,20,17,8

First build a tree:

 

 

According to the definition of the heap data structure, that is, the data of any non-leaf node is not greater than or less than the data of its left and right child nodes

, Adjusted as follows:

 

 

 

 

 

 

 

 

Until the adjustment is as above, it means that the largest is on the top, each child node below is smaller than the parent node, and the heap is initialized. Now to sort the heap (note) , we want to arrange from left to right, from small to large, according to the nature of the heap, we can arrange like this:

First we exchange the largest and last position

 

 

In addition to the elements marked in red, other data is rearranged according to the nature of the heap.

 

 

Continue to move the largest element except the red mark to the end

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Until the root node is left, the sorting has been sorted at this time. The above is the sorting algorithm using the data structure heap. We use Python code to achieve the following:

def heap_sort(lst):

def sift_down(start, end):

"" "Maximum heap adjustment" ""

root = start

while True:

child = 2 * root + 1

if child > end:

break

if child + 1 <= end and lst[child] < lst[child + 1]:

child += 1

if lst[root] < lst[child]:

lst[root], lst[child] = lst[child], lst[root]

root = child

else:

break

# Create maximum heap

for start in range((len(lst) - 2) // 2, -1, -1):

sift_down(start, len(lst) - 1)

# Heap sort

for end in range(len(lst) - 1, 0, -1):

lst[0], lst[end] = lst[end], lst[0]

sift_down(0, end - 1)

return lst

if __name__ == "__main__":

list = [16,7,3,20,17,8]

heap_sort(list)

print(list)

operation result:

 

 

After calculating the complexity, we found that heap sorting is also an excellent sorting. That stack is, in fact, I suddenly remembered one thing here is that there is a Markov chain when learning the Viterbi algorithm, which includes the backtracking algorithm , online search It is found that the data structure of the stack corresponds to this algorithm idea. Let's write a practical example to take a look. First, let's take a look at what is backtracking. Let me give an example.

As shown in the figure below, there is now a small ant P who wants to find food T in the maze. The way to find it is as follows. We take each fork as a node and record it. Suppose that the ant starts from node 1 and follows 2, 3, and 4 It's a dead end, so return to node 1. The process of returning is backtracking. When you reach node 5, you will come back to the wall (backtracking) and back to the left and right (backtracking). You will only find the food when you go down. .

 

 

In order to facilitate code implementation, we use 1 for the wall, 0 for the place to go, 666 for the food, and the matrix for the entire maze:

 

 

The thoughts and ideas are this:

1. Record the bifurcation where the small ant walked

2. Record the path taken by the small ant (using the stack data structure)

3. When the small ant finds that the road is impassable, or encounters the same fork, Yanyuan Road returns (the path data is taken out of the stack data). When returning to the fork, the data of the road that is not accessible in the direction of the fork is marked. Continue in the unmarked direction (if both are marked, continue to return to the original way) until you find the food.

I will not post the specific implementation code. This is the application of the stack data structure in the backtracking algorithm.

Hahaha, after a day of tossing, I hope that a big brother can point out mistakes and make progress together.

Reference books:

Introduction to Algorithms Third Edition

Python data structure

Guess you like

Origin www.cnblogs.com/cheari/p/12741027.html
Recommended