Hello, I was wondering if I could get some opinions on how best to
handle printing in a max heap data structure. Right now my heap
struct looks as thus:
typedef struct heapStruct {
int* heapArray;
int heapSize;
int arraySize;
int maxSize;
} heapStruct;
arraySize holds the current number of elements in the array.
maxSize is the total size of the array (so we know when to realloc).
And heapSize is the current size of the heap in the array.
heapsize <= arraySize <= maxSize.
I have a printHeap function that prints the elements in the heapArray
from 1 to heapSize. This works great if the elements in the array are
currently a heap. But I also have a heapSort function that sorts the
elements from smallest to greatest.
When this happens my array is no longer a max heap, and running
printHeap prints out just the first element in the array, since it is
the only element remaining in a heap from the heapSort process. It
seems to me that printHeap should be able to handle the scenario when
heapSort has been run, and the elements are sorted instead of in a
heap. However, the function's name printHeap seems to intuitively
state that you should only be calling this function when the client
has created a data structure that is indeed a heap. (ie after calling
insertHeap, which will insert while maintaining the heap properties).
Therefore if the client wants to print out the sorted array after
running heapSort, the ADT should have a separate printArray function
that prints heapArray's elements from 1 to arraySize. This is in fact
what I have right now, but having two print functions (printHeap and
printArray) seems non-portable style, and potentially confusing to the
client.
Maybe I could have a flag in the heap structure to keep track of when
the array is still a heap, and when I run heapSort that flag gets
turned off. Then printHeap could check for this flag, and if it is
off, print the entire array. However, this would mean that printHeap
is printing something that's not actually a heap, so I dont really
like it.
Is this just an issue of implementation? With proper documentation of
printHeap and printArray, would having the two aforedescribed print
functions be valid?
Thanks for your input,
mordac 2 7529
begin followup to mordac: Maybe I could have a flag in the heap structure to keep track of when the array is still a heap, and when I run heapSort that flag gets turned off. Then printHeap could check for this flag, and if it is off, print the entire array. However, this would mean that printHeap is printing something that's not actually a heap, so I dont really like it.
I take it that you are aiming for beauty, not strict performance.
In that case I recommend a function pointer.
typedef void (*pfnPrintHeap) (const heapStruct*);
Add one member to this heapStruct, lets say
pfnPrintHeap print;
Using the print facility then looks like this:
heapStruct heap;
/*
* do whatever to initialize and sort
*/
heap->print(heap);
The actual function name can then be hidden completely.
--
Für Google, Tux und GPL!
#include <stdio.h>
main()
{
for(;;)
{
printf ("Hello Max Heap Data Structure!\n");
}
} mo****@crackhea d.org (mordac) wrote in message news:<ca******* *************** ****@posting.go ogle.com>... Hello, I was wondering if I could get some opinions on how best to handle printing in a max heap data structure. Right now my heap struct looks as thus: typedef struct heapStruct { int* heapArray; int heapSize; int arraySize; int maxSize; } heapStruct; arraySize holds the current number of elements in the array. maxSize is the total size of the array (so we know when to realloc). And heapSize is the current size of the heap in the array. heapsize <= arraySize <= maxSize.
I have a printHeap function that prints the elements in the heapArray from 1 to heapSize. This works great if the elements in the array are currently a heap. But I also have a heapSort function that sorts the elements from smallest to greatest.
When this happens my array is no longer a max heap, and running printHeap prints out just the first element in the array, since it is the only element remaining in a heap from the heapSort process. It seems to me that printHeap should be able to handle the scenario when heapSort has been run, and the elements are sorted instead of in a heap. However, the function's name printHeap seems to intuitively state that you should only be calling this function when the client has created a data structure that is indeed a heap. (ie after calling insertHeap, which will insert while maintaining the heap properties).
Therefore if the client wants to print out the sorted array after running heapSort, the ADT should have a separate printArray function that prints heapArray's elements from 1 to arraySize. This is in fact what I have right now, but having two print functions (printHeap and printArray) seems non-portable style, and potentially confusing to the client.
Maybe I could have a flag in the heap structure to keep track of when the array is still a heap, and when I run heapSort that flag gets turned off. Then printHeap could check for this flag, and if it is off, print the entire array. However, this would mean that printHeap is printing something that's not actually a heap, so I dont really like it.
Is this just an issue of implementation? With proper documentation of printHeap and printArray, would having the two aforedescribed print functions be valid?
Thanks for your input,
mordac This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Kevin Grigorenko |
last post by:
Hello,
I couldn't find an obvious answer to this in the FAQ. My basic question,
is: Is there any difference in allocating on the heap versus the stack? If
heap or stack implementation is not part of the standard, then just
disregard this question. Here's some questions I'm confused about, and if
you can add anything else, please do so!
Is the stack limited for each program?
|
by: John Doe |
last post by:
Hi all,
I know the standard doesn't care about threads (wrongly :-)
But in current compilers implementation, is the "list" which holds count
of the occupied and free heap addresses SHARED among various threads or not?
I don't know if I was clear:
Case1: the list which holds count of what is free and what is occupied
is SHARED among all the threads. In this case each
|
by: Jonas Rundberg |
last post by:
Hi
I just started with c++ and I'm a little bit confused where stuff
go...
Assume we have a class:
class test {
private:
int arr;
};
|
by: gold |
last post by:
Hello all,
I want know abt wht kind of datastructures using both C & C++ internally.
Some were said heap, others said tree
anyone can explain brief?
|
by: Ook |
last post by:
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.
| |
by: arcticool |
last post by:
I had an interview today and I got destroyed :(
The question was why have a stack and a heap?
I could answer all the practical stuff like value types live on the
stack, enums are on the stack, as are structs, where classes are on
the heap... when value types go out of scope the memory is re-
allocated, object remain in memory waiting to be cleaned up by the
garbage collector, etc, but he responded 'so why not just put say a
class on the...
|
by: rnot |
last post by:
My code is working but I am not sure why.
Is it because of the copy constructor?
Is it becuase I still didn't define the distructor? (I did define an empty one)
I am adding a psaudo code:
//MainProg.h
//Global var
extern vector<EDRTSLot*> LotArr;
|
by: fdmfdmfdm |
last post by:
This is an interview question and I gave out my answer here, could you
please check for me?
Q. What are the memory allocation for static variable in a function,
an automatic variable and global variable?
My answer: static variable in function and global variable are
allocated in head, and automatic variable is allocated in stack.
Right?
|
by: wishbone34 |
last post by:
Hi, I have a question regarding the use of a couple functions I have for an assignment.. first here is the header file that im trying to use
//---------------------------------------------------------------------------------
struct DataType
{
int key;
string name;
};
class Heap
|
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...
|
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...
| |
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,...
|
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...
|
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...
|
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...
|
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
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
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...
| |