Priority Queue (HEAP) Priority Queue

Source: Internet
Author: User

Priority Queue (HEAP) Priority Queue
Priority Queue


Definition & Description:

In computer science/data structures, a priority queue is an abstract data type which is like a regular queue or stack data structure, but where additionally each element has a "priority" associated with it. in a priority queue, an element with high priority is served before an element with low priority. if two elements have the same priority, they are served according to their order in the queue


Similarly, in a multiuser environment, the operating system schedust must decide which of several processes to run. Generally a process is only allowed to run for a fixed period of time. One algorithm uses
Queue. jobs are initially placed at the end of the queue. the scheduler will repeatedly take the first job on the queue, run it until either it finishes or its time limit is up, and place it at the end of the queue if it does not finish. this strategy is generally not appropriate, because very short jobs will seem to take a long time because of the wait involved to run. generally, it is important that short jobs finish as fast as possible, so these jobs shoshould have preference over jobs that have already been running. furthermore, some jobs that are not short are still very important and shoshould also have preference.


This participating application seems to require a special kind of queue, known as a priority queue.


Certificate ---------------------------------------------------------------------------------------------------------------------------------------------


First, let's take a look at what files are implemented:



PriZ success? Http://www.bkjia.com/kf/ware/vc/ "target =" _ blank "class =" keylink "> vcml0eV9xdWV1ZS5oPC9wPgo8cD48cHJlIGNsYXNzPQ =" brush: java; "> /************************************* * ******************** code writer: EOFcode date: 2014.09.19code file: priority_queue.he-mail: jasonleaster@gmail.comcode purpose: Just a header file for my implementation. what you shoshould know is that you may callinit_heap () when you want to create a priority queue. and then you cocould use build_heap () to create that queuewith your source -- @ array. **************************************** * ****************/# ifndef _ PRIORITY_QUEUE_H # define _ PRIORITY_QUEUE_H # include # Include # Define EMPTY_HEAP-1 # define NOEMPTY_HEAP 0 # define FULL_HEAP 1 # define NOFULL_HEAP 0 struct heap {int capacity; int size;/*** ATTENTION! Just a little trick. */int element [0];}; struct heap * init_heap (int max_size); void destroy_heap (struct heap * p_heap); void insert_heap (struct heap * p_heap, int element ); int delete_heap (struct heap * p_heap); void build_heap (struct heap * p_heap, int * array, int size); void precolate_down (struct heap * p_heap, int parent ); int is_empty (struct heap * p_heap); int is_full (struct heap * p_heap); void print_heap (struct heap * p_heap); # endif


Build_heap.c

/*********************************************************code writer:EOFcode date:2014.09.19code file:build_heap.ce-mail:jasonleaster@gmail.comcode purpose:@p_heap : A pointer which point to our heap.@array  : A pointer which point to our input-data  which we want to fill heap with it.@size: The size of our array.*********************************************************/#include "priority_queue.h"void build_heap(struct heap* p_heap,int* array,int size){if(!p_heap || !array){printf("In function: %s() p_heap: %p array: %p\n",__FUNCTION__,p_heap,array);return ;}p_heap->size = size;int index  = 0;int tmp   = 0;int foo    = 0;int min   = 0;int parent = 0;int right_child = 0;int left_child  = 0;for(index = 0; index < size ;index++){p_heap->element[index+1] = array[index];}for(parent = size/2; parent > 0; parent--){precolate_down(p_heap,parent);}}

Delete_heap.c

/*********************************************************code writer:EOFcode date:2014.09.19code file:delete_heap.ce-mail:jasonleaster@gmail.com*********************************************************/#include "priority_queue.h"int delete_heap(struct heap* p_heap){if(!p_heap){printf("malloc failed in function %s()!\n",__FUNCTION__);return -1;}/***You have to know that heap data get start from** element[1] which is just for simplicty to implement** heap in array.*/int min_element  = p_heap->element[1];int last_element = p_heap->element[p_heap->size--];int me    = 0;int child = 0;for(me = 1;me*2 < p_heap->size; me = child){child = me*2;/***Make sure that child index into the smaller one** between the right child and the left child.*/if(child != p_heap->size && p_heap->element[child+1]  < p_heap->element[child]){child++;}if(last_element > p_heap->element[child]){p_heap->element[me] = p_heap->element[child];}else{break;}}p_heap->element[me] = last_element;return min_element;}

Destroy_heap.c

/*********************************************************code writer:EOFcode date:2014.09.19code file:destroy_heap.ce-mail:jasonleaster@gmail.comcode description:**NEVER** forget to call this function to free outmemory which p_heap point to.*********************************************************/#include "priority_queue.h"void destroy_heap(struct heap* p_heap){if(!p_heap){printf("malloc failed in function %s()!\n",__FUNCTION__);return ;}free(p_heap);}

Init_heap.c

/*********************************************************code writer:EOFcode date:2014.09.19code file:init_heap.ce-mail:jasonleaster@gmail.comcode purpose:@max_size : what's the size of the heap that you    want to create it ?This function would return a pointer which point to the heap that you created.*********************************************************/#include "priority_queue.h"struct heap* init_heap(int max_size){if(max_size < 0){return NULL;}struct heap* p_heap = NULL;p_heap = (struct heap*)malloc(sizeof(int)*(max_size+1) + sizeof(struct heap));if(!p_heap){printf("malloc failed in function %s()!\n",__FUNCTION__);return NULL;}p_heap->capacity = max_size;p_heap->size  = 0;return p_heap;}


Insert_heap.c
/*********************************************************code writer:EOFcode date:2014.09.19code file:insert_heap.ce-mail:jasonleaster@gmail.comcode purpose:Just call this function and insert @element intoheap that @p_heap point to.*********************************************************/#include "priority_queue.h"void insert_heap(struct heap* p_heap,int element){if(!p_heap){printf("p_heap is NULL in function %s\n",__FUNCTION__);return ;}int child = 0;/***Percolate up*/for(child = ++p_heap->size; p_heap->element[child/2] > element; child/=2){p_heap->element[child] = p_heap->element[child/2];}p_heap->element[child] = element;}


Is_empty.c

/*********************************************************code writer:EOFcode date:2014.09.19code file:is_empty.ce-mail:jasonleaster@gmail.com*********************************************************/#include "priority_queue.h"int is_empty(struct heap* p_heap){if(!p_heap){printf("p_heap is NULL in function %s()\n",__FUNCTION__);return -1;}if(!p_heap->size){return EMPTY_HEAP;}return NOEMPTY_HEAP;}

Is_full.c

/*********************************************************code writer:EOFcode date:2014.09.19code file:is_full.ce-mail:jasonleaster@gmail.com*********************************************************/#include "priority_queue.h"int is_full(struct heap* p_heap){if(!p_heap){printf("p_heap is NULL in function %s()\n",__FUNCTION__);return -1;}if(p_heap->size >= p_heap->capacity){return FULL_HEAP;}return NOFULL_HEAP;}

Precolate_down.c

/*********************************************************code writer:EOFcode date:2014.09.19code file:precolate_down.ce-mail:jasonleaster@gmail.comcode purpose:This is a implementation of precolate_down() whichuse recursion.*********************************************************/#include "priority_queue.h"void precolate_down(struct heap* p_heap,int parent){/***If we got the last parent, precolate_down() end.*/if(2*parent > p_heap->size){return;}int tmp = 0;int foo = 0;int min = 0;int right_child = 0;int left_child= 0;tmp = p_heap->element[parent];foo         = p_heap->element[parent];left_child  = p_heap->element[2*parent];/***If we got the last parent and the size of** heap is even. This means that the child of the ** last parent is just only one.Here is the method** to process this situation.*/if(p_heap->size %2 == 0 && 2*parent == p_heap->size){if(foo > min){tmp = p_heap->element[parent];p_heap->element[parent] = p_heap->element[2*parent];p_heap->element[2*parent] = tmp;}return ;}/*** If parent have two child.*/right_child = p_heap->element[2*parent+1];min = left_child < right_child? left_child : right_child;if(foo > min){if(right_child > left_child){tmp = p_heap->element[parent];p_heap->element[parent] = p_heap->element[2*parent];p_heap->element[2*parent] = tmp;precolate_down(p_heap,2*parent);}else{tmp = p_heap->element[parent];p_heap->element[parent] = p_heap->element[2*parent+1];p_heap->element[2*parent+1] = tmp;precolate_down(p_heap,2*parent+1);}}}



Print_heap.c

/*************************************** * ****************** Code writer: EOFcode date: 2014.09.19code file: print_heap.ce-mail: jasonleaster@gmail.comcode description: Just print out the current heap that @ p_heap pointto. **************************************** * ***************/# include "priority_queue.h" void print_heap (struct heap * p_heap) {if (! P_heap) {printf ("You passed NULL in function % s () \ n" ,__ FUNCTION _); return;} printf ("The capacity of heap: % d \ n ", p_heap-> capacity); printf (" The size of heap: % d \ n ", p_heap-> size); int index = 0; int tmp = 1; for (index = 0, tmp = 2; index <p_heap-> size; index ++) {printf ("% d ", p_heap-> element [index + 1]); if (index = 0) {printf ("\ n");} else if (index = tmp) {tmp + = 1 <
 
  
Test application:

Priority_queue_test.c

/*********************************************************code writer:EOFcode date:2014.09.19code file:priority_queue_test.ce-mail:jasonleaster@gmail.comcode description:This code would help us to test our implementationof heap.*********************************************************/#include "priority_queue.h"int main(){int array[] = {10,12,1,14,6,5,8,15,3,9,7,4,11,13,2};int size = sizeof(array)/sizeof(array[0]);struct heap* p_heap = NULL;p_heap = init_heap(size);if(!p_heap){printf("Inintialize failed!\n");return 0;}build_heap(p_heap,array,size);print_heap(p_heap);delete_heap(p_heap);print_heap(p_heap);insert_heap(p_heap,43);print_heap(p_heap);destroy_heap(p_heap);return 0;}


Test results:









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.