当前位置:网站首页>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]
边栏推荐
- MySQL development - advanced query - take a good look at how it suits you
- Wang Shuang's detailed notes on assembly language learning I: basic knowledge
- How to solve the poor sound quality of Vos?
- Global and Chinese market of maleic acid modified rosin esters 2022-2028: Research Report on technology, participants, trends, market size and share
- CSAPP Shell Lab 实验报告
- CSAPP家庭作业答案7 8 9章
- Pointer -- output all characters in the string in reverse order
- The four connection methods of JDBC are directly coded
- How to rename multiple folders and add unified new content to folder names
- 软件测试方法有哪些?带你看点不一样的东西
猜你喜欢
Statistics 8th Edition Jia Junping Chapter 4 Summary and after class exercise answers
How to rename multiple folders and add unified new content to folder names
Capitalize the title of leetcode simple question
Description of Vos storage space, bandwidth occupation and PPS requirements
Rearrange spaces between words in leetcode simple questions
Want to learn how to get started and learn software testing? I'll give you a good chat today
Leetcode simple question: check whether two strings are almost equal
自动化测试你必须要弄懂的问题,精品总结
MySQL development - advanced query - take a good look at how it suits you
Nest and merge new videos, and preset new video titles
随机推荐
Global and Chinese market of barrier thin film flexible electronics 2022-2028: Research Report on technology, participants, trends, market size and share
Stc-b learning board buzzer plays music 2.0
Practical cases, hand-in-hand teaching you to build e-commerce user portraits | with code
Keil5-MDK的格式化代码工具及添加快捷方式
Why can swing implement a form program by inheriting the JFrame class?
About the garbled code problem of superstar script
Global and Chinese markets of MPV ACC ECU 2022-2028: Research Report on technology, participants, trends, market size and share
CSAPP Shell Lab 实验报告
What are the business processes and differences of the three basic business modes of Vos: direct dial, callback and semi direct dial?
UCORE lab8 file system experiment report
Query method of database multi table link
Global and Chinese markets of cobalt 2022-2028: Research Report on technology, participants, trends, market size and share
[pointer] octal to decimal
全网最详细的postman接口测试教程,一篇文章满足你
{1,2,3,2,5} duplicate checking problem
ucore lab8 文件系统 实验报告
Global and Chinese markets for GaN on diamond semiconductor substrates 2022-2028: Research Report on technology, participants, trends, market size and share
"If life is just like the first sight" -- risc-v
Global and Chinese markets of electronic grade hexafluorobutadiene (C4F6) 2022-2028: Research Report on technology, participants, trends, market size and share
Differences between select, poll and epoll in i/o multiplexing