当前位置:网站首页>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]
边栏推荐
- Public key box
- “Hello IC World”
- Common Oracle commands
- How to solve the poor sound quality of Vos?
- Software testing interview summary - common interview questions
- What are the business processes and differences of the three basic business modes of Vos: direct dial, callback and semi direct dial?
- Nest and merge new videos, and preset new video titles
- How to transform functional testing into automated testing?
- My first blog
- Function: string storage in reverse order
猜你喜欢

Express

China's county life record: go upstairs to the Internet, go downstairs' code the Great Wall '

Réponses aux devoirs du csapp 7 8 9

Description of Vos storage space, bandwidth occupation and PPS requirements

Fundamentals of digital circuits (II) logic algebra

The minimum number of operations to convert strings in leetcode simple problem
![Cadence physical library lef file syntax learning [continuous update]](/img/0b/75a4ac2649508857468d9b37703a27.jpg)
Cadence physical library lef file syntax learning [continuous update]

Wang Shuang's detailed learning notes of assembly language II: registers

Report on the double computer experiment of scoring system based on 485 bus

基于485总线的评分系统双机实验报告
随机推荐
The minimum sum of the last four digits of the split digit of leetcode simple problem
Function: find 1-1/2+1/3-1/4+1/5-1/6+1/7-... +1/n
Summary of thread implementation
Vysor uses WiFi wireless connection for screen projection_ Operate the mobile phone on the computer_ Wireless debugging -- uniapp native development 008
ucore lab1 系统软件启动过程 实验报告
Why can swing implement a form program by inheriting the JFrame class?
Global and Chinese markets of Iam security services 2022-2028: Research Report on technology, participants, trends, market size and share
With 27K successful entry ByteDance, this "software testing interview notes" has benefited me for life
DVWA exercise 05 file upload file upload
软件测试行业的未来趋势及规划
How to solve the poor sound quality of Vos?
The minimum number of operations to convert strings in leetcode simple problem
How to rename multiple folders and add unified new content to folder names
[pointer] find the value of the largest element in the two-dimensional array
[pointer] solve the last person left
Pedestrian re identification (Reid) - data set description market-1501
“Hello IC World”
ucore lab7 同步互斥 实验报告
The four connection methods of JDBC are directly coded
How to use Moment. JS to check whether the current time is between 2 times