The purpose of the Insert function is to add a new integer in the Heap assuming that it is not already full. If Heap capacity has been reached, it attempts to double the current capacity. If capacity cannot be doubled, it throws FullHeap.
Here is the Heap.h file
const int MAXSIZE = 4; // Default maximum heap size
class Heap // Smart Heap ADT as an array
{
private:
int* ptr; // Pointer to smart heap array
int maxsize; // Current maximum heap size
int num; // Current number of elements in heap
void Swap(int& a, int& b); // Swaps integers a and b
public:
Heap(); // Creates empty smart heap of MAXSIZE
~Heap();
void ReheapDown(int root, int bottom); // Repairs heap order property from root to leaf
void ReheapUp(int root, int bottom); // Repairs heap order property from leaf to root
bool IsFull();
bool IsEmpty();
void Insert(int n); // Inserts n into heap, doubling heap capacity if needed.
int DeleteMax(); // Deletes and returns max value from heap if heap not empty
void MakeEmpty(); // Makes heap empty
void Print(); // Write heaparray to stdout
int Size(); // Returns current number of element in heap
int Capacity(); // Returns current heap capacity
};
I'm not quite sure how to double the heap capacity.
Here is what I have for the Insert function.
void Heap::Insert(int n)
{
if (num==maxsize)
{
maxsize=maxsize*2;
int* newptr;
int i=0;
newptr=new int[maxsize];
while(i<num)
{
newptr[i]=ptr[i];
i++;
}
num++;
ptr[num-1]=n;
ReheapUp(0,num-1);
}
else
{
num++;
ptr[num-1] = n;
ReheapUp(0,num-1);
}
}
I got a segmentation fault here.
4 9552
An easy way to learn is to use cout statements in your code to find what the values of your variables are against what you are expecting them to be.
-
while(i<num)
-
{
-
newptr[i]=ptr[i];
-
i++;
-
}
-
num++;
-
ptr[num-1]=n;
-
ReheapUp(0,num-1);
-
}
-
you want to add the following lines before num++ -
delete [] ptr;
-
ptr = newptr;
-
The reason you are getting a segfault is that you are trying to add an element to your old ptr, which is still of old size. newptr is of doubled size, but you are not using it yet. If you add the lines, the doubled space, address of which is stored in newptr, gets assigned to ptr.
How come your heap is an array?
I would have expected you would have a node containing the allocation information and that node would be in a heap data structure.
Thanks so much vmpstr!
It works!!
Sign in to post your reply or Sign up for a free account.
Similar topics
by: mordac |
last post by:
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*...
|
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...
|
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
>>...
|
by: Chris Mullins |
last post by:
We've been using the SSLStream class found in System.Net.Security to build a
giant Sockets server that provides TLS encryption at the channel leve.
Before .Net 2.0, we used an open-source...
|
by: serge |
last post by:
IF (SELECT OBJECT_ID('t1')) IS NOT NULL
DROP TABLE t1
GO
CREATE TABLE t1 (c1 INT, c2 INT)
DECLARE @n INT
SET @n = 1
WHILE @n <= 454
BEGIN
INSERT INTO t1 VALUES (@n, @n)
|
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...
|
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...
|
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...
|
by: AlarV |
last post by:
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...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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,...
|
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...
|
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...
|
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...
|
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,...
|
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...
| | |