473,378 Members | 1,549 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,378 software developers and data experts.

building a heap. problems with the void insert function

i need to write the code for the void insert into a heap. I have an error that fail compilation.
here is my code, can someone... help me out please
what should do

void insert (const KEY &key, const VALUE &value)
I wrote something like this:

Expand|Select|Wrap|Line Numbers
  1. void insert (const KEY &key, const VALUE &value) 
  2.  
  3. ************************************************************
  4. #pragma once
  5.  
  6. #include "UnderFlowException.h"
  7. #include "overFlowException.h"
  8. #include "Node.h"
  9.  
  10. template <typename KEY, typename VALUE>
  11. class Heap
  12. {
  13. private:
  14.     int maximumSize;
  15.     int lastUsedIndex;
  16.     Node<KEY,VALUE> *array;
  17.  
  18. public:
  19.     Heap (int maximumSize): maximumSize(maximumSize), lastUsedIndex(-1)
  20.     {
  21.         array = new Node<KEY,VALUE>[maximumSize];
  22.     }
  23.     //************************************************************
  24.     void insert (const KEY &key, const VALUE &value) 
  25.     // insert code  here
  26.     {
  27.         for (VALUE i = 0; i <maximumSize; i++)
  28.              KEY    current = key;
  29.               VALUE currentIndex = *array; 
  30.  
  31.         /*Node = new Node<KEY,VALUE>;
  32.         heap.push_back(unit);*/
  33.         //reheapUp(maximumSize);
  34.  
  35.  
  36.     }
  37.     //*****************************************************
  38.     VALUE remove ( )
  39.     {
  40.         VALUE result;
  41.  
  42.         if (!isEmpty())
  43.         {
  44.             result = array[0].value;
  45.             array[0] = array[lastUsedIndex--];
  46.             reheapDown();
  47.         }
  48.         else
  49.             throw UnderFlowException();
  50.  
  51.         return result;
  52.     }
  53.  
  54.     bool isEmpty()
  55.     {
  56.         return getSize() == 0;
  57.     }
  58.  
  59.     bool isFull()
  60.     {
  61.         return getSize() == getMaximumSize();
  62.     }
  63.  
  64.     int getSize()
  65.     {
  66.         return lastUsedIndex + 1;
  67.     }
  68.  
  69.     int getMaximumSize()
  70.     {
  71.         return maximumSize;
  72.     }
  73.  
  74. private:
  75.     static int parentIndexOf(int index)
  76.     {
  77.         return (index - 1) / 2;
  78.     }
  79.  
  80.     static int leftChildOf(int index)
  81.     {
  82.         return index * 2 + 1;
  83.     }
  84.  
  85.     static int rightChildOf(int index)
  86.     {
  87.         return index * 2 + 2;
  88.     }
  89.  
  90.     int largestChildOf (int index)
  91.     {
  92.         int result;
  93.         int rightChildIndex = rightChildOf(index);
  94.         int leftChildIndex = leftChildOf(index);
  95.  
  96.         if (rightChildIndex <= lastUsedIndex)
  97.         {
  98.             if (array[rightChildIndex].key > array[leftChildIndex].key)
  99.                 result = rightChildIndex;
  100.             else
  101.                 result = leftChildIndex;
  102.         }
  103.         else if (leftChildIndex <= lastUsedIndex)
  104.             result = leftChildIndex;
  105.         else
  106.             result = -1;
  107.  
  108.         return result;
  109.     }
  110.  
  111.     void swap (int i, int j)
  112.     {
  113.         Node<KEY,VALUE> temp = array[i];
  114.         array[i] = array[j];
  115.         array[j] = temp;
  116.     }
  117.     //*********************************************************
  118.     void reheapUp () // insert code here
  119.     {
  120.         int parent;
  121.         Node temp;
  122.  
  123.         if(bottom > root)
  124.         {
  125.             parent = (bottom - 1) / 2;
  126.             if(array[parent].getKey() < array[bottom].getKey())
  127.             {
  128.                 temp = array[parent];
  129.                 array[parent] = array[bottom];
  130.                 array[bottom] = temp;
  131.                 reheapUp(root, parent);
  132.             }
  133.         }
  134. //********************************************************************
  135.  
  136.     }
  137.  
  138.     void reheapDown ()
  139.     {
  140.         int current = 0;
  141.         int largestChildIndex = largestChildOf(current);
  142.         while (largestChildIndex > -1 && array[largestChildIndex].key > array[current].key)
  143.         {
  144.             swap(largestChildIndex,current);
  145.             current = largestChildIndex;
  146.             largestChildIndex = largestChildOf(current);
  147.         }
  148.     }
  149. };
Jun 30 '14 #1
7 1541
weaknessforcats
9,208 Expert Mod 8TB
You did not post your header files so I can't compile your code.
Jun 30 '14 #2
Expand|Select|Wrap|Line Numbers
  1. //This is the Node.h
  2. #pragma once
  3.  
  4. template <typename KEY, typename VALUE>
  5. class Node
  6. {
  7. public:
  8.     KEY key;
  9.     VALUE value;
  10.  
  11.     Node( )
  12.     {
  13.     }
  14.  
  15.     Node(const KEY &key, const VALUE &value): key(key), value(value)
  16.     {
  17.     }
  18. };
  19. //This is the overFlowException
  20.  
  21. #pragma once
  22.  
  23. #include <exception>
  24. using namespace std;
  25.  
  26. class overFlowException: public exception
  27. {
  28. };
  29.  
  30. //This is the UnderflowExceptio
  31. #pragma once
  32.  
  33. #include <exception>
  34. using namespace std;
  35.  
  36. class UnderFlowException: public exception
  37. {
  38. };
  39.  
  40. //this is the main.cpp
  41. #include <stdlib.h>
  42. #include <iostream>
  43. using namespace std;
  44.  
  45. #include "Heap.h"
  46.  
  47. int main()
  48. {
  49.     Heap<int,double> heap(30);
  50.  
  51.     for (int i = 0; i < heap.getMaximumSize(); i++)
  52.     {
  53.         int newValue = rand() % 100 + 1;
  54.         heap.insert(newValue,(double)newValue);
  55.     }
  56.  
  57.     try
  58.     {
  59.         heap.insert(0,0);
  60.         cout << "We should not be here!" << endl;
  61.     }
  62.     catch (overFlowException &)
  63.     {
  64.         cout << "Caught overflow exception" << endl;
  65.     }
  66.  
  67.     while (!heap.isEmpty())
  68.         cout << heap.remove() << endl;
  69.  
  70.     try
  71.     {
  72.         heap.remove();
  73.         cout << "We should not be here!" << endl;
  74.     }
  75.     catch (UnderFlowException &)
  76.     {
  77.         cout << "Caught underflow exception" << endl;
  78.     }
  79.  
  80.     system("pause");
  81.     return EXIT_SUCCESS;
  82. }
Jun 30 '14 #3
weaknessforcats
9,208 Expert Mod 8TB
I am still missing heap.h.
Jul 1 '14 #4
Expand|Select|Wrap|Line Numbers
  1. UnderflowException.h
  2.  
  3. #pragma once
  4.  
  5. #include <exception>
  6. using namespace std;
  7.  
  8. class UnderflowException: public exception
  9. {
  10. };
  11.  
  12.  
  13. OverflowException.h
  14. #pragma once
  15.  
  16. #include <exception>
  17. using namespace std;
  18.  
  19. class OverflowException: public exception
  20. {
  21. };
  22.  
  23.  
  24.  
  25. Node.h
  26. #pragma once
  27.  
  28. template <typename KEY, typename VALUE>
  29. class Node
  30. {
  31. public:
  32.     KEY key;
  33.     VALUE value;
  34.  
  35.     Node( )
  36.     {
  37.     }
  38.  
  39.     Node(const KEY &key, const VALUE &value): key(key), value(value)
  40.     {
  41.     }
  42. };
  43.  
  44.  
  45.  
  46.  
  47. Main.cpp
  48. #include <stdlib.h>
  49. #include <iostream>
  50. using namespace std;
  51.  
  52. #include "Heap.h"
  53.  
  54. int main()
  55. {
  56.     Heap<int,double> heap(30);
  57.  
  58.     for (int i = 0; i < heap.getMaximumSize(); i++)
  59.     {
  60.         int newValue = rand() % 100 + 1;
  61.         heap.insert(newValue,(double)newValue);
  62.     }
  63.  
  64.     try
  65.     {
  66.         heap.insert(0,0);
  67.         cout << "We should not be here!" << endl;
  68.     }
  69.     catch (OverflowException &)
  70.     {
  71.         cout << "Caught overflow exception" << endl;
  72.     }
  73.  
  74.     while (!heap.isEmpty())
  75.         cout << heap.remove() << endl;
  76.  
  77.     try
  78.     {
  79.         heap.remove();
  80.         cout << "We should not be here!" << endl;
  81.     }
  82.     catch (UnderflowException &)
  83.     {
  84.         cout << "Caught underflow exception" << endl;
  85.     }
  86.  
  87.     system("pause");
  88.     return EXIT_SUCCESS;
  89. }
  90.  
  91.  
  92.  
  93.  
  94.  
  95. Heap.h
  96. #pragma once
  97.  
  98. #include "UnderflowException.h"
  99. #include "OverflowException.h"
  100. #include "Node.h"
  101.  
  102. template <typename KEY, typename VALUE>
  103. class Heap
  104. {
  105. private:
  106.     int maximumSize;
  107.     int lastUsedIndex;
  108.     Node<KEY,VALUE> *array;
  109.  
  110. public:
  111.     Heap (int maximumSize): maximumSize(maximumSize), lastUsedIndex(-1)
  112.     {
  113.         array = new Node<KEY,VALUE>[maximumSize];
  114.     }
  115.  
  116.  
  117.  
  118.  
  119.     void insert (const KEY &key, const VALUE &value)
  120.     {
  121.  
  122.  
  123.  
  124.  
  125.  
  126.  
  127.     }
  128.  
  129.     VALUE remove ( )
  130.     {
  131.         VALUE result;
  132.  
  133.         if (!isEmpty())
  134.         {
  135.             result = array[0].value;
  136.             array[0] = array[lastUsedIndex--];
  137.             reheapDown();
  138.         }
  139.         else
  140.             throw UnderflowException();
  141.  
  142.         return result;
  143.     }
  144.  
  145.     bool isEmpty()
  146.     {
  147.         return getSize() == 0;
  148.     }
  149.  
  150.     bool isFull()
  151.     {
  152.         return getSize() == getMaximumSize();
  153.     }
  154.  
  155.     int getSize()
  156.     {
  157.         return lastUsedIndex + 1;
  158.     }
  159.  
  160.     int getMaximumSize()
  161.     {
  162.         return maximumSize;
  163.     }
  164.  
  165. private:
  166.     static int parentIndexOf(int index)
  167.     {
  168.         return (index - 1) / 2;
  169.     }
  170.  
  171.     static int leftChildOf(int index)
  172.     {
  173.         return index * 2 + 1;
  174.     }
  175.  
  176.     static int rightChildOf(int index)
  177.     {
  178.         return index * 2 + 2;
  179.     }
  180.  
  181.     int largestChildOf (int index)
  182.     {
  183.         int result;
  184.         int rightChildIndex = rightChildOf(index);
  185.         int leftChildIndex = leftChildOf(index);
  186.  
  187.         if (rightChildIndex <= lastUsedIndex)
  188.         {
  189.             if (array[rightChildIndex].key > array[leftChildIndex].key)
  190.                 result = rightChildIndex;
  191.             else
  192.                 result = leftChildIndex;
  193.         }
  194.         else if (leftChildIndex <= lastUsedIndex)
  195.             result = leftChildIndex;
  196.         else
  197.             result = -1;
  198.  
  199.         return result;
  200.     }
  201.  
  202.     void swap (int i, int j)
  203.     {
  204.         Node<KEY,VALUE> temp = array[i];
  205.         array[i] = array[j];
  206.         array[j] = temp;
  207.     }
  208.  
  209.  
  210.  
  211.  
  212.     void reheapUp ()
  213.     {
  214.  
  215.  
  216.  
  217.  
  218.  
  219.  
  220.  
  221.  
  222.     }
  223.  
  224.     void reheapDown ()
  225.     {
  226.         int current = 0;
  227.         int largestChildIndex = largestChildOf(current);
  228.         while (largestChildIndex > -1 && array[largestChildIndex].key > array[current].key)
  229.         {
  230.             swap(largestChildIndex,current);
  231.             current = largestChildIndex;
  232.             largestChildIndex = largestChildOf(current);
  233.         }
  234.     }
  235. };
Jul 1 '14 #5
weaknessforcats
9,208 Expert Mod 8TB
This code compiles and links for me using Visual Studio 2013.

What is the error you are getting?
Jul 1 '14 #6
yes, but i need to know what goes in the void insert
void insert (const KEY &key, const VALUE &value)
{
}
any help i will appreciate, thank you
Jul 1 '14 #7
it compiles but how can i set the whole insert function
Jul 1 '14 #8

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

Similar topics

3
by: jason | last post by:
How does one loop through the contents of a form complicated by dynamic construction of checkboxes which are assigned a 'model' and 'listingID' to the NAME field on the fly in this syntax:...
2
by: Xiangliang Meng | last post by:
Hi, all. As far as I know, the speical arithmetic operator on void pointer is an externsion in GNU CC. However, I could not find the relative topics in C99. Would someone like to help me find...
16
by: JCauble | last post by:
We have a large Asp.net application that is currently crashing our production servers. What we are seeing is the aspnet_wp eat up a bunch of memory and then stop unexpectedly. Does not recycle. ...
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...
14
by: Chris Ochs | last post by:
The documentation doesn't have any examples of using an sql language function to do an insert, andI am at loss as to I am doing wrong here. The error I get trying to create the function is: ERROR:...
1
by: Alexander Eisenhuth | last post by:
Hi, I'm near the ground and need help. I'm building a multithreaded extension with boost.python. python extension ----------------------------------------------------- import extension
6
by: choonmui | last post by:
hello.. i am new here and need some help in vb.net!! i need to do an insert function.. for example: there are some checkboxes. if the box is checked and a user clicks the button 'select', the item...
3
by: ANURAGBAJPAI | last post by:
Please tell me what is the purpose of void in function main.
4
by: ggoubb | last post by:
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...
1
by: monalikolhe | last post by:
can any1 plz tell me, waht is the address of private Heap created by HeapCreate() function?
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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?

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.