473,398 Members | 2,404 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,398 software developers and data experts.

Heap, how to insert?

2
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.
Nov 21 '08 #1
4 9552
r035198x
13,262 8TB
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.
Nov 21 '08 #2
vmpstr
63
Expand|Select|Wrap|Line Numbers
  1.     while(i<num)
  2.     {
  3.     newptr[i]=ptr[i];
  4.     i++;
  5.     }
  6.     num++;
  7.     ptr[num-1]=n;
  8.     ReheapUp(0,num-1);
  9.     }
  10.  
you want to add the following lines before num++

Expand|Select|Wrap|Line Numbers
  1. delete [] ptr;
  2. ptr = newptr;
  3.  
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.
Nov 21 '08 #3
weaknessforcats
9,208 Expert Mod 8TB
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.
Nov 21 '08 #4
ggoubb
2
Thanks so much vmpstr!
It works!!
Nov 21 '08 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

2
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*...
5
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...
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 >>...
1
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...
1
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)
1
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...
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...
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...
14
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
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,...
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
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
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...
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 project—planning, coding, testing,...
0
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...

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.