Question

In C++ language, need a full executable program!! Build a templated max heap using a linked...

In C++ language, need a full executable program!!

Build a templated max heap using a linked implementation. Insert 100 unique random int’s into

the heap. Display the heap. Then, delete the first 50 int’s that were inserted. Display the heap.

Keep track of those first 50 int’s using an array or a vector. Display the heap in such a manner

that the viewer is convinced that it is a heap. Now, repeat these actions but using an array

implementation. Comparing the two implementations, which did you find easier to build and

debug?

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

1. Linked List Implementation of heap

#include <stdio.h>
#include <stdlib.h>
#include<time.h>
#define NUM 100
#define RANGE 1000

typedef struct node {
int val;
struct node* left;
struct node* right;
struct node* parent;
} node;

typedef struct queue {
struct queue* next;
node* element;
} queue;

//a factory to create a node
node* get_a_node(int x, node* parent) {
node* nd = (node *)malloc(sizeof(node));
nd->val = x;
nd->left = NULL;
nd->right = NULL;
nd->parent = parent;
return nd;
}

//a factory to create a queue element
queue* get_a_queue(node* element) {
queue* q = (queue *)malloc(sizeof(queue));
q->next = NULL;
q->element = element;
return q;
}

//function to free queue's memory
void free_queue(queue** q) {
queue* current_q = *q;
while(current_q != NULL) {
*q = current_q->next;
free(current_q);
current_q = *q;
}
}

//put nodes into q
void queue_put(node* n, queue** last) {
if(n->left != NULL) {
(*last)->next = get_a_queue(n->left);
(*last) = (*last)->next;
}
if(n->right != NULL) {
(*last)->next = get_a_queue(n->right);
(*last) = (*last)->next;
}
}

//insert a node into a heap
void heap_insert(node** root, int x) {
//insert new node if the heap is empty
if(*root == NULL) {
*root = get_a_node(x, NULL);
}
//serach for a proper insert point
else {
queue* q = get_a_queue(*root);
queue* current_q = q;
queue* last_q = q;
node* current_node;

while(current_q != NULL) {
//get one node out of queue
current_node = current_q->element;

if(current_node->left == NULL) {
current_node->left = get_a_node(x, current_node);
current_node = current_node->left;
}
else if(current_node->right == NULL) {
current_node->right = get_a_node(x, current_node);
current_node = current_node->right;
}
else {
queue_put(current_node, &last_q);
//increment the current pointer
current_q = current_q->next;
continue;
}

//put the node to proper point
while(current_node->parent != NULL && current_node->parent->val < current_node->val) {
//swap the value
int temp = current_node->parent->val;
current_node->parent->val = current_node->val;
current_node->val = temp;
current_node = current_node->parent;
}
break;

}

free_queue(&q);
}
return;
}

void heap_remove(node** root) {
if(*root != NULL) {
queue* q = get_a_queue(*root);
queue* current_q = q;
queue* last_q = q;
queue* previous_q;
node* current_node;

//find the last node
while(current_q != NULL) {
current_node = current_q->element;

queue_put(current_node, &last_q);

previous_q = current_q;
current_q = current_q->next;
}
current_node = previous_q->element;
free_queue(&q);

//remove last node and get the value to root
if(current_node->parent == NULL) {
free(current_node);
*root = NULL;
}
else {
(*root)->val = current_node->val;
current_node = current_node->parent;
if(current_node->right != NULL) {
free(current_node->right);
current_node->right = NULL;
}
else {
free(current_node->left);
current_node->left = NULL;
}
//move down the value
int a, b, c;
current_node = *root;
while(1) {
if(current_node->left == NULL) {
//implies current->right == NULL is true
break;
}
else if(current_node->right == NULL) {
a = current_node->val;
b = current_node->left->val;
if(a < b) {
current_node->val = b;
current_node->left->val = a;
current_node = current_node->left;
}
else {
break;
}
}
else {
a = current_node->val;
b = current_node->left->val;
c = current_node->right->val;
//a is the largest, do nothing
if(a >= b && a >= c) {
break;
}
//b is the largest, swap a and b
else if(b > a && b >= c) {
current_node->left->val = a;
current_node->val = b;
current_node = current_node->left;
}
//c is the largest, swap a and c
else {
current_node->right->val = a;
current_node->val = c;
current_node = current_node->right;
}
}
}
}
}
}

void print_heap(node** root) {
if(*root != NULL) {
queue* q = get_a_queue(*root);
queue* current_q = q;
queue* last_q = q;
node* test;
while(current_q != NULL) {
test = current_q->element;
queue_put(test, &last_q);
printf("%d ", test->val);
current_q = current_q->next;
}
free_queue(&q);
printf("\n");
}
}

//linear search for max of the array
int max(int* a, int length, int* index) {
int i, max = a[0];
*index = 0;
for(i = 0; i < length; i++) {
if(a[i] > max) {
max = a[i];
*index = i;
}
}
return max;
}

int main () {
node *root = NULL;
int i, r, m;
int verifier[NUM];

srand(time(NULL));

for(i = 0; i < NUM; i++) {
r = rand()%RANGE;
heap_insert(&root, r);
verifier[i] = r;
}

printf("Element removed are ");
for(i=0;i<50;i++)
{
printf("%d\n",root->val);
heap_remove(&root);
}
printf("Remaining Heap ");
print_heap(&root);

return 0;
}

2.Using Array

#include <iostream>
#include <time.h>
using namespace std;
void max_heapify(int *a, int i, int n)
{
int j, temp;
temp = a[i];
j = 2 * i;
while (j <= n)
{
if (j < n && a[j+1] > a[j])
j = j + 1;
if (temp > a[j])
break;
else if (temp <= a[j])
{
a[j / 2] = a[j];
j = 2 * j;
}
}
a[j/2] = temp;
return;
}
void build_maxheap(int *a,int n)
{
int i;
for(i = n/2; i >= 1; i--)
{
max_heapify(a,i,n);
}
}
int main()
{
int n=100, i,a[100],r;
srand(time(NULL));

for(i = 0; i < n; i++) {
r = rand()%1000;
a[i]=r;
}
build_maxheap(a,n);
printf("Removing first 50 elements ");
for(i=0;i<50;i++)
{
build_maxheap(&a[i],n-i);
}
cout<<"Max Heap\n";
for (i = 1; i <= n-50; i++)
{
cout<<a[i]<<" ";
}

}

Comparisions:

1. Implementation using array is much easier as we don't have to keep track of pointers.

2.In Linked List traversal takes more time.

3.Array implementation is faster as it takes less time to travel.

4.Linked List takes more memory.

5.Array implementation is easier to debug as it contains less lines of code.

Add a comment
Know the answer?
Add Answer to:
In C++ language, need a full executable program!! Build a templated max heap using a linked...
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 Java program, In this project, you are going to build a max-heap using array...

    Write a Java program, In this project, you are going to build a max-heap using array representation. In particular, your program should: • Implement two methods of building a max-heap. o Using sequential insertions (its time complexity: ?(?????), by successively applying the regular add method). o Using the optimal method (its time complexity: ?(?), the “smart” way we learned in class). For both methods, your implementations need to keep track of how many swaps (swapping parent and child) are required...

  • Using Java In this project, you are going to build a max-heap. You will use an...

    Using Java In this project, you are going to build a max-heap. You will use an array to implement the heap. Your program should: ? Allow the user to select one of the following two choices (Note that your program needs to implement both choices): o (1) test your program with 100 randomly generated integers (no duplicates, positive numbers with proper range); o (2) test your program with the following 100 fixed values from 1, 2, 3, ..., and 100....

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

  • I need this in C++. This is all one question Program 2: Linked List Class For...

    I need this in C++. This is all one question Program 2: Linked List Class For this problem, let us take the linked list we wrote in a functional manner in a previous assignment and convert it into a Linked List class. For extra practice with pointers we'll expand its functionality and make it a doubly linked list with the ability to traverse in both directions. Since the list is doubly linked, each node will have the following structure: struct...

  • In C Program This program will store the roster and rating information for a soccer team. There w...

    In C Program This program will store the roster and rating information for a soccer team. There will be 3 pieces of information about each player: Name: string, 1-100 characters (nickname or first name only, NO SPACES) Jersey Number: integer, 1-99 (these must be unique) Rating: double, 0.0-100.0 You must create a struct called "playData" to hold all the information defined above for a single player. You must use an array of your structs to to store this information. You...

  • Edit a C program based on the surface code(which is after the question's instruction.) that will...

    Edit a C program based on the surface code(which is after the question's instruction.) that will implement a customer waiting list that might be used by a restaurant. Use the base code to finish the project. When people want to be seated in the restaurant, they give their name and group size to the host/hostess and then wait until those in front of them have been seated. The program must use a linked list to implement the queue-like data structure....

  • RE-POSTED - Computer Science staff, I need this question answered. It will determine a pass or...

    RE-POSTED - Computer Science staff, I need this question answered. It will determine a pass or a fail. Its very imortant that this and the other questions posted are answered 100% Thank you. 13 - Template C++ Advance Please Only answer assignment if your code is 100%. When pasteing your code use text so I may copy and paste into visual studio. The code and questions must be answered 100% correct and works. Thank you. Programming Assignment Convert the int...

  • How to write the insert, search, and remove functions for this hash table program? I'm stuck......

    How to write the insert, search, and remove functions for this hash table program? I'm stuck... This program is written in C++ Hash Tables Hash Table Header File Copy and paste the following code into a header file named HashTable.h Please do not alter this file in any way or you may not receive credit for this lab For this lab, you will implement each of the hash table functions whose prototypes are in HashTable.h. Write these functions in a...

  • SYNOPSIS The product manager for coffee development at Kraft Canada must decide whether to introduce the...

    SYNOPSIS The product manager for coffee development at Kraft Canada must decide whether to introduce the company's new line of single-serve coffee pods or to await results from the product's launch in the United States. Key strategic decisions include choosing the target market to focus on and determining the value proposition to emphasize. Important questions are also raised in regard to how the new product should be branded, the flavors to offer, whether Kraft should use traditional distribution channels or...

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