Python data structure heap __ data structure

Source: Internet
Author: User

Heap definition: The heap is a special kind of tree data structure, each node has a value, usually we say the heap of data structure refers to the two-fork tree. The heap is characterized by the maximum (or minimum) value of the root node, and the two children of the root node can also form a subtree with the child's node, which is called a heap.
The heap is divided into two, the Dagen and the small Gan is a tree where each node's key value is not less than (greater than) the key value of its child node. Whether it's a Dagen or a small Gan (the premise is a two-fork heap) can be considered as a complete binary tree. Here is a picture of the form of intuitive experience:

>>> Import HEAPQ #heap和random是python的基础包
>>> Import Random
>>> data=list (range) #注意range对象并不是一个列表, want data to be a list need to convert it to list type >>> data
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> random.choice (data) #从列表中随机选择一个数显示出来, the elements of the original list are not changed at this time
3
>>> random.shuffle (data) #random基础库中的shuffle能打乱列表元素的顺序, the Python official document says it will return an upset list, #实际上只是对原来的列表进行了原地修改
>>> data
[1, 4, 8, 0, 3, 2, 9, 7, 6, 5]
>>> heap=[] #新建一个空堆
>>> for N in data: #用for循环把列表中的元素放在空堆中, the order of the elements in the heap is not in the order of the list, it seems to have been disrupted
Heapq.heappush (heap,n) >>> heap
[0, 1, 2, 4, 3, 8, 9, 7, 6, 5]
>>> Heapq.heappush (heap,0.5) #把0.5 inserted into the heap, automatically rebuilding the heap
>>> Heap
[0, 0.5, 2, 4, 1, 8, 9, 7, 6, 5, 3]
>>> Heapq.heappush (heap,5.1) #堆中插入元素的顺序好像并不是有规律
>>> Heap
[0, 0.5, 2, 4, 1, 5.1, 9, 7, 6, 5, 3, 8]
>>> Heapq.heappush (heap,5.2)
>>> Heap
[0, 0.5, 2, 4, 1, 5.1, 9, 7, 6, 5, 3, 8, 5.2]
>>> Heapq.heappop (Heap)
0
>>> Heapq.heappop (Heap)
0.5
>>> heapq.heappop (Heap) #弹出堆中的元素, the returned values are not sequential
1
>>> myheap=[1,2,3,5,7,8,9,4,10,333] >>> heapq.heapify (myheap) # Converts a list to a heap at linear time (previously built with A For loop)
>>> Myheap
[1, 2, 3, 4, 7, 8, 9, 5, 333] >>> item = myheap[0] #查看堆中最小的值, do not eject
>>> Item
2
>>> heapq.heapreplace (myheap,6) #用一个新的元素替代掉原堆中最小的元素
1
>>> Myheap
[2, 4, 3, 5, 7, 8, 9, 6, 10, 333]
>>> heapq.nlargest (3, Myheap) #返回堆中前3个最大的元素
[333, 10, 9]
>>> Myheap
[2, 4, 3, 5, 7, 8, 9, 6, 10, 333]
>>> heapq.nsmallest (3, Myheap) #返回堆中前3个最小的元素, this does not change the elements in the original heap
[2, 3, 4]
>>> Myheap
[2, 4, 3, 5, 7, 8, 9, 6, 10, 333]

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.