当前位置:网站首页>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]
边栏推荐
- ByteDance ten years of experience, old bird, took more than half a year to sort out the software test interview questions
- The minimum sum of the last four digits of the split digit of leetcode simple problem
- Statistics 8th Edition Jia Junping Chapter 4 Summary and after class exercise answers
- The common methods of servlet context, session and request objects and the scope of storing data in servlet.
- Currently, mysql5.6 is used. Which version would you like to upgrade to?
- 自动化测试中敏捷测试怎么做?
- Functions: Finding Roots of equations
- pytest
- C language do while loop classic Level 2 questions
- HackTheBox-Emdee five for life
猜你喜欢
Leetcode simple question: check whether the numbers in the sentence are increasing
软件测试工作太忙没时间学习怎么办?
[HCIA continuous update] advanced features of routing
CSAPP家庭作业答案7 8 9章
What level do 18K test engineers want? Take a look at the interview experience of a 26 year old test engineer
软件测试Bug报告怎么写?
Fundamentals of digital circuits (I) number system and code system
How to transform functional testing into automated testing?
Cadence physical library lef file syntax learning [continuous update]
What is an index in MySQL? What kinds of indexes are commonly used? Under what circumstances will the index fail?
随机推荐
With 27K successful entry ByteDance, this "software testing interview notes" has benefited me for life
Build your own application based on Google's open source tensorflow object detection API video object recognition system (II)
Logstack introduction and deployment -- elasticstack (elk) work notes 019
Wang Shuang's detailed learning notes of assembly language II: registers
The minimum number of operations to convert strings in leetcode simple problem
王爽汇编语言学习详细笔记一:基础知识
How to learn automated testing in 2022? This article tells you
JDBC介绍
Function: find the maximum common divisor and the minimum common multiple of two positive numbers
China's county life record: go upstairs to the Internet, go downstairs' code the Great Wall '
[issue 18] share a Netease go experience
Cadence physical library lef file syntax learning [continuous update]
Leetcode simple question: check whether two strings are almost equal
Vysor uses WiFi wireless connection for screen projection_ Operate the mobile phone on the computer_ Wireless debugging -- uniapp native development 008
UCORE lab1 system software startup process experimental report
ucore lab6 调度器 实验报告
The common methods of servlet context, session and request objects and the scope of storing data in servlet.
Soft exam information system project manager_ Project set project portfolio management --- Senior Information System Project Manager of soft exam 025
Build your own application based on Google's open source tensorflow object detection API video object recognition system (I)
Function: find the root of the equation by Newton iterative method