Priority queue (HEAP)

Source: Internet
Author: User

Priority queue is a data structure that allows at least two types of operations: insert and deletemin (the smallest one to delete ). It is equivalent to the enqueue and dequeue operations in the queue.

Priority Queues can be implemented using linked lists, binary search trees, and binary heap.

Binary heap

1. Structure

Heap is a fully filled binary tree. The possible exception is that it is at the bottom layer, and the elements at the bottom layer are filled in from left to right. Such a tree is called a Complete Binary Tree.

A Complete Binary Tree with a height of H has 2 h to 2 h + 1-1 nodes. The height of the Complete Binary Tree is logn.

The full binary tree can be represented by an array. If the value starts from 0, the left son of the element at any position I in the array is at the position 2I + 1, the right son is at 2I + 2 and the parent node is at floor (I-1)/2.

2. Heap Sequence

Minimum heap: For each node in the heap, the keywords of its subnodes are greater than those of its keywords, and the root node is the minimum value.

Max heap: For each node in the heap, the keywords of its subnodes are smaller than those of its keywords, and the root node is the maximum value.

3. Basic heap operations

Insert:

The element x to be inserted is placed at the end of the heap. If the heap structure is not damaged, the operation is completed. Otherwise, the X is filtered.

If the element to be inserted is always filtered to the root, the insertion time is as high as O (logn ). On average, it seems that the filtering will be terminated early. Basically, an average of 2.607 insert operations are required.

Deletemin:

For the minimum heap, delete the root node, put the node at the end of the heap to the root, and then perform the filter operation. The average operation time of an operation is O (logn ).

Other operations on the minimum heap:

Decreasekey (value for downgrading keywords): performs the filter operation.

Increasekey (value for adding a keyword): Perform the filter operation.

Delete: place the value at the end of the heap at the deleted node and perform the filter operation.

Buildheap (build heap ):

1. Execute n insert operations. The total running time is O (n ).

2. Start from the parent node of the node at the end of the heap, and perform the Filter Operations in sequence to the root direction. The total running time is O (n ).

Code:

class MinHeap(object):    def __init__(self):        self.heap=[]        self._count=0    def __len__(self):        return self._count    def insert(self,key):        self.heap.append(key)        self._count+=1        self._up(self._count-1)    def deleteMin(self):        a=self.heap.pop()        self.heap[0]=a        self._count-=1        self._down(0)    def _up(self,i):        if i>0:            parent=(i-1)/2            if self.heap[parent]>self.heap[i]:                self.heap[parent],self.heap[i]=self.heap[i],self.heap[parent]                self._up(parent)    def _down(self,i):        left=2*i+1        right=2*i+2        small=i        if left<self._count and self.heap[i]>self.heap[left]:            small=left        if right<self._count and self.heap[right]<self.heap[small]:            small=right        if small!=i:            self.heap[i],self.heap[small]=self.heap[small],self.heap[i]            self._down(small)

  

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.