473,804 Members | 2,136 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Dynamic Priority Queue(heap) implementation

37 New Member
Hello everyone, here is my problem. I have to make a dynamic priority queue,which is a heap, and my book isn't helpful at all, so I have no clues on how to create it.. I read something like a static priority queue, which is an array heap[i], and for example i/2 is the father of i and so on..
the code of the static priority queue is this:

Expand|Select|Wrap|Line Numbers
  1. int i= ++CurrentSize;
  2. while(i != 1 && X > heap[i/2])  {
  3. // cannot put x in heap [i]
  4. heap[i] = heap[i/2];  //move element down
  5. i /= 2;  /move to parent
  6. }
  7. heap[i] = x;
  8.  
So I created a .h file:

Expand|Select|Wrap|Line Numbers
  1. #ifndef queuedef
  2. #define queuedef
  3.  
  4. class Queue {
  5.        public:
  6.               int a;
  7.               int i;
  8.               class Queue *next;
  9.               Queue();
  10.               void insert(int a);
  11.               void deletetop();
  12.               int gettop();
  13.               void destroy();
  14.               void show();
  15. };
  16. #endif
  17.  
in which "i" is like the "i" of the static priority queue.

Here is the code of the main file(only insert):

Expand|Select|Wrap|Line Numbers
  1. class Queue * gotofather(class Queue *temp1)
  2. {
  3.      class Queue *temp;
  4.      temp=top;
  5.      while((temp->i!=temp1->i/2)&&(temp->i!=1))
  6.               temp=temp->next;            
  7.      return temp;
  8. }
  9.  
  10. void Queue::insert(int data)
  11. {
  12.      class Queue *temp,*temp1;
  13.      int temp2;
  14.      temp=top;
  15.      top=(class Queue *) malloc(sizeof(class Queue));
  16.      top->a=data;
  17.      top->next=temp;
  18.      top->i=++CurrentSize;
  19.      temp=top;
  20.      temp1=gotofather(temp);
  21.      while((temp1->i!=1)&&(data>temp1->a))
  22.      {
  23.           temp->a=temp1->a;   //heap[i]=heap[i/2]
  24.           temp1=gotofather(temp1);   //i=i/2
  25.           temp=gotofather(temp);   //i=i/2
  26.      }
  27.      temp->a=data;//heap[i]=x
  28. }
  29.  
I know it's kind of hard to read the whole code, but please I would appreciate it if you did. My university is closed (due to strike) and we didn't have any lessons for the past 2 months. I thank anyone who will reply and give me at least some clues, if what I'm doing is right or totally wrong. Thanks in advance!

Kind regards, AlarV
Jan 14 '09
14 16977
donbock
2,426 Recognized Expert Top Contributor
Do you maybe mean to create the priority queue on the heap? That is, create it dynamically?
Well it's not really clear to me, but what I got from the lesson is that a priority queue could be a heap. That's why I wrote it like that. And yes I have to make it dynamic too.
I'm pretty sure that the word "dynamic" in "dynamic priority queue" refers to the priority values being dynamic not that the queue be kept in dynamic [heap] memory ... although it may well be malloc'ed.

The trick is that the heap is ordered by priority, so you have to rearrange it when a task priority value changes.
Jan 15 '09 #11
AlarV
37 New Member
Well due to what you said and the article from Wikipedia,that donbock suggested, I re-changed the "insert" and now it seems to work properly, here is the code:

Expand|Select|Wrap|Line Numbers
  1. class Queue * gotofather(class Queue *temp1)
  2. {
  3.      class Queue *temp;
  4.      temp=top;
  5.      while((temp->i!=temp1->i/2)&&(temp->i!=1))
  6.               temp=temp->next;            
  7.      return temp;
  8. }
  9.  
  10. void Queue::insert(int data)
  11. {
  12.      class Queue *temp,*temp1;
  13.      int temp2;
  14.      temp=top;
  15.      top=(class Queue *) malloc(sizeof(class Queue));
  16.      top->a=data;
  17.      top->next=temp;
  18.      top->i=++CurrentSize;
  19.      temp=top;
  20.      temp1=gotofather(temp);
  21.      while((temp1->i!=0)&&(data>temp1->a))
  22.      {
  23.           temp2=temp->a;
  24.           temp->a=temp1->a;
  25.           temp1->a=temp2;
  26.           temp1=gotofather(temp1);
  27.           temp=gotofather(temp);
  28.      }
  29. }
  30.  
It seems that it was easier than I thought. However I still have my doubts, as I created it all by myself, and the project might need some kinds of templates.

Should I have all the code in the main file or it could be somewhere else? For example could i have this code at an .h file?

Thanks everyone again, u've been very helpful!

Kind regards,
AlarV
Jan 15 '09 #12
AlarV
37 New Member
@donbock
What do u mean by the priority values being dynamic and not the whole queue? Could you explain it with an example?
Jan 15 '09 #13
JosAH
11,448 Recognized Expert MVP
@AlarV
Maybe you should read that article in the Java Howtos section. I wrote it and of course I didn't invent it. Basically you need one method/function: one that bubbles a number up in the heap in order to put it in place. That algorithm is described in detail in that article.

The check whether or not both children exist for a node is just a small coding burden,

kind regards,

Jos
Jan 16 '09 #14
donbock
2,426 Recognized Expert Top Contributor
@AlarV
When you add a node to the heap, you determine its position based on the priority value stored within it. There are a number of ways in which a task's priority can be altered. If the priority value of a node changes then the heap is no longer order properly -- you must rearrange it. I believe that this dynamicism is what is commonly meant by a "_dynamic_ priority queue".
Jan 16 '09 #15

Sign in to post your reply or Sign up for a free account.

Similar topics

0
1444
by: aaronwmail-usenet | last post by:
I've been wondering about benchmarks recently. What is a fair benchmark? How should benchmarks be vetted or judged? I decided to see what you folks thought, so for discussion I compared two priority queue implementations I published for Python in 1995 against the "heap" priority queue implementation added to the python distribution
9
4965
by: Tom | last post by:
What I mean is why can I only allocate const size stuff on the stack in C++? If I want to allocate a variable amount I need to use the OS API (Win32 in my case). Thanks, Tom.
4
3856
by: Henk | last post by:
Hi, I am new to the c-programming language and at the moment I am struggling with the following: I want to read a file using fread() and then put it in to memory. I want to use a (singel) linked list to continousely (dynamically) free up more memory when needed and put the buffers with the data of the file on a sort of stack. The problem is that i have no idea how to do this.
2
12315
by: soniya_batra1982 | last post by:
I have a problem in imploementation of circular queue through array in C language. Please send me the code of functions for insertion, deletion and display the elements of array. Plz send me quick reply if you know the solution.
1
3041
by: VanKha | last post by:
I write a queue program but it doesn't work properly:I enqueue an element and dequeue just after that, but the output is the first value and then many times of "Empty queue.."!Why ?Please help me. #include<iostream> #include<conio.h> using namespace std; class Node{ private: int value; Node * next; public:
3
1635
by: shublakhan | last post by:
How to implement queue in C++ using pointers.
11
5251
by: amolbehl | last post by:
Hi. Does anyone know how to implement queue in VB6.0 ? I just need the push function to push end of queue and pop to remove an item from top of the queue.
3
5231
by: Stuart Golodetz | last post by:
Hi guys, Just wondering what you'd recommend in terms of a C++ priority queue implementation that supports bubble-up and bubble-down operations please? std::priority_queue doesn't seem to support doing that, and I couldn't find an alternative in Boost. I could write my own, but that feels a little bit like reinventing the wheel, so I thought it was worth asking first. Cheers,
0
2206
by: tabassumpatil | last post by:
Please send the c code for: 1.STACK OPERATIONS : Transfer the names stored in stack s1 to stack s2 and print the contents of stack s2. 2.QUEUE OPERATIONS : Write a program to implement DOUBLE ENDED QUEUE operations a) Use names as data to perform all operations like enqueue,dequeue,print,etc. b)Use students records as data to perform the same operations. 3.FILE OPERATIONS: write c program to implement following FILE operations:...
0
10353
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10356
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10099
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9176
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6869
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5536
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5675
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4314
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
3003
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.