Question

C++, implement the bst.cpp file without adding any additional methods or functions to it ======================================================================== //...

C++, implement the bst.cpp file without adding any additional methods or functions to it

========================================================================

// bst.cpp

#include <iostream>

#include "bst.h"

using namespace std;

BinarySearchTree::BinarySearchTree()

{

root = nullptr;

}

void BinarySearchTree::insert(int key, string val)

{

Node* new_node = new Node;

new_node->key = key;

new_node->val = val;

new_node->left = nullptr;

new_node->right = nullptr;

if (root == nullptr) {

root = new_node;

} else {

insertHelper(root, new_node);

}

}

void BinarySearchTree::insertHelper(Node* parent, Node* new_node)

{

if (new_node->key < parent->key) {

if (parent->left == nullptr) {

parent->left = new_node;

}

else {

insertHelper(parent->left, new_node); }

}

else if (new_node->key > parent->key) {

if (parent->right == nullptr) {

parent->right = new_node;

}

else {

insertHelper(parent->right, new_node);

}

}

}

int BinarySearchTree::max() const {

// Assume non empty tree

// Your code here

}

int BinarySearchTree::min() const {

// Assume non empty tree

// Your code here

}

void BinarySearchTree::deleteMax() {

// Your code here

}

void BinarySearchTree::printPreOrder() const {

if (root == nullptr) {

cout << endl;

}

printPreOrderHelper(root);

cout << endl;

}

void BinarySearchTree::printPreOrderHelper(Node* n) const {

// Your code here

}

========================================================================

// bst.h

#ifndef BINARY_SEARCH_TREE_H

#define BINARY_SEARCH_TREE_H

#include <string>

using namespace std;

class Node

{

private:

int key;

string val;

Node* left;

Node* right;

friend class BinarySearchTree;

};

class BinarySearchTree

{

public:

BinarySearchTree();

void insert(int key, string val); // Recursive

int max() const; // Max key

int min() const; // Min key

void deleteMax(); // Deletes the max value;

void printPreOrder() const; // Prints keys pre-ordered. Recursive

private:

Node* root;

void insertHelper(Node* parent, Node* new_node);

void printPreOrderHelper(Node *n) const; //Helper for recursive

implemenation of printInroder()

};

#endif

===============================================================================

// bst_test.cpp

#include <iostream>

#include "bst_exam.h"

using namespace std;

// Tests the binary search tree class.

int main()

{

BinarySearchTree t;

t.insert(5, "Boron");

t.insert(3, "Lithium");

t.insert(7, "Nitrogen");

t.insert(2, "Helium");

t.insert(4, "Berylium");

t.insert(6, "Carbon");

t.insert(8, "Oxygen");

cout << "Max key " << t.max() << endl;

cout << "Min key " << t.min() << endl;

t.deleteMax();

t.printPreOrder(); // Prints 5 3 2 4 7 6

return 0;

}

==========================================================

Thank you for your time and help!

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

PFB completed code with appropriate comments

=============

// bst.cpp

#include <iostream>

#include "bst.h"

using namespace std;

BinarySearchTree::BinarySearchTree()

{

root = nullptr;

}

void BinarySearchTree::insert(int key, string val)

{

Node* new_node = new Node;

new_node->key = key;

new_node->val = val;

new_node->left = nullptr;

new_node->right = nullptr;

if (root == nullptr) {

root = new_node;

} else {

insertHelper(root, new_node);

}

}

void BinarySearchTree::insertHelper(Node* parent, Node* new_node)

{

if (new_node->key < parent->key) {

if (parent->left == nullptr) {

parent->left = new_node;

}

else {

insertHelper(parent->left, new_node); }

}

else if (new_node->key > parent->key) {

if (parent->right == nullptr) {

parent->right = new_node;

}

else {

insertHelper(parent->right, new_node);

}

}

}

int BinarySearchTree::max() const {

   // Assume non empty tree

   // Your code here
   // The rightmost node in a BST has the max value
   if (root == nullptr) {
       return -1;
   }
   else {
       Node* tempNode = root;
       while (tempNode->right != nullptr)
       {
           tempNode = tempNode->right;
       }
       return tempNode->key;
   }

}

int BinarySearchTree::min() const {

// Assume non empty tree

// Your code here
   // The leftmost node in a BST has the min value
   if (root == nullptr) {
       return -1;
   }
   else {
       Node* tempNode = root;
       while (tempNode->left != nullptr)
       {
           tempNode = tempNode->left;
       }
       return tempNode->key;
   }
}

void BinarySearchTree::deleteMax() {

// Your code here
   // Rightmost element in BST has the max value
   // Link the left node of maxnode to it's parent's right to make it bst

   Node* tempNode = root;
   Node* tempParent = root;
   while (tempNode->right != nullptr)
   {
       tempParent = tempNode;
       tempNode = tempNode->right;
   }
   // By this step, tempNode is our maxNode
   tempParent->right = tempNode->left;

}

void BinarySearchTree::printPreOrder() const {

if (root == nullptr) {

cout << endl;

}

printPreOrderHelper(root);

cout << endl;

}

void BinarySearchTree::printPreOrderHelper(Node* n) const {

// Your code here
   if (n != nullptr) {
       cout << n->key;
       cout << " ";
       printPreOrderHelper(n->left);
       printPreOrderHelper(n->right);
   }
}

========================================================================

// bst.h

#ifndef BINARY_SEARCH_TREE_H

#define BINARY_SEARCH_TREE_H

#include <string>

using namespace std;

class Node

{

private:

int key;

string val;

Node* left;

Node* right;

friend class BinarySearchTree;

};

class BinarySearchTree

{

public:

BinarySearchTree();

void insert(int key, string val); // Recursive

int max() const; // Max key

int min() const; // Min key

void deleteMax(); // Deletes the max value;

void printPreOrder() const; // Prints keys pre-ordered. Recursive

private:

Node* root;

void insertHelper(Node* parent, Node* new_node);

void printPreOrderHelper(Node *n) const; //Helper for recursive

implemenation of printInroder()

};

#endif

===============================================================================

// bst_test.cpp

#include <iostream>

#include "bst_exam.h"

using namespace std;

// Tests the binary search tree class.

int main()

{

BinarySearchTree t;

t.insert(5, "Boron");

t.insert(3, "Lithium");

t.insert(7, "Nitrogen");

t.insert(2, "Helium");

t.insert(4, "Berylium");

t.insert(6, "Carbon");

t.insert(8, "Oxygen");

cout << "Max key " << t.max() << endl;

cout << "Min key " << t.min() << endl;

t.deleteMax();

t.printPreOrder(); // Prints 5 3 2 4 7 6

return 0;

}

Add a comment
Know the answer?
Add Answer to:
C++, implement the bst.cpp file without adding any additional methods or functions to it ======================================================================== //...
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
  • 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>...

  • Write a method that determines the key of the successor of the root node in a...

    Write a method that determines the key of the successor of the root node in a binary search tree. For any input binary search tree, find the key of successor of the root node.Note: Successor is the node with the next highest key,  should work for any binary search tree - not just the given example input. #include <iostream> using namespace std; class Node { private: int key; string val; Node* left; Node* right; friend class BinarySearchTree; }; class BinarySearchTree {...

  • In C++ and use functions that are asked for, thanks! Implement the BinarySearchTree ADT in a...

    In C++ and use functions that are asked for, thanks! Implement the BinarySearchTree ADT in a file BinarySearchTree.h exactly as shown below. // BinarySearchTree.h // after Mark A. Weiss, Chapter 4, Dr. Kerstin Voigt #ifndef BINARY_SEARCH_TREE_H #define BINARY_SEARCH_TREE_H #include <cassert> #include <iostream> using namespace std; template <typename C> class BinarySearchTree { public: BinarySearchTree( ) : root{ nullptr } { } ~BinarySearchTree( ) { makeEmpty(); } const C & findMin( ) const { assert(!isEmpty()); return findMin( root )->element; } const C...

  • please write the code in C++ Implement the BinarySearchTree ADT in a file BinarySearchTree.h exactly as...

    please write the code in C++ Implement the BinarySearchTree ADT in a file BinarySearchTree.h exactly as shown below. // BinarySearchTree.h // after Mark A. Weiss, Chapter 4, Dr. Kerstin Voigt #ifndef BINARY_SEARCH_TREE_H #define BINARY_SEARCH_TREE_H #include <cassert> #include <iostream> using namespace std; template <typename C> class BinarySearchTree { public: BinarySearchTree( ) : root{ nullptr } { } ~BinarySearchTree( ) { makeEmpty(); } const C & findMin( ) const { assert(!isEmpty()); return findMin( root )->element; } const C & findMax( ) const...

  • Here you'll write the inorder traversal function in the header file "bst.h". Notice that the public...

    Here you'll write the inorder traversal function in the header file "bst.h". Notice that the public inorder function calls a private recursive function _ inorder to do the actual traversal. This public-private strategy is the correct way to implement recursive functions, where the public function kicks off the recursion and the private function does the actual work. The public function is written for you, your job is to implement the private _ inorder function. he main program has been written...

  • In C++ I need the printRange function, and the main.cpp program. Thanks. (Binary search tree) Write...

    In C++ I need the printRange function, and the main.cpp program. Thanks. (Binary search tree) Write a function printRange that takes as input a binary search tree t and two keys, k1 and k2, which are ordered so that k1 < k2, and print all elements x in the tree such that k1 <= x <= k2. You can add this function in BinarySearchTree.h (click the link) that we used in the lecture and lab 7. public: void printRange(int k1,...

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

  • Having code issues wth my C++ program. My program checks if two binary trees are similar...

    Having code issues wth my C++ program. My program checks if two binary trees are similar and if they're not they return false. My program is return true with different binary trees. Could use some help thanks #include <iostream> #include <string> using namespace std; //Struct of Nodes struct BinarySearchTree { int data; BinarySearchTree *left; BinarySearchTree *right; }; // Inserting nodes into BST BinarySearchTree* insert( BinarySearchTree* node, int val) { if (node == NULL) { BinarySearchTree *newNode = new BinarySearchTree(); newNode->data...

  • Take the following code for a binary source tree and make it include the following operations....

    Take the following code for a binary source tree and make it include the following operations. bool replace(const Comparable & item, const Comparable & replacementItem); int getNumberOfNodes() const; (a) The replace method searches for the node that contains item in a binary search tree, if it is found, it replaces item with replacementItem. The binary tree should remain as a binary search tree after the replacement is done. Add the replace operation to the BinarySearchTree class. Test your replace using...

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

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