Question

Write Binary Heap program in C++. And Insert E,H,I,D,G,F,A,B,C. Then, use reheapup algorithm and print out...

Write Binary Heap program in C++. And Insert E,H,I,D,G,F,A,B,C. Then, use reheapup algorithm and print out the result.

Need to use this algorithm.

ReheapUp ( int root, int bottom )

// Pre: bottom is the index of the node that may violate the heap

// order property. The order property is satisfied from root to

// next-to-last node.

// Post: Heap order property is restored between root and bottom

{

    int parent ;

    if ( bottom > root )

    {

parent = ( bottom - 1 ) / 2;

if ( elements [ parent ] < elements [ bottom ] )

{

   Swap ( elements [ parent ], elements [ bottom ] );

   ReheapUp ( root, parent );

}

    }

}

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

#include <iostream>
#include <cstdlib>
#include <vector>
#include <iterator>
using namespace std;

void Swap(char &x, char &y)
{
   char tmp = x;
   x = y;
   y = tmp;
}

// Inside the class ReheapUp is the same function as the ReheapUp.
// I know it looks different but you can check that they are performing the same operation.


/*
* Class for BinaryHeap
*/
class BinaryHeap
{
private:
  
int left(int parent);
int right(int parent);
int parent(int child);
void ReheapUp(int index);
public:
       vector <char> heap;
BinaryHeap(){}
void Insert(char data);
void DisplayHeap();
int Size();
};

int BinaryHeap::Size()
{
/**
Returns size of the heap
*/
return heap.size();
}

void BinaryHeap::Insert(char data)
{
/*
* Function to Insert Element into the Heap
*/
heap.push_back(data);
   ReheapUp(heap.size() -1);
}

/*
* Display Heap
*/
void BinaryHeap::DisplayHeap()
{
vector <char>::iterator pos = heap.begin();
cout<<"Heap --> ";
while (pos != heap.end())
{
cout<<*pos<<" ";
pos++;
}
cout<<endl;
}

int BinaryHeap::left(int parent)
{
int l = 2 * parent + 1;
if (l < heap.size())
return l;
else
return -1;
}

/*
* Return Right Child
*/
int BinaryHeap::right(int parent)
{
int r = 2 * parent + 2;
if (r < heap.size())
return r;
else
return -1;
}

int BinaryHeap::parent(int child)
{
/*
* Return Parent
*/
int p = (child - 1)/2;
if (child == 0)
return -1;
else
return p;
}

void BinaryHeap::ReheapUp(int in)
{
if (in >= 0 && parent(in) >= 0 && heap[parent(in)] < heap[in])
{
char tmp = heap[in];
heap[in] = heap[parent(in)];
heap[parent(in)] = tmp;
ReheapUp(parent(in));
}
}


int main()
{
BinaryHeap h;
while (1)
{
  
cout<<"1.Insert Element"<<endl;
cout<<"2.Print Heap"<<endl;
cout<<"3.Exit"<<endl;
int choice;
       char data;
cout<<"Enter your choice: ";
cin>>choice;
switch(choice)
{
case 1:
cout<<"Enter the data to be inserted: ";
cin>>data;
h.Insert(data);
break;
case 2:
cout<<"Displaying elements of Heap: ";
h.DisplayHeap();
break;
case 3:
exit(1);
default:
cout<<"Enter Correct Choice"<<endl;
}
}
return 0;
}

P-T3TOJFP:/mnt/g/HomeworkLib/c++ noorul-hasan DESKTOP-T3TeOFP: noorul-hasan DESKTOP-T3TeFP: 1.Insert Element 2.Print Heap 3.Exit End noorul-hasan@DESKTOP-T3TOJFP:/mnt/g/HomeworkLib/c++ 3.Exit Enter your choice: 1 Enter the data to be inserted: D 1.Insert Element

Add a comment
Know the answer?
Add Answer to:
Write Binary Heap program in C++. And Insert E,H,I,D,G,F,A,B,C. Then, use reheapup algorithm and print out...
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
  • Using C++, data structures, C++ STL, inputs and expected outputs are shown below. Max Heap Heap...

    Using C++, data structures, C++ STL, inputs and expected outputs are shown below. Max Heap Heap is a specialized tree-based data structure that satisfies the heap property: if P is a parent node of C, then the key (the value) of P is either > (in a max heap) or s (in a min heap) the key of C. The node at the "top" of the heap (with no parents) is called the root node. In binary-tree based heap, it...

  • Write a C program insertion and deletion functions for a max heap represented as a linked binary tree. Assume that each...

    Write a C program insertion and deletion functions for a max heap represented as a linked binary tree. Assume that each node has a parent field as well as the usual left child, right child, and key fields. -Condition : Do not use Array representation. Use the following structure. typedef struct node *treePointer; typedef struct node { int key; treePointer parent; treePointer leftChild, rightChild; }; - INPUT i k : Insert the node with the key value of k in...

  • Write a C program insertion and deletion functions for a max heap represented as a linked binary tree. Assume that each...

    Write a C program insertion and deletion functions for a max heap represented as a linked binary tree. Assume that each node has a parent field as well as the usual left child, right child, and key fields. -Condition : Do not use Array representation. Use the following structure. typedef struct node *treePointer; typedef struct node { int key; treePointer parent; treePointer leftChild, rightChild; };    - INPUT i k : Insert the node with the key value of k in...

  • Please finish all the questions,Thanks Following is Appendix assume the definition of Java class Heap given in the App...

    Please finish all the questions,Thanks Following is Appendix assume the definition of Java class Heap given in the Appendix For this question, a. (2 marks Consider the following heap 30 12 20 19 6 10 18 Given the array representation of a heap discussed in class, what is the array that corre sponds to this heap? b. (5 marks) Successively insert into the heap of part (a.) 22, 35 and 11, in that order. Use the standard heap insertion algorithm....

  • Java Programming Write a program to find the number of comparison using binarySearch and the sequentialSearch...

    Java Programming Write a program to find the number of comparison using binarySearch and the sequentialSearch algorithms as follows: Suppose list is an array of 2500 elements. 1. Use a random number generator to fill list; 2. Use a sorting algorithm to sort list; 3. Search list for some items as follows: a) Use the binary search algorithm to search list (please work on SearchSortAlgorithms.java and modify the algorithm to count the number of comparisons) b) Use the sequential search...

  • 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....

  • C++ Programming By using Binary heap Develop a CPP program to test is an array conforms...

    C++ Programming By using Binary heap Develop a CPP program to test is an array conforms heap ordered binary tree. This program read data from cin (console) and gives an error if the last item entered violates the heap condition. Use will enter at most 7 numbers. Example runs and comments (after // ) are below. Your program does not print any comments. An output similar to Exp-3 and Exp-4 is expected. Exp-1: Enter a number: 65 // this is...

  • Language = c++ Write a program to find the number of comparisons using the binary search...

    Language = c++ Write a program to find the number of comparisons using the binary search and sequential search algorithms as follows: o Suppose list is an array of 1000 elements. o Use a random number generator to fill the list. o Use the function insertOrd to initially insert all the elements in the list. o You may use the following function to fill the list: void fill(orderedArrayListType& list) {       int seed = 47; int multiplier = 2743;                                ...

  • Language C++ Instructions Develop a CPP program to test is an array conforms heap ordered binary...

    Language C++ Instructions Develop a CPP program to test is an array conforms heap ordered binary tree. This program read data from cin (console) and gives an error if the last item entered violates the heap condition. Use will enter at most 7 numbers. Example runs and comments (after //) are below. Your program does not print any comments. An output similar to Exp-3 and Exp-4 is expected. Exp-1: Enter a number: 65 Enter a number: 56 // this is...

  • Write a method called removeDuplicates that accepts a PriorityQueue of integers as a parameter and modifies...

    Write a method called removeDuplicates that accepts a PriorityQueue of integers as a parameter and modifies the queue’s state so that any element that is equal to another element in the queue is removed. For example, if the queue stores [7, 7, 8, 8, 8, 10, 45, 45], your method should modify the queue to store [7, 8, 10, 45]. You may use one stack or queue as auxiliary storage. Please also create a Main Program to test the code....

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