473,796 Members | 2,515 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 #1
14 16975
Tassos Souris
152 New Member
If you had a strike for 2 months then you are probably loosing the semester so do not bother making the project :P:P:P
Jan 14 '09 #2
AlarV
37 New Member
@Tassos Souris
hahaha! ellhnas eisai? Well not actually 2 months but 1,5 so we'll see...
Jan 14 '09 #3
donbock
2,426 Recognized Expert Top Contributor
Please explain what a dynamic priority queue(heap) is. If priority is dynamic, then what events can cause it to change?

I believe the meaning of this term is open to interpretation, so I would like to hear what you need it to be.
Jan 14 '09 #4
AlarV
37 New Member
@donbock
Well I need it to be a max heap.

Like this:
Expand|Select|Wrap|Line Numbers
  1.  
  2.        36
  3.        /  \
  4.       4   5
  5.      / \ 
  6.     2  1
  7.  
here for example 4 is the father of 2,1. and 36 is the father of 4,5

It's not really clear to me too, on how a heap's priority is assigned. In my book it says that you check (each time when you insert one number) the father, and then the father of the father and so on..
Jan 14 '09 #5
donbock
2,426 Recognized Expert Top Contributor
@AlarV
This looks like a "binary heap" sorted by priority. A binary heap has two basic explicit operations and several implicit operations:
> explicit: add a node to the heap;
> explicit: remove the root node from the heap;
> implicit: create the heap;
> implicit: destroy the heap;
> implicit: create a heap node;
> implicit: destroy a heap node.

To make this a _dynamic_ priority heap you need to add one more explicit operation:
> explicit: change the priority of a node in the heap (and rearrange the heap accordingly).
Jan 14 '09 #6
weaknessforcats
9,208 Recognized Expert Moderator Expert
If memory serves, a priority queue is a data structure where values are inserted randomly but when extracted reppear in descending sequence. That is, a fetch from the priority queue always returns the element with the highest value.

Determination of the highest value is usually left to a callback function so the values can be returned in the order specified in the callback function.

Usual implementations accomplish this by keeping the containter sorted at all times so the highest value is always at the beginning of the queue.

Now a Heap is a data structure constructed such that the number of levels between the root and the farthest leaves is constant.

So I am having trouble digesting the phrase "priority queue that is a heap".

Do you maybe mean to create the priority queue on the heap? That is, create it dynamically?

But the thing that really bugs me is that you are using C++ but are not using the already written priority_queue template in the C++ Standard Library. Why is that? Are you taking a class in archaeological computer science?
Jan 15 '09 #7
JosAH
11,448 Recognized Expert MVP
@weaknessforcats
That is not true; usual implementations keep the heap a heap, i.e. for a parent P and two childs L and R, the relation P >= L && P >= R is true. (both L and R may not exist of course).

In the 'Generic Heap Sort' article in the Java Howtos that algorithm is described in detail. Keeping the entire thing sorted at all times is overkill.

kind regards,

Jos
Jan 15 '09 #8
donbock
2,426 Recognized Expert Top Contributor
@weaknessforcats
One source of further information is Wikipedia: "binary heap" and "priority queue".
Jan 15 '09 #9
AlarV
37 New Member
First of all I'd like to thank anyone, who tried to solve my problem, I appreciate it!
To answer to some of you
@weaknessforcats
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.

Well the "priority_q ueue template" is something that I don't know, (or I could say not have been taught yet, due to the strikes of the past 2 months).

Generally what I have to do is a project that contains some kinds of data structures,that we create by ourselves.

@JosAH: So what you are saying is that I should have 2 variables for each clue of the heap, which are the children of it(even if they don't exist), and check the max each time. This sounds right but the implementation might
not be as easy as it seems... I agree that "Keeping the entire thing sorted at all times is overkill." and that's why I came here :D. I already did that(having the whole heap sorted) and I could say that it is easy.
Jan 15 '09 #10

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
12314
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
3039
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
5250
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
2203
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
9683
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10457
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10231
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...
0
10013
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
9054
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...
1
7550
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6792
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
5576
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3733
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.