Question

It is a bonus homework to implement either the AVL tree or the heap sort algorithm....

It is a bonus homework to implement either the AVL tree or the heap sort algorithm. You need to write your program in C++ following the object-oriented style, which means to have a header file to define a class, and to have an implementation C++ file to implement the class. You need to have a main.cpp file to test your implementation.

  


Can you explain how you got your answer also .... please and thank you very much.

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

/* Please read all the comments */

/* See the output */

// C++ program to simulate the Heap sort
#include <iostream>

using namespace std;

// Largest element is kept at root place in binary tree.
// Due to behavior of Complete Binary Tree, buildHeap() method can be done in array manner
// Assume Max element, parent data is at Index I
// Left child would be 2I + 1 && Right Child would be 2I + 2
// Array index is from 0
void buildHeap(int arr[], int totalElement, int i)
{
   int biggest = i; // Initialize biggest as root
   int leftChild = 2*i + 1;
   int rightChild = 2*i + 2;

   // check if left child is biggest than root
   if (leftChild < totalElement && arr[leftChild] > arr[biggest])
       biggest = leftChild;

   // If right child is larger than biggest
   if (rightChild < totalElement && arr[rightChild] > arr[biggest])
       biggest = rightChild;

   // If biggest is not root
   if (biggest != i)
   {
       swap(arr[i], arr[biggest]);

       // Recurse again buildHeap the for rest affected section
       buildHeap(arr, totalElement, biggest);
   }
}

// function to do heap sort
void heapSort(int arr[], int total)
{
   // start to Build heap
   for (int i = total / 2 - 1; i >= 0; i--)
       buildHeap(arr, total, i);

   // One by one get element from heap
   for (int i=total-1; i>=0; i--)
   {
       // Move current root element for the end position
       swap(arr[0], arr[i]);

       // Call buildHeap() with reduced heap
       buildHeap(arr, i, 0);
   }
}

// Function to show the array element
void showArray(int arr[], int n)
{
   for (int i=0; i<n; ++i)
       cout << arr[i] << " ";
   cout << "\n";
}

// Main Program
int main()
{
   int intArray[] = {12, 11, 13, 5, 6, 7};
   int n = sizeof(intArray)/sizeof(intArray[0]);
cout<< "Array Before sort "<<endl;
showArray(intArray,n);

cout<<" After HeapSort" ;
   heapSort(intArray, n);

   cout << "Sorted array is \n";
   showArray(intArray, n);
}

/* This is screen shot of output*/

Add a comment
Know the answer?
Add Answer to:
It is a bonus homework to implement either the AVL tree or the heap sort algorithm....
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
  • You are going to implement Treesort algorithm in C++ to sort string data. Here are the...

    You are going to implement Treesort algorithm in C++ to sort string data. Here are the steps to complete the homework 1) Use the following class definition for binary search tree nodes. Its constructor is incomplete you should first complete the constructor. class TreeNode t public: string data; / this is the string stored in the node TreeNode left: TreeNode right; TreeNode (string element, TreeNode 1t, TreeNode rt //your code here 2) Write a function that will insert a string...

  • Goal: design and implement a dictionary. implement your dictionary using AVL tree . Problem​: Each entry...

    Goal: design and implement a dictionary. implement your dictionary using AVL tree . Problem​: Each entry in the dictionary is a pair: (word, meaning). Word is a one-word string, meaning can be a string of one or more words (it’s your choice of implementation, you can restrict the meaning to one-word strings). The dictionary is case-insensitive. It means “Book”, “BOOK”, “book” are all the same . Your dictionary application must provide its operations through the following menu (make sure that...

  • Implement a method to build an AVL tree out of a sorted (ascending order) array of...

    Implement a method to build an AVL tree out of a sorted (ascending order) array of unique integers, with the fastest possible big O running time. You may implement private helper methods as necessary. If your code builds a tree that is other than an AVL tree, you will not get any credit. If your code builds an AVL tree, but is not the fastest big O implementation, you will get at most 12 points. You may use any of...

  • Min heap class implementation in Python. Implement a min-using an array. Your min-heap class will have...

    Min heap class implementation in Python. Implement a min-using an array. Your min-heap class will have one private attribute, an array of integers. Implement the following methods for the min-heap class You may not use the built-in min function. init - Constructor makes an empty heap str - Prints the heap out in any way you want for debugging only) makenull(self) - Makes the heap empty insert(self,x) - Insert element x into the heap parent(self,i) - Returns the index of...

  • Implement in C SharpCreate a new algorithm based on the algorithm, selection sort. The new algorithm...

    Implement in C SharpCreate a new algorithm based on the algorithm, selection sort. The new algorithm should be able to sort an array like this: Input: an array that has n elements, and the values of its elements are assigned randomly, for example: Index 0 1 2 3 4 5 value 7 3 6 2 1 5 Output: an array - its first n/2 elements are sorted in ascending order and its second n/2 elements sorted in descending order. That...

  • Implement an AVL tree stored in a random access file Each node contains an integer key,...

    Implement an AVL tree stored in a random access file Each node contains an integer key, one or more fixed length character strings, a left child reference, a right child reference and a height The format of the file is shown on the next slide The methods you must implement are shown on the followingslides. Duplicate keys cannot be entered in the tree Your implementation MUST NOT load the whole tree in memory. Each operation only makes copies of the...

  • Implement downHeap(Node *n) for a min heap implemented as an ordinary binary tree with an integer...

    Implement downHeap(Node *n) for a min heap implemented as an ordinary binary tree with an integer member variable "value" and left and right child pointers. Any node might have only a left child, only a right child, both, or neither. The starter code below defines a class called "Node" that has two child pointers ("left" , "right") and an integer "value" member variable. There is also a constructor Node(int val) that initializes the children to nullptr and the node's value...

  • Write a Python function to implement the quick sort algorithm over a singly linked list. The...

    Write a Python function to implement the quick sort algorithm over a singly linked list. The input of your function should be a reference pointing to the first node of a linked list, and the output of your function should also be a reference to the first node of a linked list, in which the data have been sorted into the ascending order. (You may use the LinkedQueue class we introduced in the lecture directly in your program.)

  • C++ (Using Binary Search Trees) other methods will result in downvote Implement the binary search tree...

    C++ (Using Binary Search Trees) other methods will result in downvote Implement the binary search tree methods (bst.cpp) for the binary search tree provided in the header file. Test your implementation with the included test. bst.h bst_test.cpp Note: Your implementation must correspond to declarations in the header file, and pass the test. Do not modify these two. I will compile your code against these. If the compilation fails, you will get down vote. bst.h #ifndef BINARY_SEARCH_TREE_H #define BINARY_SEARCH_TREE_H #include <string>...

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