I have a working array class that I am trying to base a (Binary tree) heap class on. I would like to know the code for (or at least get some direction on) the following code for the heap class: the copy constructor, the upheap, and the downheap. I have everything else I need. Let me mention that although this is for homework, it is more for self study.
Hai jdixon !
I am having 2 solutions for creating HEAP with different definitions.
I SOLUTION :
------------
HEAP : A heap is a complete binary tree with the property that the value at each node
I'm not familar with this terminology. By binary tree heap class, do you mean a class for implementing a heap that uses a binary tree? Or do you mean a binary tree class that uses the heap to store the nodes?
What I mean is a class for implementing a heap using a binary tree. The binary tree is represented using a dynamic array. If you like, I can give you the code for the array class.
I'm afraid I'm more curious than helpful in this area. It seems strange to me that you want to impliment a heap as a class. I can't image what the advantage would be. For example, you want code for a copy constructor. What should the copy constructor do? It shouldn't copy the data in the heap, there would be no points to the data so it would never get freed. I gues the copy constructor could create a new heap of the same size as the source heap, but with no objects allocated in it? Is that what you were planning?
Sorry Nietod, I guess this query is more dicey than I thought.
I think my instructor wanted to use this excerise to show us how dynamic arrays and trees can be related.
The only thing that I can think of for the copy constructor is that it will allocate memory for a heap that will be the same size as the source heap. After the memory has been allocated the data would be copied into the newly allocated array, probably with a small loop. The destructor (I guess) would release that dynamic memory.
Like I said, I can give you the .h and .cpp files that I have, but if this is getting too sticky I would understand because believe me I¹m more confused than you are.
0
More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.
The copy constructor could copy the data from the source heap. But what would be the point? The data in the source heap is being "used" in the sense that whatever created the data (requested it from the heap) has pointers to the data and can use the data. but if you create a copy of the data, no one has pointers to the copies so no one can use the data. Its kinda like
char *StrPtr1 = new char[100]; // this is useful.
new char[100]; // This is useless. I can't access the data created!
Are you sure you want a copy constructor? My intuition would be that the class should declare a private copy constructor and not implement it. This technique prevents the class from being copied. This makes sense to me. It yould allow you to declare the heap class objects, but could not pass them by value.
I'd be happy to look at the code, but I'm going away today. I'll be back next tuesday. hopefully you'll get more help before then.
I am having 2 solutions for creating HEAP with different definitions.
I SOLUTION :
------------
HEAP : A heap is a complete binary tree with the property that the value at each node is at least
as large as its children (if they exist).
#include <iostream.h>
const int MAX = 80;
/* If you want you can include this in Class object also */
void heap(int heap_array[],int n);
void main(void)
{
int arr[MAX],i,j,n;
/* First you store all the elements in the ARRAY
whatever order it is */
cout << "Enter the no. of elements in the array : ";
cin >> n;
for (i=0;i<n;i++)
{
cout<<"Enter the element "<<i<<" : ";
cin >> arr[i];
}
/* Assuming that 1st element in the array is heaped
and from 2 element onwards this will place in a proper position */
if (n > 0) // If there is more than one record then only heap is required
for (i=1; i<n; i++)
heap(arr,i);
}
void heap(heap_array[],n) /* Heap Creation by inserting one item at a time
Inserts the value in heap_array[n] into the heap which
is stored at heap_array[1] to heap_array[n-1] */
{
int i,j,k;
j = n;
i = n/2;
item = heap_array[n];
while (i>=0 && heap_array[i] < item)
{
heap_array[j] = heap_array[i]; /* Move the parent down */
j = i;
i /= 2; /* Parent of heap i is at i/2 */
}
heap_array[j] = item; /* A place for heap_array[n] is found */
}
II SOLUTION
This is another algorithm for creating a heap which has the nice property that its worst
case time is an order of magnitude faster than n-1 calls of above program.
#include <iostream.h>
const int MAX = 80;
/* If you want you can include this in Class object also */
/* First you store all the elements in the ARRAY
whatever order it is */
cout << "Enter the no. of elements in the array : ";
cin >> n;
for (i=0;i<n;i++)
{
cout<<"Enter the element "<<i<<" : ";
cin >> arr[i];
}
heapify(arr,n);
}
void heapify(int heap_array[],int n)
{
/* Readjust the elements in heap_array(0:n-1) to form a heap */
int i,j;
j = n/2;
for (i=j;i>=0;i--)
adjust(heap_array,i,n);
}
void adjust(int heap_array[],int i, int n)
{
/* The complete binary trees with roots heap(2*i) and heap(2*i+1) are
combined with heap(i) to form a single heap, 0<= i < n
No node has an address >= n or less than 0 */
int j,item,k;
j = 2*i;
item = heap_array[i];
while ( j < n)
{
if ( j<(n-1) && heap_array[j] < heap_array[j+1] ) /* Compare left and right childs */
j++;
if (item >= heap_array[j])
return; /* item has been already in heap order */
else
{
k = j/2;
heap_array[k] = item;
}
}
k = j/2;
heap_array[k] = item;
return;
}
I hope these solutions are more than sufficient. If you have any problem, you are
welcome to contact me either EE or through my E-Mail sganta@ch.oracle.com
Yes, yes, yes. You hit it on the head. Thanks a lot.
0
Featured Post
More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.
Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples. You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.