473,399 Members | 4,177 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,399 software developers and data experts.

What is Max Heap, how to insert

Ook
I'm not sure this is technically a c++ question, maybe there is a better ng
to ask, but I'll start here since I'm coding this in c++. What exactly is a
max heap, and more specifically, how do you inert into one? I've looked at
faq, several books, and but am not quite understanding that algorithm to
insert into max heap.
Nov 22 '05 #1
5 10610
Ook wrote:
I'm not sure this is technically a c++ question, maybe there is a better ng
to ask, but I'll start here since I'm coding this in c++. What exactly is a
max heap, and more specifically, how do you inert into one? I've looked at
faq, several books, and but am not quite understanding that algorithm to
insert into max heap.


A heap is an implementation of a priority queue (PQ) which is itself an
abstract data structure that supports operations including (supposing
that we are holding ints):

void push(int value); // inserts value into the PQ
int top(); // returns largest value in the PQ
void pop(); // removes largest value from the PQ

The standard library has a priority queue called priority_queue
(technically this is an adaptor because it builds priority_queue
behavior on top of an underlying container such as a vector).

http://www.sgi.com/tech/stl/priority_queue.html

That site has a description of the class as well as a short example
showing its usage.

Mark
Nov 22 '05 #2
Ook

"Mark P" <fa******@REMOVEfall2005.CAPSfastmail.fm> wrote in message
news:49******************@newssvr12.news.prodigy.c om...
Ook wrote:
I'm not sure this is technically a c++ question, maybe there is a better
ng to ask, but I'll start here since I'm coding this in c++. What exactly
is a max heap, and more specifically, how do you inert into one? I've
looked at faq, several books, and but am not quite understanding that
algorithm to insert into max heap.


A heap is an implementation of a priority queue (PQ) which is itself an
abstract data structure that supports operations including (supposing that
we are holding ints):

void push(int value); // inserts value into the PQ
int top(); // returns largest value in the PQ
void pop(); // removes largest value from the PQ

The standard library has a priority queue called priority_queue
(technically this is an adaptor because it builds priority_queue behavior
on top of an underlying container such as a vector).

http://www.sgi.com/tech/stl/priority_queue.html

That site has a description of the class as well as a short example
showing its usage.

Mark


That is helpfull, but what I'm really after is how the values are
represented in the heap - what order they are in, where they would appear,
etc. IE if I had a string of numbers, how can I tell how those numbers would
be represented in the tree?
Nov 22 '05 #3
Ook wrote:
"Mark P" <fa******@REMOVEfall2005.CAPSfastmail.fm> wrote in message
news:49******************@newssvr12.news.prodigy.c om...
Ook wrote:
I'm not sure this is technically a c++ question, maybe there is a better
ng to ask, but I'll start here since I'm coding this in c++. What exactly
is a max heap, and more specifically, how do you inert into one? I've
looked at faq, several books, and but am not quite understanding that
algorithm to insert into max heap.


A heap is an implementation of a priority queue (PQ) which is itself an
abstract data structure that supports operations including (supposing that
we are holding ints):

void push(int value); // inserts value into the PQ
int top(); // returns largest value in the PQ
void pop(); // removes largest value from the PQ

The standard library has a priority queue called priority_queue
(technically this is an adaptor because it builds priority_queue behavior
on top of an underlying container such as a vector).

http://www.sgi.com/tech/stl/priority_queue.html

That site has a description of the class as well as a short example
showing its usage.

Mark

That is helpfull, but what I'm really after is how the values are
represented in the heap - what order they are in, where they would appear,
etc. IE if I had a string of numbers, how can I tell how those numbers would
be represented in the tree?


That's entirely an implementation detail and in general there's no way
to know unless you know exactly how the code operates behind the scenes.
The "heap property" specifies that the root of the tree is the largest
element and, more generally, that every element in the tree (which
should be balanced, btw) is greater than all of it descendants.

For example, a heap with the numbers 1 2 3 4 5 could look like:

5
/ \
4 1
/ \
2 3

or,

5
/ \
4 3
/ \
2 1

or,

5
/ \
3 4
/ \
1 2

etc...
Nov 22 '05 #4
Ook

That's entirely an implementation detail and in general there's no way to
know unless you know exactly how the code operates behind the scenes. The
"heap property" specifies that the root of the tree is the largest element
and, more generally, that every element in the tree (which should be
balanced, btw) is greater than all of it descendants.

For example, a heap with the numbers 1 2 3 4 5 could look like:

5
/ \
4 1
/ \
2 3

or,

5
/ \
4 3
/ \
2 1

or,

5
/ \
3 4
/ \
1 2

etc...


Ahh..I think that is the key - that the root is the largest element. Would I
be correct to describe it like a BST, but not necessarily strictly sorted?
Nov 22 '05 #5
Ook wrote:
That's entirely an implementation detail and in general there's no way to
know unless you know exactly how the code operates behind the scenes. The
"heap property" specifies that the root of the tree is the largest element
and, more generally, that every element in the tree (which should be
balanced, btw) is greater than all of it descendants.

For example, a heap with the numbers 1 2 3 4 5 could look like:

5
/ \
4 1
/ \
2 3

or,

5
/ \
4 3
/ \
2 1

or,

5
/ \
3 4
/ \
1 2

etc...

Ahh..I think that is the key - that the root is the largest element. Would I
be correct to describe it like a BST, but not necessarily strictly sorted?


Well, sort of-- that's a bit contradictory since you can't really search
(the 'S' in BST) if it isn't sorted, but I think you get the idea. You
really ought to look up an implementation of a heap-- by only requiring
that each element is larger than its descendants it's much simpler to
maintain a balanced tree than a sorted balanced BST (e.g. a red-black tree).
Nov 22 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: Richard Thompson | last post by:
I've got a memory overwrite problem, and it looks as if a vector has been moved, even though I haven't inserted or deleted any elements in it. Is this possible? In other words, are there any...
14
by: uli2003wien | last post by:
Dear group, we are running a SQL-Server Database which is about 30 GB large. The purpose of this database is to contain periodic data from automatic devices which insert values into some tables....
5
by: bull | last post by:
hi could any one explain with example the following in a better way to understand 1. what is stack in memory, how the programs are stored in stack , what is the use of stack 2. What is heap in...
43
by: Mountain Bikn' Guy | last post by:
I have a situation where an app writes data of various types (primitives and objects) into a single dimensional array of objects. (This array eventually becomes a row in a data table, but that's...
3
by: silver360 | last post by:
Hello, I'm trying to create a basic Heap manager and i have some question about new/delete overloading. The following code give me this output : >> $./heap >> registered : 0x804d098 >>...
669
by: Xah Lee | last post by:
in March, i posted a essay “What is Expressiveness in a Computer Language”, archived at: http://xahlee.org/perl-python/what_is_expresiveness.html I was informed then that there is a academic...
0
by: not_a_commie | last post by:
Here is some code for the Soft Heap, taken directly from the original paper on the subject with a little help from manish_gupta. Unfortunately, it's about half as fast as a basic binary heap simply...
2
by: openuser | last post by:
Hi, I'm just wondering: when I have class A which owns a non-heap based vector which stores pointers to heap based object B (these objects are also owned by class A), and the object instance of the...
16
by: Jon Harrop | last post by:
I need a data structure with the following capabilities: 1. Fast access to the smallest element. 2. Fast insertion of a new element. 3. Fast deletion of an existing element. I have been...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
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,...
0
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...
0
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...
0
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 projectplanning, coding, testing,...

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.