1)
#include <iostream>
using namespace std;
// Function to create max heap
void MaxHeap(int numbers[], int pos, int size)
{
// Stores the current position data
int temp = numbers[pos];
// Calculates the next child position
int nextPos = 2 * pos;
// Loops till next position is less than or equals to size of the number array
while (nextPos <= size)
{
// Checks if next position is less than size and
// data at next position plus one position is greater than the next position data
if (nextPos < size && numbers[nextPos + 1] > numbers[nextPos])
// Update the next position
nextPos = nextPos + 1;
// Stop the loop if parent value is already greater than child value
if (temp > numbers[nextPos])
break;
// Switching value with the parent node id it is less
else if (temp <= numbers[nextPos])
{
numbers[nextPos / 2] = numbers[nextPos];
nextPos = 2 * nextPos;
}// End of else
}// End of while loop
numbers[nextPos / 2] = temp;
return;
}// End of function
// Function to perform heap sort
void HeapSortImplementation(int numbers[], int size)
{
// Loops from end of the array to 2nd position in reverse order
for (int pos = size; pos >= 2; pos--)
{
// Storing maximum value at the end
// Using swapping process
int temp = numbers[pos];
numbers[pos] = numbers[1];
numbers[1] = temp;
// Calls the function to build max heap of remaining element
MaxHeap(numbers, 1, pos - 1);
}// End of for loop
}// End of function
// Function to call the function maxHeap to generate max heap
void BuildMaximumHeap(int numbers[], int size)
{
for(int pos = size / 2; pos >= 1; pos--)
MaxHeap(numbers, pos, size);
}// End of function
// main function definition
int main()
{
int size;
// Accepts the size of the array
cout<<"\n Enter the number of element to be sorted: ";
cin>>size;
size++;
// Creates an array
int numbers[size];
// Loops till size
for(int c = 1; c < size; c++)
{
// Accepts each element
cout<<"Enter element "<<c<<": ";
cin>>numbers[c];
}// End of for loop
// Calls the function to build max heap
BuildMaximumHeap(numbers, size - 1);
// Calls the function to sort
HeapSortImplementation(numbers, size - 1);
cout<<"\nSorted Data ";
// Loops till end to print the sorted data.
for (int c = 1; c < size; c++)
cout<<"->"<<numbers[c];
return 0;
}// End of main function
Sample Output:
Enter the number of element to be sorted: 8
Enter element 1: 10
Enter element 2: 222
Enter element 3: 89
Enter element 4: 3
Enter element 5: 701
Enter element 6: 33
Enter element 7: 56
Enter element 8: 98
Sorted Data ->3->10->33->56->89->98->222->701
2)
#include <iostream>
#include <time.h>
#include <stdlib.h>
using namespace std;
// Function to display the data set
void display(int *list, int size)
{
// Loops till end of the data set
for (int i = 0; i < size; i++)
// Displays each element data
cout << list[i] << " ";
cout << endl;
}// End of function
// Function to search a key in the list using binary search
// Returns found index position if found otherwise returns -1
int binarySearch(int *list, int size, int key)
{
// Initializes the beginning index position of the list to zero
int begin = 0;
// Initializes the last index position of the list to size minus one
int end = size - 1;
// Loops till beginning is less than or equals to end
while (begin <= end)
{
// Calculates the middle index position
int middle = (begin + end) / 2;
// Checks if the middle index position value is equals to key value
if (list[middle] == key)
// Returns the found index position as middle value
return middle;
// Otherwise checks if key value is less than the middle index position value of the list
else if (key < list[middle])
// Update the end index position by middle minus one
end = middle - 1;
// Otherwise the key value is greater than the middle index position value of the list
else
// Update the beginning index position by middle plus one
begin = middle + 1;
}// End of while loop
// Returns -1 for not found
return -1;
}// End of while loop
// main function definition
int main()
{
// Defines a constant for list size to 1000
const int SIZE = 100;
// Defines a constant for number of searches to 20
const int NUM_SEARCHES = 5;
// Declares an array of size 1000
int list[SIZE];
// Declares an array of size 20 to store search numbers
int testValues[NUM_SEARCHES];
srand(time(0));
// Loops till end of the list size
for (int i = 0; i < SIZE; i++)
// Generates random number and stores it in array
list[i] = rand() % (SIZE);
// Loops till end of the searched array size
for (int i = 0; i < NUM_SEARCHES; i++)
// Generates random number and stores it in array
testValues[i] = rand() % (SIZE * 5);
// Calls the function to display the numbers
cout<<"\n *********** Numbers in the list *********** \n";
display(list, SIZE);
// Calls the function to display the numbers to search
cout<<"\n *********** Numbers to search *********** \n";
display(list, NUM_SEARCHES);
cout << "\n *********** BINARY SEARCH TEST *********** \n";
// Loops till end of the search array size
for (int i = 0; i < NUM_SEARCHES; i++)
{
// Calls the function to perform binary search and store the found index status
int index = binarySearch(list, SIZE, testValues[i]);
// Checks if found index position is -1 then display message key not found with search key value
if (index == -1)
cout << "Did not find " << testValues[i] << endl;
// Otherwise, display the search key with found index position
else
cout << "Found " << testValues[i] << " at index " << index << endl;
}// End of for loop
}// End of main function
Sample Output:
*********** Numbers in the list ***********
17 20 16 54 15 86 45 40 47 6 54 5 3 88 33 16 18 20 35 39 40 62 39 68 32 98 60 78 65 34 79 11 58 57 69 5 63 36 43 95 79 14 47 6 3 86 29 80 63 47 78 55 87 67 33 29 37 5 74 86 34 3 64 18 73 65 54 97 59 26 70 21 39 27 29 87 22 5 5 0 91 2 35 77 51 80 79 64 55 33 58 13 60 18 24 10 88 35 78 75
*********** Numbers to search ***********
17 20 16 54 15
*********** BINARY SEARCH TEST ***********
Did not find 231
Did not find 63
Did not find 94
Found 64 at index 87
Did not find 233
3) V = 200
v = 12
v = 90
1. In Lab 4, you developed a program to build a Max Heap, and then Heap...
Prob. 3. Given the following data 50 40 23 36 19 20 9 a) Is this a max heap, draw the tree and check if this is a max heap. b) Illustrate how you would add a new data 46 to the existing maxheap. c) From the answer obtained in part b, illustrate how you would delete the data 40 d) Now illustrate heap sort with the existing data after step c. Give steps, and find runtime. Runtime|Explanation of Algorithm...
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 implemented in a function. a. Add a node. One node to be added to the max-heap. b. Delete a node. One node to be deleted from the max-heap. C. Search a node. Returns true if the node exists in the max-heap, otherwise it returns false. d. Print the tree. Prints the heap in level-order...
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...
A heap can be encoded either as an array, or as a full binary tree. For this question, write a function that takes the array representation of a heap and outputs its binary tree representation. More specifically, you should write a function with the specifications given below. Specifications for the function: # def arrayToTree(A, j): # input: array A representing a heap, an index j in [0:len(A)] # output: a Node object storing the heap with root j in the...
NOTE: Completing the Third Chart is the most important. This is one question with three parts. (4 pts) Is the following array-based tree a min-heap or a max-heap or not a heap at all? 85 91 S8 95 100 92 a. Min-heap b. Max-heap c. Not a heap 5 pts) Turn the following array-based binary tree into a max-heap. Show your work step by step. (You will not need all the columns) 34 7 12 47 19 5 pts) Show...
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...
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....
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...
5. A three-heap with n elements can be stored in an array A, where A[O] contains the root of the tree. a) Draw the three-heap that results from inserting 5, 2, 8, 3, 6, 4, 9, 7, 1 in that order into an initially empty three-heap. You do not need to show the array representation of the heap. You are only required to show the final tree, although if you draw intermediate trees. b) Assuming that elements are placed in...
IN JAVA 2 A Binary Search Tree The goal of this lab is to gain familiarity with simple binary search trees. 1. Begin this lab by implementing a simple class that represents a "node” in a binary search tree, as follows. public class MyTreeNode<t extends Comparable<T>> { public T data; public MyTreeNode<T> leftchild; public MyTreeNode<T> rightChild; public MyTreeNode<T> parent; 2. Have the second member of your pair type in the code for the simple binary search tree interface. public interface...