Question

A) Write a program to take n elements and insert them into a heap one by...

A) Write a program to take n elements and insert them into a heap one by one. Include a function to print out the elements in the heap.

B) Write a program to take n elements and insert them into a binary tree. Include a function to print out the elements in the tree.

Add commenting if possible so I can learn from the code. Thanks!

Programming language C++ please.

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

A)Write a program to take n elements and insert them into a heap one by one. Include a function to print out the elements in the heap.

#include <iostream>

using namespace std;

// To heapify a subtree rooted with node i which is

// an index in arr[]. N is size of heap

void heapify(int arr[], int n, int i)

{

int largest = i; // Initialize largest as root

    int l = 2 * i + 1; // left = 2*i + 1

    int r = 2 * i + 2; // right = 2*i + 2

    // If left child is larger than root

    if (l < n && arr[l] > arr[largest])

        largest = l;

    // If right child is larger than largest so far

    if (r < n && arr[r] > arr[largest])

        largest = r;

    // If largest is not root

    if (largest != i) {

        swap(arr[i], arr[largest]);

// Recursively heapify the affected sub-tree

        heapify(arr, n, largest);

    }

}

  

// Function to build a Max-Heap from the given array

void buildHeap(int arr[], int n)

{

    // Index of last non-leaf node

    int startIdx = (n / 2) - 1;

  

    // Perform reverse level order traversal

    // from last non-leaf node and heapify

    // each node

    for (int i = startIdx; i >= 0; i--) {

        heapify(arr, n, i);

    }

}

  

// A utility function to print the array

// representation of Heap

void printHeap(int arr[], int n)

{

    cout << "Array representation of Heap is:\n";

for (int i = 0; i < n; ++i)

        cout << arr[i] << " ";

    cout << "\n";

}

  

// main

int main()

{

   int arr[] = { 1, 3, 5, 4, 6, 13, 10, 9, 8, 15, 17 };

int n = sizeof(arr) / sizeof(arr[0]);

  buildHeap(arr, n);

  printHeap(arr, n);

  return 0;

}

B) Write a program to take n elements and insert them into a binary tree. Include a function to print out the elements in the tree.

#include <bits/stdc++.h>

using namespace std;

  

/* A binary tree node has data,

pointer to left child and a

pointer to right child */

struct Node

{

    int data;

    Node* left, * right;

};

  

/* Helper function that allocates a

new node */

Node* newNode(int data)

{

    Node* node = (Node*)malloc(sizeof(Node));

    node->data = data;

    node->left = node->right = NULL;

    return (node);

}

  

// Function to insert nodes in level order

Node* insertLevelOrder(int arr[], Node* root,

                       int i, int n)

{

    // Base case for recursion

    if (i < n)

    {

        Node* temp = newNode(arr[i]);

        root = temp;

  

        // insert left child

        root->left = insertLevelOrder(arr,

                   root->left, 2 * i + 1, n);

  

        // insert right child

        root->right = insertLevelOrder(arr,

                  root->right, 2 * i + 2, n);

    }

    return root;

}

  

// Function to print tree nodes in

// InOrder fashion

void inOrder(Node* root)

{

    if (root != NULL)

    {

        inOrder(root->left);

        cout << root->data <<" ";

        inOrder(root->right);

    }

}

  

// Driver program to test above function

int main()

{

    int arr[] = { 1, 2, 3, 4, 5, 6, 6, 6, 6 };

    int n = sizeof(arr)/sizeof(arr[0]);

    Node* root = insertLevelOrder(arr, root, 0, n);

    inOrder(root);

}

  

Add a comment
Know the answer?
Add Answer to:
A) Write a program to take n elements and insert them into a heap one by...
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 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...

  • please solve this question: Write a character Max-Heap Builder program in C++. The program should display...

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

  • C++ Vectors and Binary Search Trees • Write a program that takes from the user n...

    C++ Vectors and Binary Search Trees • Write a program that takes from the user n integers and stores them a vector of int. Then, create a function insert After that takes first Value and second Value. This function searches for each occurrence of first Value in the vector and insert the second Value after it in the same vector. The first and second values are taken from the user. • Create another function that creates a Binary Search Tree...

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

  • 1. Given the two binary trees below: 14 16 Write a method called swapSubtrees, which swaps all of...

    in java ..write all complete program from a- e 1. Given the two binary trees below: 14 16 Write a method called swapSubtrees, which swaps all of the left and right subtrees in the above binary trees. Add this method to the class BinaryTree and create a program to test this method for these 2 trees. Show the original trees and the resulting trees. Note: To test your algorithm, first create a binary search tree. Write a method called singleParent,...

  • C# prograaming language 1. write a program that generates 10,000 random integers in the range of ...

    c# prograaming language 1. write a program that generates 10,000 random integers in the range of 0-9 and them in binary search tree. 2. use any sort algorithm (insertion, bubble, quick...) to display a list of each of the integers and how many times they appear. 3. at last, add ruction to the BinarySearchTree class that count the number of edges in a tree. Write a program that generates 10,000 random integers in the range of 0-9 and store them...

  • Write a program in Java to implement the max-priority queue using max-heap data structure. Implement the...

    Write a program in Java to implement the max-priority queue using max-heap data structure. Implement the max-heap data structure using an integer array of 10 cells. (Do not use Java in-built PriorityQueue class.) [In a max-heap, the root node and the intermediate node vales are always greater than their children.] First, take 10 integer values from the user and insert them in the max-priority queue. Then print the elements of the queue. After that, delete two elements from the queue...

  • 5. A three-heap with n elements can be stored in an array A, where A[O] contains...

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

  • please do this lab in Java in given instructions by tomorrow morning. TkA CHRI - TREE...

    please do this lab in Java in given instructions by tomorrow morning. TkA CHRI - TREE UTH A HEAPOF PRESDNS CENETH CSC 236-Lab 6 (2 programs) trees 1. Given the two binary trees below: 14 18 16) Write a method called swapSubtrees, which swaps all of the left and right subtrees in the above binary trees. Add this method to the class BinaryTree and create a program to test this method for these 2 trees. Show the original trees and...

  • Write a C Program that inputs an array of integers from the user along-with the length...

    Write a C Program that inputs an array of integers from the user along-with the length of array. The program then prints out the array of integers in reverse. (You do not need to change (re-assign) the elements in the array, only print the array in reverse.) The program uses a function call with array passed as call by reference. Part of the Program has been given here. You need to complete the Program by writing the function. #include<stdio.h> void...

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