The Pseudo codes given below are for a max-priority queue. Let's learn to code these operations to make a priority queue. But we may also face a situation in which we need to change the key of an element, so Increase/Decrease key is used to do that. With these operations, we have fulfilled most of our demand of a priority queue i.e., to insert data into the queue and take data from the queue. The entire point of the priority queue is to get the data according to the key of the data and the Maximum/Minimum and Extract Maximum/Minimum does this for us. So, inserting a new data must go in a place according to the specified order. Increase/Decrease key → To increase or decrease key of any element in the queue.Ī priority queue stores its data in a specific order according to the keys of the elements. Extract Maximum/Minimum → To remove and return the maximum and the minimum element from the max-priority queue and min-priority queue respectively.Ĥ. Maximum/Minimum → To get the maximum and the minimum element from the max-priority queue and min-priority queue respectively.ģ. Insert → To insert a new element in the queue.Ģ. There are mainly 4 operations we want from a priority queue:ġ. We use a max-heap for a max-priority queue and a min-heap for a min-priority queue. Heaps are great for implementing a priority queue because of the largest and smallest element at the root of the tree for a max-heap and a min-heap respectively. It is also used in scheduling processes for a computer, etc. Priority queues are used in many algorithms like Huffman Codes, Prim's algorithm, etc. Thus, a max-priority queue returns the element with maximum key first whereas, a min-priority queue returns the element with the smallest key first. d defaults to None.Priority queue is a type of queue in which every element has a key associated to it and the queue returns the element according to these keys, unlike the traditional queue which works on first come first serve basis.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |