Question

please solve this question:

Write a character Max-Heap Builder program in C++. The program should display the menu below. Each item in the menu should be

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

Program

#include <iostream>
#include <vector>

using namespace std;

//class MaxHeap
class MaxHeap
{
private:
vector<char> heap;
int currentSize;
public:
//constructor
MaxHeap()
{
currentSize = 0;
heap.resize(10);
}
/* insert to the heap */
void insertHeap(char x)
{
if (currentSize == heap.size()-1)
heap.resize(heap.size()*2);

currentSize++;
int i = currentSize;

while(x > heap[i/2] && i/2 >= 1){
heap[i] = heap[i/2];
i=i/2;
}
heap[i] = x;
}
/* delete from the heap */
int deleteHeap(char x)
{
int k = searchHeap(x);
if(k==0) return 0;

char t = heap[k];
heap[k] = heap[currentSize];
heap[currentSize] = t;

currentSize--;

x = heap[k];
int parent = k;

while(1)
{
int child = 2*parent;
if(child>=currentSize) break;
if(child+1 <= currentSize &&heap[child+1]>heap[child])
child++;
if(x>=heap[child]) break;
heap[parent] = heap[child];
parent = child;
}
heap[parent] = x;

return 1;
}
/* search into the heap */
int searchHeap(char x)
{
for(int i = 1; i <= currentSize; i++)
if(heap[i]==x)
return i;
return 0;
}

/* Display the MaxHeap */
void display()
{
for(int i = 1; i <= currentSize; i++)
cout<<heap[i]<<" ";
}
};

//function to display menu
void menu()
{
cout<<"1. Add a node"<<endl;
cout<<"2. Delete a node"<<endl;
cout<<"3. Search a node"<<endl;
cout<<"4. Print the tree"<<endl;
cout<<"5. Exit"<<endl;
}


//main function
int main()
{
MaxHeap heap;
int k, op;
char letter;

while(1)
{
menu();
cout<<"Enter option : ";
cin >> op;

switch(op)
{
//insert to the heap
case 1:
cout<<"Enter a letter : ";
cin >> letter;
if(!isalpha(letter))
cout<<"Error: Value is not a letter! "<<endl;
else
heap.insertHeap(letter);
break;
//delete from the heap
case 2:
cout<<"Enter a letter to delete: ";
cin >> letter;
if(!isalpha(letter))
cout<<"Error: Value is not a letter! "<<endl;
else
{
k = heap.deleteHeap(letter);
if(k==0) cout<<"Not found! "<<endl;
}

break;
//search into the heap
case 3:
cout<<"Enter a letter to search: ";
cin >> letter;
if(!isalpha(letter))
cout<<"Error: Value is not a letter! "<<endl;
else
{
k = heap.searchHeap(letter);
if(k==0)
cout<<"Not found! "<<endl;
else
cout<<"Found at "<<k<< " position. "<<endl;
}
break;
//Print the tree
case 4:
heap.display();
break;
//Exit
case 5:
return 0;
}
cout<<endl;
}
return 0;
}

Output:

1. Add a node
2. Delete a node
3. Search a node
4. Print the tree
5. Exit
Enter option : 1
Enter a letter : q

1. Add a node
2. Delete a node
3. Search a node
4. Print the tree
5. Exit
Enter option : 1
Enter a letter : w

1. Add a node
2. Delete a node
3. Search a node
4. Print the tree
5. Exit
Enter option : 1
Enter a letter : e

1. Add a node
2. Delete a node
3. Search a node
4. Print the tree
5. Exit
Enter option : 1
Enter a letter : r

1. Add a node
2. Delete a node
3. Search a node
4. Print the tree
5. Exit
Enter option : 1
Enter a letter : t

1. Add a node
2. Delete a node
3. Search a node
4. Print the tree
5. Exit
Enter option : 1
Enter a letter : y

1. Add a node
2. Delete a node
3. Search a node
4. Print the tree
5. Exit
Enter option : 4
y t w q r e
1. Add a node
2. Delete a node
3. Search a node
4. Print the tree
5. Exit
Enter option : 2
Enter a letter to delete: w

1. Add a node
2. Delete a node
3. Search a node
4. Print the tree
5. Exit
Enter option : 4
y t e q r
1. Add a node
2. Delete a node
3. Search a node
4. Print the tree
5. Exit
Enter option : 2
Enter a letter to delete: y

1. Add a node
2. Delete a node
3. Search a node
4. Print the tree
5. Exit
Enter option : 4
t r e q
1. Add a node
2. Delete a node
3. Search a node
4. Print the tree
5. Exit
Enter option : 3
Enter a letter to search: q
Found at 4 position.

1. Add a node
2. Delete a node
3. Search a node
4. Print the tree
5. Exit
Enter option : 5

N.B. Whether you face any problem or need any modification then share with me in the comment section, I'll happy to help you.

Add a comment
Know the answer?
Add Answer to:
please solve this question: Write a character Max-Heap Builder program in C++. The program should display...
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
  • 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...

  • 1. In Lab 4, you developed a program to build a Max Heap, and then Heap...

    1. In Lab 4, you developed a program to build a Max Heap, and then Heap Sort. Update the program by adding two additional functions: (a) AddData(A, N, V) where V is the new value added. (b) Delete a data Delete by giving the index of the data position Make sure to display the array after calling each of the function. 2. Write a program to implement Binary Search Algorithm, which will return the index of the data searched (V)....

  • C++ program Write a C++ program to manage a hospital system, the system mainly uses file...

    C++ program Write a C++ program to manage a hospital system, the system mainly uses file handling to perform basic operations like how to add, edit, search, and delete record. Write the following functions: 1. hospital_menu: This function will display the following menu and prompt the user to enter her/his option. The system will keep showing the menu repeatedly until the user selects option 'e' from the menu. Arkansas Children Hospital a. Add new patient record b. Search record c....

  • Write a java program implementing the Linked list. It should be on an small office who...

    Write a java program implementing the Linked list. It should be on an small office who has 5 employees. The program ask the user for ID, First name, Last name and what field the work in(eg: accounting, programmer, HR etc). Each employee (with all the information of that paticular employee) should be placed in one node in the program. The program should repeat and ask the user for all 5 employees information. Also when you display the Linked list it...

  • Please help!! (C++ PROGRAM) You will design an online contact list to keep track of names...

    Please help!! (C++ PROGRAM) You will design an online contact list to keep track of names and phone numbers. ·         a. Define a class contactList that can store a name and up to 3 phone numbers (use an array for the phone numbers). Use constructors to automatically initialize the member variables. b.Add the following operations to your program: i. Add a new contact. Ask the user to enter the name and up to 3 phone numbers. ii. Delete a contact...

  • Write a program to use a binary search tree to store a list of computer games....

    Write a program to use a binary search tree to store a list of computer games. Each node in the tree stores the title (string) of a computer game. Different games have different titles. Your program should display the following menu repeatedly: 1. Insert new game 2. Search for games          3. List games 4. quit Option 1 should read a game (title) and add the game into the tree.  Option 2 allows the user to enter a partial key...

  • Write a contacts database program that presents the user with a menu that allows the user...

    Write a contacts database program that presents the user with a menu that allows the user to select between the following options: (In Java) Save a contact. Search for a contact. Print all contacts out to the screen. Quit If the user selects the first option, the user is prompted to enter a person's name and phone number which will get saved at the end of a file named contacts.txt. If the user selects the second option, the program prompts...

  • Please show that the code is working at the end. Your program should read from the standard input...

    Please show that the code is working at the end. Your program should read from the standard input a sequence of integer values, with each value separated by a space. Your task is to: Build a binary search tree using these values in the order they are entered. Print 3 traversals: pre-, in-, and post-order. Allow the user to insert/delete a value. Once a new tree is generated, print it in-order. Find predecessor of a given value. The predecessor is...

  • Assignment on Java programing 1. Write programs for the following exercises in Java. Each file should...

    Assignment on Java programing 1. Write programs for the following exercises in Java. Each file should have short description of the implemented class and for files with main method the problem it is solving. Make sure your files have appropriate names. Programs should write output to the Console. b) BST: Implement Binary Search Tree ADT with insert(int key), delete(int key), Node find(int key), and in-order traverse() where it prints the value of the key. Your operations should use recursion. The...

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