Question

#ifndef HEAP_H_ #define HEAP_H_ namespace cse { template<typename dtype> class heap { public: /** TO DO::...

#ifndef HEAP_H_
#define HEAP_H_



namespace cse {

    template<typename dtype>
    class heap {

    public:
        /** TO DO:: default constructor 
            perform new to reserve memory of size INITIAL_CAPACITY.
            set num_items = 0;
         */
        heap() {
            // write your code here
          
        };

        /** Create a heap from a given array */
        heap(dtype *other, size_t len) {
            // write your code here
        };

        /** copy constructor
            copy another heap to this heap.
         */
        heap(const heap<dtype> &other) {
            //write your code here
        };


        /** Accessor functions */

        /** returns the root of the heap */
        dtype &root();

        /** returns the left child of the given node. */
        size_t left(size_t index);


        /** returns the right child of the given node. */
        size_t right(size_t index);

        /** returns the parent of the given node. */
        size_t parent(size_t index);

        /** returns num_items of the array */
        size_t size();

        /** Core heap operations */

        /** perform heapify operation over the array. */
        void heapify(size_t index);

        /** create a heap from the given input. */
        void build_heap();

        /** heap sort
          create a copy darray to sorted_array.
          then perform heapsort over sorted_array.
          */
        void heapsort();


        /* insert items into heap */
        void insert(const dtype &key);

        /* remove items from the heap*/
        void remove();

        /** print heap **/
        void print(std::string name);


    private:
        static const size_t INITIAL_CAPACITY;
        size_t num_items;
        dtype *darray;
        dtype *sorted_array;


    }; //end of class heap


    template<typename dtype>
    const size_t heap<dtype>::INITIAL_CAPACITY = 100;
}
    /** TO DO : implement all the member functions here. 


} //end of namespace
     **/


#endif
0 0
Add a comment Improve this question Transcribed image text
Answer #1

#ifn def HEAP H #define HEAP H #include <vector> #include <stdexcept> using namespace std; templatetypename T> class Heap pub

if (v.size 0) throw runtime_error (Heap is empty) T removedElement = v[0]; v[0] = v[v . size ( ) -1]; v.pop backO int curre

template <typename T> void Heap<T>: :add(T element) v.push _back (element) int currentindex=v.size() - 1; while (currentIndex

Add a comment
Know the answer?
Add Answer to:
#ifndef HEAP_H_ #define HEAP_H_ namespace cse { template<typename dtype> class heap { public: /** TO DO::...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Given the following code: #ifndef TREE_H #define TREE_H #include <iostream> #include "TreeNode.h" template< typename NODETYPE > class Tree { public: Tree() : rootPtr( nullptr ) {}...

    Given the following code: #ifndef TREE_H #define TREE_H #include <iostream> #include "TreeNode.h" template< typename NODETYPE > class Tree { public: Tree() : rootPtr( nullptr ) {} void insertNode( const NODETYPE &value ) { insertNodeHelper( &rootPtr, value ); } void preOrderTraversal() const { preOrderHelper( rootPtr ); } void inOrderTraversal() const { inOrderHelper( rootPtr ); } private: TreeNode< NODETYPE > *rootPtr; void insertNodeHelper( TreeNode< NODETYPE > **ptr, const NODETYPE &value ) { if ( *ptr == nullptr ) * ptr = new...

  • Please show me how to overload the operators << and >> #ifndef LINK_LIST #define LINK_LIST #include...

    Please show me how to overload the operators << and >> #ifndef LINK_LIST #define LINK_LIST #include <iostream> using namespace std; template <typename T> struct Int_Node {    T value;    Int_Node<T> *pre, *next; }; template <typename T> class Link_List {    template <typename U>    friend ostream &operator<<(ostream &, const Link_List<U> &);// print all integers in the list    template <typename U>    friend istream &operator>>(istream &, Link_List<U> &);// input a value at the back of the list, like insert_node(val);...

  • #include <iostream> using namespace std; template <typename Item> class MyArray{ private:    Item *myarray;    int...

    #include <iostream> using namespace std; template <typename Item> class MyArray{ private:    Item *myarray;    int size;    int used;    void doubleSize(); public:    MyArray();    ~MyArray();    int length();    void insertHead(Item i);    void insertTail(Item i);    void deleteHead();    void deleteTail();    void sortAscending();    void sortDescending();    Item operator [](int i){        return myarray[i];    } }; template <typename Item> MyArray<Item>::MyArray(){    size = 5;    used = 0;    myarray = new Item[size];...

  • vector.h: #ifndef VECTOR_H #define VECTOR_H #include <algorithm> #include <iostream> #include <cassert> template <typename T> class Vector...

    vector.h: #ifndef VECTOR_H #define VECTOR_H #include <algorithm> #include <iostream> #include <cassert> template <typename T> class Vector {     public:         Vector( int initsize = 0 )         : theSize( initsize ),          theCapacity( initsize + SPARE_CAPACITY )         { objects = new T[ theCapacity ]; }         Vector( const Vector & rhs )         : theSize( rhs.theSize),          theCapacity( rhs.theCapacity ), objects( 0 )         {             objects = new T[ theCapacity ];             for( int k = 0; k < theSize; ++k)                 objects[ k ] = rhs.objects[ k...

  • #include<iostream> using namespace std; class TNode { public: int val; TNode(){} TNode(int v){val = v;} TNode...

    #include<iostream> using namespace std; class TNode { public: int val; TNode(){} TNode(int v){val = v;} TNode * left; TNode * right; TNode * parent; }; class BTree { public: //constructors and destructor BTree(); BTree(TNode *r);// initialize BTree with the root r. r is the only node. BTree(const BTree & t); // copy constructor BTree(const int *p, const int n);// similar to the copy constructor, but your input is of the array form. // input is given an array that denotes...

  • Implement the class MaxHeapPriorityQueue as a heap with the following operations: • MaxHeapPriorityQueue() creates a new...

    Implement the class MaxHeapPriorityQueue as a heap with the following operations: • MaxHeapPriorityQueue() creates a new heap that is empty. It needs no parameters and returns nothing. Note that as discussed in the video lecture, the index range of the array implementation of a heap is 1:n, NOT 0:n-1 • parent(index) returns the value of the parent of heap[index]. index is between 1 and the size of the heap. If index<=1 or index>size of the heap, it returns None •...

  • PROGRAM DESCRIPTION Using the given class definitions for either C++, create a minimum heap that stores...

    PROGRAM DESCRIPTION Using the given class definitions for either C++, create a minimum heap that stores integers and and implements a minimum priority queue. (Your program can be "hard coded" for integers - it does not need to use templates, generics, or polymorphism.) Your data structure must always store its internal data as a heap. Your toString function should return a string with the heap values as a comma separated list, also including the size of the heap as well....

  • // Header code for stack // requesting to create source code using C++ #ifndef Stack_h #define...

    // Header code for stack // requesting to create source code using C++ #ifndef Stack_h #define Stack_h #include <stdio.h> #include <string> #include <iostream> using namespace std; class Stack { public: Stack(); ~Stack(); bool empty(); string top(); void push(const string &val); void pop(); void display(ostream &out); private: class Node { public: string word; Node *next; }; Node *tos; }; #endif Header file provided above. PLS create source code for functions declared in the header. If changes are need no make in...

  • #ifndef STUDENTTESTSCORES_H #define STUDENTTESTSCORES_H #include <string> using namespace std; const double DEFAULT_SCORE = 0.0; class StudentTestScores...

    #ifndef STUDENTTESTSCORES_H #define STUDENTTESTSCORES_H #include <string> using namespace std; const double DEFAULT_SCORE = 0.0; class StudentTestScores { private: string studentName; // The student's name double *testScores; // Points to array of test scores int numTestScores; // Number of test scores // Private member function to create an // array of test scores. void createTestScoresArray(int size) { numTestScores = size; testScores = new double[size]; for (int i = 0; i < size; i++) testScores[i] = DEFAULT_SCORE; } public: // Constructor StudentTestScores(string...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT