Question

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>

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

void printInOrder() const; // Prints keys in-order. Recursive

string find(int key) const; //Returns value if node is present, else return

empty string. Iterative

private:

Node* root;

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

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

implemenation of printInroder()

};

#endif

bst_test.cpp

#include <iostream>

#include "bst.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");

t.printInOrder(); // Prints the keys in order (will appear sorted)

int ele = 8;

string val = t.find(ele);

if (val == "" ) {

cout << ele << " does not exist in tree" << endl;

} else {

cout << ele << " : " << val << endl;

}

ele = 0;

val = t.find(ele);

if (val == "" ) {

cout << ele << " does not exist in tree" << endl;

} else {

cout << ele << " : " << val << endl;

}

return 0;

}

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

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

void printInOrder() const; // Prints keys in-order. Recursive

string find(int key) const; //Returns value if node is present, else return empty string. Iterative

private:

Node* root;

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

void printInOrderHelper(Node *n) const; //Helper for recursive implemenation of printInroder()

};

#endif

//bst.cpp

#include"bst.h"
#include<iostream>
using namespace std;
BinarySearchTree::BinarySearchTree(){
   root = NULL;
}

void BinarySearchTree::insert(int key, string val){
   Node *newNode = new Node;
   newNode->key = key;
   newNode->val = val;
   newNode->left = NULL;
   newNode->right = NULL;
  
   root=insertHelper(root, newNode);
}

void BinarySearchTree::printInOrder() const{
   printInOrderHelper(root);
   cout<<endl;
} // Prints keys in-order. Recursive

string BinarySearchTree::find(int key) const{
   Node *head = root;
   while (head != NULL) {
// pass right subtree as new tree
if (key > head->key)
head = head->right;
  
// pass left subtree as new tree
else if (key < head->key)
head = head->left;
else
return head->val; // if the key is found return val
}
return "";
}

Node* BinarySearchTree::insertHelper(Node* parent, Node* new_node){
   if (parent == NULL) return new_node;

if (new_node->key < parent->key)
parent->left = insertHelper(parent->left, new_node);
else if (new_node->key > parent->key)
parent->right = insertHelper(parent->right, new_node);   

return parent;

}

void BinarySearchTree::printInOrderHelper(Node *n) const{
   if(n==NULL)return;
   printInOrderHelper(n->left);
   cout<<n->key<<" ";
   printInOrderHelper(n->right);
}

//bst_test.cpp

#include <iostream>

#include "bst.cpp"

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");

t.printInOrder(); // Prints the keys in order (will appear sorted)

int ele = 8;

string val = t.find(ele);

if (val == "" ) {

cout << ele << " does not exist in tree" << endl;

} else {

cout << ele << " : " << val << endl;

}

ele = 0;

val = t.find(ele);

if (val == "" ) {

cout << ele << " does not exist in tree" << endl;

} else {

cout << ele << " : " << val << endl;

}

return 0;

}

//sample output

Add a comment
Know the answer?
Add Answer to:
C++ (Using Binary Search Trees) other methods will result in downvote Implement the binary search tree...
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++, 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...

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

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

  • Consider the class specifications for the Binary Tree class and Binary Search Tree class in the...

    Consider the class specifications for the Binary Tree class and Binary Search Tree class in the attached files // BinaryTree.h #include <iostream> using namespace std; //Definition of the Node template <class elemType> struct TreeNode { elemType data; TreeNode<elemType> *left; TreeNode<elemType> *right; }; //Definition of class Binary Tree template <class elemType> class BinaryTree { protected: TreeNode<elemType> *root; public: BinaryTree(); BinaryTreel const BinaryTree<elemType>& otherTree); BinaryTree(); bool is Empty() const; virtual boot search(const elemType& searchItem) const = 0; virtual void insert(const elemType& insertItem)...

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

  • Hi there, I am working on a binary search tree code in c++. The program must...

    Hi there, I am working on a binary search tree code in c++. The program must store and update students' academic records, each node includes the student name, credits attempted, credits earned and GPA. I have made some progress with the code and written most of the functions in the .cpp file (already did the .h file) but i am struggling with what to put in the main and how to put an update part in the insert function. I...

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

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

  • Java binary search tree Add the following print method to the binary search tree class created...

    Java binary search tree Add the following print method to the binary search tree class created in class (on D2L). This method should print all the nodes in the tree in level order (root first, then all children of root, then all children of those). Ensure your method runs in O(N), include comments to show how it conforms to this rule. Method header: public void printInLevelOrder() public class BinarySearchTree<E extends Comparable<? super E>> { private Node root; public BinarySearchTree() {...

  • Binary Tree Template Write your own version of a class template that will create a binary...

    Binary Tree Template Write your own version of a class template that will create a binary tree that can hold values of any data type. Demonstrate the class with a driver program. Place your binary tree template in it's own header file, Btree.h. Include methods for the following: inserting new values into the tree removing nodes from the tree searching the tree returning the number of nodes in the tree displaying the contents of the tree using preorder traversal Your...

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