Question

Lab 6 Help

I need help with this lab assignment. I use C++, thank you.

 

  • Derive a MinHeap class from the BST class of your Lab 4 code.

  • Define/Override only the Search/Insert/Delete methods in the new class.

  • Write a new main for this lab which:

    • Will only be a test program for the MinHeap.

    • Declares and creates a MinHeap object as necessary.

    • Declares and creates the Dollar objects as necessary.

    • Inserts the 20 Dollar objects specified in Lab 4 in the same order.

    • Performs all 4 traversals after the inserting the 10th and the last objects - in total there will be 8 outputs which should be clearly demarcated.

    • No user input is necessary, no data validation is necessary.

  • For submission - upload all class files from Lab 4 as necessary as well as your new MinHeap class and the main. Remember to also include adequate number of screenshots of the program execution.

 

My lab 4 code (you can edit it if you have to):

 

#include
#include
using namespace std;
class Node {
public:
       float value;
       Node *left;
       Node *right;
       Node(float value) {
               this->value = value;
               right = NULL;
               left = NULL;
       }
};

class BinaryTree {
private:
   Node* insertHelper(Node* current, float value) {
               if (current == NULL) {
           return new Node(value);
               }
               if (value < current->value) {
                       current->left = insertHelper(current->left, value);
               } else if (value > current->value) {
                       current->right = insertHelper(current->right, value);
               } else {
                       return current;
               }
               return current;
       }
       bool searchNode(Node* current, float value) {
               if (current == NULL) {
                       return false;
               }
               if (value == current->value) {
                       return true;
               }
               return value < current->value ? searchNode(current->left, value) : searchNode(current->right, value);
       }
   Node* deleteHelper(Node* root, float key){
       if (root == NULL)
           return root;
       if (key < root->value)
           root->left = deleteHelper(root->left, key);
       else if (key > root->value)
           root->right = deleteHelper(root->right, key);

       else {
           if (root->left == NULL)
               return root->right;
           else if (root->right == NULL)
               return root->left;
           root->value = minValue(root->right);
           root->right = deleteHelper(root->right, root->value);
       }

       return root;
   }
public:
       Node* root;

   void insert(float value) {
               root = insertHelper(root, value);
       }

       bool find(int value) {
               return searchNode(root, value);
       }
       void update(float old,float new_){
           if(this->find(old)){
           this->delete_(old);
           this->insert(new_);
           }
           cout<<"Element to be updated not found";
       }


       int minValue(Node* root)
   {
       int min = root->value;
       while (root->left != NULL)
       {
           min = root->left->value;
           root = root->left;
       }
       return min;
   }
       void delete_(int key) {
               root = deleteHelper(root, key);
       }

       void postOrder(Node* node) {
               if (node != NULL) {
                       postOrder(node->left);
                       postOrder(node->right);
                       cout<<" "<< node->value;
               }
       }
       void preOrder(Node* node) {
               if (node != NULL) {
                       cout<<" "<< node->value;
                       preOrder(node->left);
                       preOrder(node->right);
               }
       }
       void inOrder(Node* node) {
               if (node != NULL) {
                       inOrder(node->left);
                       cout<<" "<< node->value;
                       inOrder(node->right);
               }
       }
};
int main(){
   BinaryTree bt;
   ifstream file;
   file.open("Currency.h");
   if(!file.is_open()){
       cout<<"File not found check file info"<   }else{
       string temp;
       while(getline(file,temp)){
           temp.replace(0,1,"");
           float value = stof(temp);
           bt.insert(value);
       }
   }
   bt.preOrder(bt.root);
   bt.postOrder(bt.root);
   bt.inOrder(bt.root);
   file.close();
   return 0;
}


0 0
Add a comment Improve this question Transcribed image text
Request Professional Answer

Request Answer!

We need at least 10 more requests to produce the answer.

0 / 10 have requested this problem solution

The more requests, the faster the answer.

Request! (Login Required)


All students who have requested the answer will be notified once they are available.
Know the answer?
Add Answer to:
Lab 6 Help
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Similar Homework Help Questions
  • Can you take a look at my code that why the maxDepth function is not working?...

    Can you take a look at my code that why the maxDepth function is not working? public class BinaryTree {          class Node{        int key;        Node left,right;               public Node(int item) {            key = item;            left = right = null;        }    }       Node root;       public void BinaryTree(){        root = null;    }           void...

  • Question - modify the code below so that for a node, the value of every node...

    Question - modify the code below so that for a node, the value of every node of its right subtree is less the node, and the value of each node of its left subtree is greater than the node. - create such a binary tree in the Main method, and call the following method:  InOrder(Node theRoot),  PreOrder(Node theRoot),  PostOrder(Node theRoot),  FindMin(),  FindMax(),  Find(int key) using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks;...

  • Q. write the algorithm for each function in this code: void insert(int x, node*&p) { //cheak...

    Q. write the algorithm for each function in this code: void insert(int x, node*&p) { //cheak if the pointer is pointing to null. if (p==NULL) {     p = new node;     p->key=x;     p->left=NULL;     p->right=NULL; } else {     //Cheak if the element to be inserted is smaller than the root.     if (x < p->key)     {       //call the function itself with new parameters.       insert(x,p->left);     }     //cheak if the alement to be inserted...

  • Using the following implementation of Tree class Node   {   public int iData;              // data item (key)   public...

    Using the following implementation of Tree class Node   {   public int iData;              // data item (key)   public double dData;           // data item   public Node leftChild;         // this node's left child   public Node rightChild;        // this node's right child   public void displayNode()      // display ourself      {      System.out.print('{');      System.out.print(iData);      System.out.print(", ");      System.out.print(dData);      System.out.print("} ");      }   }  // end class Node //------------------------------------------------------------------ import java.io.IOException; import java.util.Stack; public class Tree { private Node root; // first node of tree // ------------------------------------------------------------- public Tree() // constructor { root = null; }...

  • C++ Help with Test #1 (at bottom) to work properly. May adjust code to work or...

    C++ Help with Test #1 (at bottom) to work properly. May adjust code to work or add any additional code. Do not use global variables. #include <iostream> #include <string> using std::endl; using std::cout; using std::cin; using std::string; void pressAnyKeyToContinue() { printf("Press any key to continue\n"); cin.get(); } //This helps with testing, do not modify. bool checkTest(string testName, int whatItShouldBe, int whatItIs) {    if (whatItShouldBe == whatItIs) { cout << "Passed " << testName << endl; return true; } else...

  • 3. (Gaddis Exercises 20.4) Tree Height Write a recursive member function for the BinaryTree class that...

    3. (Gaddis Exercises 20.4) Tree Height Write a recursive member function for the BinaryTree class that returns the height of the tree. The height of the tree is the number of levels it contains. Demonstrate the function in a driver program. CPP FILE CODE: #include "BinaryTree.h" #include <iostream> using namespace std; BinaryTree::BinaryTree() { root = NULL; } BinaryTree::~BinaryTree() { destroy(root); } bool BinaryTree::search(int data) { return search(data, root); } void BinaryTree::insert(int data) { insert(data, root); } void BinaryTree::traverseInOrder() { traverseInOrder(root);...

  • I need to make it so this program outputs to an output.txt, the program works fine,...

    I need to make it so this program outputs to an output.txt, the program works fine, just need it to fprintf to output.txt #include <stdio.h> #include <string.h> #include <malloc.h> #define MAX 30 struct treeNode { char names[MAX];    struct treeNode *right; struct treeNode *left; }*node; void searchName(char names[], struct treeNode ** parent, struct treeNode ** location) { struct treeNode * ptr, * tempPtr; if(node == NULL)    { *location = NULL; *parent = NULL; return; } if(strcmp(names, node->names) == 0)...

  • Convert the TreeArray C++ source code(posted below) into a BinaryTree, using this TreeNode definition: class TreeNode<T>...

    Convert the TreeArray C++ source code(posted below) into a BinaryTree, using this TreeNode definition: class TreeNode<T>       T data       TreeNode<T> left       TreeNode<T> right Since this TreeNode is a generic Template, use any data file we've used this quarter to store the data in the BinaryTree. To do this will likely require writing a compare function or operator. Hint: Think LEFT if the index is calculate (2n+1) and RIGHT if index is (2n+2). Source code: #include<iostream> using namespace std;...

  • Write a function, swapSubtrees, that swaps all of the left and right subtrees of a binary...

    Write a function, swapSubtrees, that swaps all of the left and right subtrees of a binary tree. Add this function to the class binaryTreeType and create a program to test this function. #include <iostream> using namespace std; //Definition of the Node template <class elemType> struct nodeType { elemType info; nodeType<elemType> *lLink; nodeType<elemType> *rLink; }; //Definition of the class template <class elemType> class binaryTreeType { public: //Overload the assignment operator. const binaryTreeType<elemType>& operator=(const binaryTreeType<elemType>&) { if (this != &otherTree) //avoid self-copy...

  • Need help in the below question. Answer in java Start with the tree.java program (Listing 8.1)...

    Need help in the below question. Answer in java Start with the tree.java program (Listing 8.1) and modify it to create a binary tree from a string of letters (like A, B, and so on) entered by the user. Each letter will be displayed in its own node. Construct the tree so that all the nodes that contain letters are leaves. Parent nodes can contain some non-letter symbol like +. Make sure that every parent node has exactly two children....

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