473,657 Members | 2,586 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 10632
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******@REMOV Efall2005.CAPSf astmail.fm> wrote in message
news:49******** **********@news svr12.news.prod igy.com...
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******@REMOV Efall2005.CAPSf astmail.fm> wrote in message
news:49******** **********@news svr12.news.prod igy.com...
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
(technicall y 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
2895
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 circumstances in which the STL will move a vector, or invalidate iterators to elements in the vector, if you don't insert or remove elements? My actual problem seems to be as follows: I have class X, which contains an STL vector. The constructor...
14
2363
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. Unfortunately most of these tables don't have a key (and a key can only be introduced when the application programmers have changed their software). Tables have this structure
5
7005
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 memory, how the programs are stored in heap , what is the use of heap 3. what is pool memory otherwise memory pool, what is the use of memory pool 4. what is difference between stack and heap
43
6867
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 another story.) The data is written once and then read many times. Each primitive read requires unboxing. The data reads are critical to overall app performance. In the hopes of improving performance, we have tried to find a way to avoid the...
3
4642
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 >> 0x804d008 _Delete unknown block >> registered : 0x804d138 >> 0x804d008 _Delete unknown block >> 0x804d098 _Delete ok
669
25858
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 paper written on this subject. On the Expressive Power of Programming Languages, by Matthias Felleisen, 1990. http://www.ccs.neu.edu/home/cobbe/pl-seminar-jr/notes/2003-sep-26/expressive-slides.pdf
0
1832
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 because of memory allocation. In fact, my profiler says that 90% of the time in this sucker is memory allocation. Can anybody devise a way to cut down on the number of "news" in this? I need to insert a few million elements and remove a tenth of...
2
2280
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 class A is deleted, then would I need to manually delete the heap based object B? i.e. class A{ private: std::vector <B*> b public: void method1 ();
16
7705
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 using a sorted immutable balanced binary tree set implementation that provides the above operations each in O(log n) but it is extremely
0
8392
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, well explore What is ONU, What Is Router, ONU & Routers main usage, and What is the difference between ONU and Router. Lets take a closer look ! Part I. Meaning of...
0
8305
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8823
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
8730
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
8503
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
8605
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
4301
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2726
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
2
1950
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.