当前位置:网站首页>Heap, stack, queue
Heap, stack, queue
2022-07-06 15:09:00 【Naive code writing】
Pile up

It's a binary tree , The value of each of its parent nodes will only be less than or equal
On all child nodes ( Value ).
• It uses arrays to implement : Count from zero , For all k ,
There are heap[k] <= heap[2k+1] and heap[k] <= heap[2k+2].
• The most interesting feature of heap is that the smallest element is always at the root node :heap[0].
| function | describe |
|---|---|
| heapq.heapify(x) | take list x Into piles |
| heapq.heappush(heap, item | take item Add the value of heap in , Keep the heap invariant . |
| heapq.heappop(heap) | Pop up and return heap The smallest element of , Keep the heap invariant . Use heap[0] , You can access only the smallest element without popping it up . |
heapq.heappushpop(heap, item)
• take item Put in a pile , And then pop up and go back
heap The smallest element of .
• The heap size remains the same .
• Put it in first 、 Pop it up again
Interactive function :
heapq.heapreplace(heap, item)
• Pop up and return heap The smallest of all ,
And push in New item.
• The size of the heap doesn't change .
• First pop up one , Put one more
• Replace and then sort
The generic function :
heapq.merge(*iterables)
Merge multiple sorted inputs into one sorted output ( for example ,
Merge time stamped entries from multiple log files ). Return to scheduled
Ordinal valued iterator.
heapq.nlargest(n, iterable)
from iterable The defined dataset is returned before n The largest elements
A list of .
heapq.nsmallest(n, iterable)
from iterable The defined dataset is returned before n Composed of the smallest elements
A list of .
practice :
Build a heap :
• Delete the smallest value ;
• Add a value to it ;
import heapq
x=[1,2,3,4,6,7,9,14,15,17,23,36,0]
heapq.heapify(x)
print(x)
heapq.heappop(x)
print(x)
heapq.heappush(x,8)# The previous heap has changed , This change is the result of the last time
print(x)
Running results :
[0, 2, 1, 4, 6, 3, 9, 14, 15, 17, 23, 36, 7]
[1, 2, 3, 4, 6, 7, 9, 14, 15, 17, 23, 36]
[1, 2, 3, 4, 6, 7, 9, 14, 15, 17, 23, 36, 8]
Stacks and queues :
queue :
principle : fifo
Application scenarios
• Historical record : First generated , Discard at the earliest
• The data backup : First generated , Discard first
• Printer control
• CPU Distribution, etc
Realization :
from collections import deque # Need to import
x=deque([9,7,8])
print(x)
x.append(666)
print(x)
x.popleft() # No parameters , The function defines the execution from the left
print(x)
Output results :
deque([9, 7, 8])
deque([9, 7, 8, 666])
deque([7, 8, 666])
Stack :
principle : Last in, first out
Application scenarios :
• Operation fallback :Ctrl+Z
• Database system and other scenarios
• The browser goes back to the previous page
• Parentheses matching
Realization :
x=[9,7,8]
print(x)
x.append(666)
print(x)
x.pop()
print(x)
Running results :
[9, 7, 8]
[9, 7, 8, 666]
[9, 7, 8]
边栏推荐
- What is an index in MySQL? What kinds of indexes are commonly used? Under what circumstances will the index fail?
- Function: find the root of the equation by Newton iterative method
- Keil5 MDK's formatting code tool and adding shortcuts
- [issue 18] share a Netease go experience
- 全网最详细的postman接口测试教程,一篇文章满足你
- Function: calculates the number of uppercase letters in a string
- [pointer] find the value of the largest element in the two-dimensional array
- Which version of MySQL does php7 work best with?
- Emqtt distribution cluster and node bridge construction
- Daily code 300 lines learning notes day 9
猜你喜欢

王爽汇编语言详细学习笔记二:寄存器

CSAPP家庭作业答案7 8 9章

Stc-b learning board buzzer plays music

Login the system in the background, connect the database with JDBC, and do small case exercises

如何成为一个好的软件测试员?绝大多数人都不知道的秘密

Opencv recognition of face in image

ucore lab6 调度器 实验报告

Es full text index

Get started with Matplotlib drawing

C language do while loop classic Level 2 questions
随机推荐
MySQL development - advanced query - take a good look at how it suits you
Quaternion -- basic concepts (Reprint)
Global and Chinese markets for complex programmable logic devices 2022-2028: Research Report on technology, participants, trends, market size and share
Flash implements forced login
HackTheBox-Emdee five for life
Face and eye recognition based on OpenCV's own model
Global and Chinese market of RF shielding room 2022-2028: Research Report on technology, participants, trends, market size and share
Thinking about three cups of tea
Global and Chinese market of maleic acid modified rosin esters 2022-2028: Research Report on technology, participants, trends, market size and share
Function: string storage in reverse order
Global and Chinese market for antiviral coatings 2022-2028: Research Report on technology, participants, trends, market size and share
Detailed introduction to dynamic programming (with examples)
Expanded polystyrene (EPS) global and Chinese markets 2022-2028: technology, participants, trends, market size and share Research Report
如何成为一个好的软件测试员?绝大多数人都不知道的秘密
自动化测试你必须要弄懂的问题,精品总结
How to learn automated testing in 2022? This article tells you
STC-B学习板蜂鸣器播放音乐2.0
Pointers: maximum, minimum, and average
5 minutes to master machine learning iris logical regression classification
Wang Shuang's detailed notes on assembly language learning I: basic knowledge