building a heap. problems with the void insert function

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Fernanda
    New Member
    • Jun 2014
    • 5

    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:

    Code:
    void insert (const KEY &key, const VALUE &value) 
    
    ************************************************************
    #pragma once
    
    #include "UnderFlowException.h"
    #include "overFlowException.h"
    #include "Node.h"
    
    template <typename KEY, typename VALUE>
    class Heap
    {
    private:
    	int maximumSize;
    	int lastUsedIndex;
    	Node<KEY,VALUE> *array;
    
    public:
    	Heap (int maximumSize): maximumSize(maximumSize), lastUsedIndex(-1)
    	{
    		array = new Node<KEY,VALUE>[maximumSize];
    	}
    	//************************************************************
    	void insert (const KEY &key, const VALUE &value) 
    	// insert code  here
    	{
    		for (VALUE i = 0; i <maximumSize; i++)
    			 KEY	current = key;
    			  VALUE currentIndex = *array; 
    
    		/*Node = new Node<KEY,VALUE>;
    		heap.push_back(unit);*/
    		//reheapUp(maximumSize);
    
    
    	}
    	//*****************************************************
    	VALUE remove ( )
    	{
    		VALUE result;
    
    		if (!isEmpty())
    		{
    			result = array[0].value;
    			array[0] = array[lastUsedIndex--];
    			reheapDown();
    		}
    		else
    			throw UnderFlowException();
    
    		return result;
    	}
    
    	bool isEmpty()
    	{
    		return getSize() == 0;
    	}
    
    	bool isFull()
    	{
    		return getSize() == getMaximumSize();
    	}
    
    	int getSize()
    	{
    		return lastUsedIndex + 1;
    	}
    
    	int getMaximumSize()
    	{
    		return maximumSize;
    	}
    
    private:
    	static int parentIndexOf(int index)
    	{
    		return (index - 1) / 2;
    	}
    
    	static int leftChildOf(int index)
    	{
    		return index * 2 + 1;
    	}
    
    	static int rightChildOf(int index)
    	{
    		return index * 2 + 2;
    	}
    
    	int largestChildOf (int index)
    	{
    		int result;
    		int rightChildIndex = rightChildOf(index);
    		int leftChildIndex = leftChildOf(index);
    
    		if (rightChildIndex <= lastUsedIndex)
    		{
    			if (array[rightChildIndex].key > array[leftChildIndex].key)
    				result = rightChildIndex;
    			else
    				result = leftChildIndex;
    		}
    		else if (leftChildIndex <= lastUsedIndex)
    			result = leftChildIndex;
    		else
    			result = -1;
    
    		return result;
    	}
    
    	void swap (int i, int j)
    	{
    		Node<KEY,VALUE> temp = array[i];
    		array[i] = array[j];
    		array[j] = temp;
    	}
    	//*********************************************************
    	void reheapUp () // insert code here
    	{
    		int parent;
    		Node temp;
    
    		if(bottom > root)
    		{
    			parent = (bottom - 1) / 2;
    			if(array[parent].getKey() < array[bottom].getKey())
    			{
    				temp = array[parent];
    				array[parent] = array[bottom];
    				array[bottom] = temp;
    				reheapUp(root, parent);
    			}
    		}
    //********************************************************************
    
    	}
    
    	void reheapDown ()
    	{
    		int current = 0;
    		int largestChildIndex = largestChildOf(current);
    		while (largestChildIndex > -1 && array[largestChildIndex].key > array[current].key)
    		{
    			swap(largestChildIndex,current);
    			current = largestChildIndex;
    			largestChildIndex = largestChildOf(current);
    		}
    	}
    };
    Last edited by weaknessforcats; Jun 30 '14, 10:46 PM. Reason: added code tags
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    You did not post your header files so I can't compile your code.

    Comment

    • Fernanda
      New Member
      • Jun 2014
      • 5

      #3
      Code:
      //This is the Node.h
      #pragma once
      
      template <typename KEY, typename VALUE>
      class Node
      {
      public:
      	KEY key;
      	VALUE value;
      
      	Node( )
      	{
      	}
      
      	Node(const KEY &key, const VALUE &value): key(key), value(value)
      	{
      	}
      };
      //This is the overFlowException
      
      #pragma once
      
      #include <exception>
      using namespace std;
      
      class overFlowException: public exception
      {
      };
      
      //This is the UnderflowExceptio
      #pragma once
      
      #include <exception>
      using namespace std;
      
      class UnderFlowException: public exception
      {
      };
      
      //this is the main.cpp
      #include <stdlib.h>
      #include <iostream>
      using namespace std;
      
      #include "Heap.h"
      
      int main()
      {
      	Heap<int,double> heap(30);
      
      	for (int i = 0; i < heap.getMaximumSize(); i++)
      	{
      		int newValue = rand() % 100 + 1;
      		heap.insert(newValue,(double)newValue);
      	}
      
      	try
      	{
      		heap.insert(0,0);
      		cout << "We should not be here!" << endl;
      	}
      	catch (overFlowException &)
      	{
      		cout << "Caught overflow exception" << endl;
      	}
      
      	while (!heap.isEmpty())
      		cout << heap.remove() << endl;
      
      	try
      	{
      		heap.remove();
      		cout << "We should not be here!" << endl;
      	}
      	catch (UnderFlowException &)
      	{
      		cout << "Caught underflow exception" << endl;
      	}
      
      	system("pause");
      	return EXIT_SUCCESS;
      }
      Last edited by weaknessforcats; Jul 1 '14, 02:04 AM. Reason: added code tags

      Comment

      • weaknessforcats
        Recognized Expert Expert
        • Mar 2007
        • 9214

        #4
        I am still missing heap.h.

        Comment

        • Fernanda
          New Member
          • Jun 2014
          • 5

          #5
          building a heap

          Code:
          UnderflowException.h
          
          #pragma once
          
          #include <exception>
          using namespace std;
          
          class UnderflowException: public exception
          {
          };
          
          
          OverflowException.h
          #pragma once
          
          #include <exception>
          using namespace std;
          
          class OverflowException: public exception
          {
          };
          
          
          
          Node.h
          #pragma once
          
          template <typename KEY, typename VALUE>
          class Node
          {
          public:
          	KEY key;
          	VALUE value;
          
          	Node( )
          	{
          	}
          
          	Node(const KEY &key, const VALUE &value): key(key), value(value)
          	{
          	}
          };
          	
          
          
          
          Main.cpp
          #include <stdlib.h>
          #include <iostream>
          using namespace std;
          
          #include "Heap.h"
          
          int main()
          {
          	Heap<int,double> heap(30);
          
          	for (int i = 0; i < heap.getMaximumSize(); i++)
          	{
          		int newValue = rand() % 100 + 1;
          		heap.insert(newValue,(double)newValue);
          	}
          
          	try
          	{
          		heap.insert(0,0);
          		cout << "We should not be here!" << endl;
          	}
          	catch (OverflowException &)
          	{
          		cout << "Caught overflow exception" << endl;
          	}
          
          	while (!heap.isEmpty())
          		cout << heap.remove() << endl;
          
          	try
          	{
          		heap.remove();
          		cout << "We should not be here!" << endl;
          	}
          	catch (UnderflowException &)
          	{
          		cout << "Caught underflow exception" << endl;
          	}
          
          	system("pause");
          	return EXIT_SUCCESS;
          }
          
          
          
          
          
          Heap.h
          #pragma once
          
          #include "UnderflowException.h"
          #include "OverflowException.h"
          #include "Node.h"
          
          template <typename KEY, typename VALUE>
          class Heap
          {
          private:
          	int maximumSize;
          	int lastUsedIndex;
          	Node<KEY,VALUE> *array;
          
          public:
          	Heap (int maximumSize): maximumSize(maximumSize), lastUsedIndex(-1)
          	{
          		array = new Node<KEY,VALUE>[maximumSize];
          	}
          
          
          
          
          	void insert (const KEY &key, const VALUE &value)
          	{
          
          
          
          
          
          
          	}
          
          	VALUE remove ( )
          	{
          		VALUE result;
          
          		if (!isEmpty())
          		{
          			result = array[0].value;
          			array[0] = array[lastUsedIndex--];
          			reheapDown();
          		}
          		else
          			throw UnderflowException();
          
          		return result;
          	}
          
          	bool isEmpty()
          	{
          		return getSize() == 0;
          	}
          
          	bool isFull()
          	{
          		return getSize() == getMaximumSize();
          	}
          
          	int getSize()
          	{
          		return lastUsedIndex + 1;
          	}
          
          	int getMaximumSize()
          	{
          		return maximumSize;
          	}
          
          private:
          	static int parentIndexOf(int index)
          	{
          		return (index - 1) / 2;
          	}
          
          	static int leftChildOf(int index)
          	{
          		return index * 2 + 1;
          	}
          
          	static int rightChildOf(int index)
          	{
          		return index * 2 + 2;
          	}
          
          	int largestChildOf (int index)
          	{
          		int result;
          		int rightChildIndex = rightChildOf(index);
          		int leftChildIndex = leftChildOf(index);
          
          		if (rightChildIndex <= lastUsedIndex)
          		{
          			if (array[rightChildIndex].key > array[leftChildIndex].key)
          				result = rightChildIndex;
          			else
          				result = leftChildIndex;
          		}
          		else if (leftChildIndex <= lastUsedIndex)
          			result = leftChildIndex;
          		else
          			result = -1;
          
          		return result;
          	}
          
          	void swap (int i, int j)
          	{
          		Node<KEY,VALUE> temp = array[i];
          		array[i] = array[j];
          		array[j] = temp;
          	}
          
          
          
          
          	void reheapUp ()
          	{
          
          
          
          
          
          
          
          
          	}
          
          	void reheapDown ()
          	{
          		int current = 0;
          		int largestChildIndex = largestChildOf(current);
          		while (largestChildIndex > -1 && array[largestChildIndex].key > array[current].key)
          		{
          			swap(largestChildIndex,current);
          			current = largestChildIndex;
          			largestChildIndex = largestChildOf(current);
          		}
          	}
          };

          Comment

          • weaknessforcats
            Recognized Expert Expert
            • Mar 2007
            • 9214

            #6
            This code compiles and links for me using Visual Studio 2013.

            What is the error you are getting?

            Comment

            • Fernanda
              New Member
              • Jun 2014
              • 5

              #7
              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

              Comment

              • Fernanda
                New Member
                • Jun 2014
                • 5

                #8
                it compiles but how can i set the whole insert function

                Comment

                Working...