Question

I am coding this in C++ I have a BST filled with a class called Entry....

I am coding this in C++

I have a BST filled with a class called Entry. Entry contains 2 members, a string and an int. The BST is sorted alphabetically by the string of each Entry. How do I take the 3 Entries with the highest int and put them into a vector from GREATEST to LEAST?

   // A helper class that stores a string and a frequency.
   class Entry
   {
   public:
       string s;
       int freq;
   };

   // A helper class that implements a BST node.
   class Node
   {
   public:
       Node()
       {
           left = right = nullptr;
       }

       Node(Entry e)
       {
           this->e = e;
           left = right = nullptr;
       }

       Entry e;
       Node* left;
       Node* right;
   };

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

All the explanation is in the code comments. hope this helps!

Code:

#include <iostream>

using namespace std;

// A helper class that stores a string and a frequency.
class Entry
{
public:
string s;
int freq;
};

// A helper class that implements a BST node.
class Node
{
public:
Node()
{
left = right = nullptr;
}

Node(Entry e)
{
this->e = e;
left = right = nullptr;
}

Entry e;
Node* left;
Node* right;
};

// function to insert string into BST
Node* insert(Node *head, string s) {
  
// empty node
if(head == nullptr)
{
Entry e = Entry();
e.s = s;
e.freq = 1;
head = new Node(e);
return head;
}
// equal node value
if(head->e.s == s) {
head->e.freq += 1;
}
else if(head->e.s < s) // go to right
head->right = insert(head->right, s);
else // go to left
head->left = insert(head->left, s);
return head;
}

// recursive function to traverse the BST to get strings with top 3 frequencies
void getMax(Node *head, Entry max[3])
{
// empty node
if(head == nullptr)
return;
  
// compare present node
for(int i=0; i<3; i++)
{
if(head->e.freq > max[i].freq) {
// shift all the entries from i to 1 place right
for(int j=2; j>i; j--)
{
max[j] = max[j-1];
}
// put present node at i
max[i] = head->e;
break;
}
}
  
// call for left and right trees
getMax(head->left, max);
getMax(head->right, max);
}

int main()
{
// sample run
// create a BST
Node *head = nullptr;
head = insert(head, "hello");
head = insert(head, "banana");
head = insert(head, "c++");
head = insert(head, "apple");
head = insert(head, "leaf");
head = insert(head, "apple");
head = insert(head, "apple");
head = insert(head, "book");
head = insert(head, "banana");
head = insert(head, "tree");
head = insert(head, "c++");
  
// array of first 3 max frequencies for strings
Entry max[3];
for(int i=0; i<3; i++) // initialization
{
max[i].s = "";
max[i].freq = 0;
}
  
// get top 3 frequency strings
getMax(head, max);
// output
cout << "String\t\tFrequency\n";
for(int i=0; i<3; i++)
{
cout << max[i].s << "\t\t" << max[i].freq << endl;
}

return 0;
}

Sample run:

Code screenshots:

Add a comment
Know the answer?
Add Answer to:
I am coding this in C++ I have a BST filled with a class called Entry....
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
  • Consider the partial implementation of a Binary Search Tree (BST) class. For simplicity, each Node stores...

    Consider the partial implementation of a Binary Search Tree (BST) class. For simplicity, each Node stores only the key. Add a public member function to class BST that returns the largest absolute value in the tree. The language is C++ Want the height #4 Coding [6 points] Consider the partial implementation of a Binary Search Tree (BST) class. For simplicity, each Node stores only the key. Add a public member function to class BST that returns the height of the...

  • Add a public member function to the BST class below that returns the size of the...

    Add a public member function to the BST class below that returns the size of the tree—the number of the nodes in the tree. For simplicity, the class contains only the key and not a value. You may add any helper functions you find necessary. (Hint: think recursion) template <typename T> struct Node {                 T key;                 Node<T>*              left;                 Node<T>*              parent;                 Node<T>*              right; }; template <typename T> class BST { private:         Node<T>* root; public:         BST():...

  • hi, I am suppose to implement that method in java for bst but the result doesn't...

    hi, I am suppose to implement that method in java for bst but the result doesn't work as excpected * _Part 8: Implement this method._ * * toString() is a method defined by Java's Object class * that can be used to provide a String representation for your * class. It is called anytime you concatenate an Object to * a String. * * Build a string representation of the BST that describes both * the values stored inside and...

  • I have almost done with this code to Implement Huffman Coding. However I have an error...

    I have almost done with this code to Implement Huffman Coding. However I have an error at the "Heap<Tree>". Could anyone help with solving this issue, really appreciated. Thank you!! import java.util.Scanner; public class HuffmanEncoding { public static void main(String[] args) { Scanner input = new Scanner(System.in); System.out.print("Enter a text: "); String text = input.nextLine();    int[] counts = getCharacterFrequency(text); // Count frequency System.out.printf("%-15s%-15s%-15s%-15s\n", "ASCII Code", "Character", "Frequency", "Code");    Tree tree = getHuffmanTree(counts); // Create a Huffman tree String[]...

  • In need of JAVA CODE FOR THIS OBJECTIVE: Modify the BST class I gave you as...

    In need of JAVA CODE FOR THIS OBJECTIVE: Modify the BST class I gave you as follows: In the case in which the deleted node has two children, write the code in two ways. a) always replace the deleted node with the largest node on the left. b) count how many deletions have been done, and for even deletions replace the deleted node with the largest node on the left, for odd deletions, replace the deleted node with the smallest...

  • BST JAVA FILE import java.util.*; public class BST <E extends Comparable <E>> {    private TreeNode<E>...

    BST JAVA FILE import java.util.*; public class BST <E extends Comparable <E>> {    private TreeNode<E> overallRoot;    public BST() {        overallRoot = null;    }    // ************ ADD ************ //    public void add(E addThis) {        if (overallRoot == null) {            overallRoot = new TreeNode<>(addThis);        } else {            add(overallRoot, addThis);        }    }    private TreeNode<E> add(TreeNode<E> node, E addThis) {        if...

  • Please I need help ASAP Java Programing: Binary Search Tree Fully implement the BST class in Listing 25.4 (on page 961 of the 11th Edition of the text). Design and write a (main) driver program to com...

    Please I need help ASAP Java Programing: Binary Search Tree Fully implement the BST class in Listing 25.4 (on page 961 of the 11th Edition of the text). Design and write a (main) driver program to completely test every method in the BST class to ensure the class meets all its requirements. You should read the Listing 25.5: TestBST.java for an idea of what your program should look like. Listing 25.4 BST.java public class BST> extends AbstractTree { protected TreeNode...

  • C++ Binary tree, help pls #pragma once // include this library to use NULL, otherwise use...

    C++ Binary tree, help pls #pragma once // include this library to use NULL, otherwise use nullptr instead #include <cstddef> #include <iostream> #include "node.hpp" template<class T> class BST{ public: // Constructor for the BST class, creates an empty tree BST(void); // Destructor for the BST class, destroys the tree ~BST(void); // Inserts data into the tree // param: The data to be inserted into the tree void insert(T); // Removes data from the tree // param: The data to be...

  • JAVA CODE Topic: BST Animation For each task, submit the source code with detail comments. Objective:...

    JAVA CODE Topic: BST Animation For each task, submit the source code with detail comments. Objective: javafx GUI program for BST Animation. BSTAnimation.java, AbstractTree.java and Tree.java. Modify the BSTAnimation.java  (1) Add Show Preoreder and Show Postorder button. Insert these two buttons next to the Show Inorder button to display tree in selected order. (2) Currently removing node method is to replace the removed node by the highest node on left-subtree. Modify the method by using lowest node on the right-subtree instead....

  • C++ program, item.cpp implementation. Implementation: You are supposed to write three classes, called Item, Node and In...

    C++ program, item.cpp implementation. Implementation: You are supposed to write three classes, called Item, Node and Inventory respectively Item is a plain data class with item id, name, price and quantity information accompanied by getters and setters Node is a plain linked list node class with Item pointer and next pointer (with getters/setters) Inventory is an inventory database class that provides basic linked list operations, delete load from file / formatted print functionalities. The majority of implementation will be done...

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