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

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

  • I need help in C++ implementing binary search tree. I have the .h file for the...

    I need help in C++ implementing binary search tree. I have the .h file for the binary search tree class. I have 4 classic texts, and 2 different dictionaries. Classic Texts: Alice In Wonderland.txt A Tale of Two Cities.txt Pride And Prejudice.txt War and Peace.txt 2 different dictionaries: Dictionary.txt Dictionary-brit.txt The data structures from the standard template library can not be used.The main program should open the text file, read in the words, remove the punctuation and change all the...

  • Consider the class specifications for the Binary Tree class and BinarySearch Tree class below: // Binary...

    Consider the class specifications for the Binary Tree class and BinarySearch Tree class below: // Binary Tree.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 Binary Tree { protected: TreeNode<elemType> *root; public: BinaryTree(); BinaryTreel const BinaryTree<elemType>& otherTree); -Binary Tree(): bool is Empty() const; virtual bool search(const elemTypes searchItem) const = 0; virtual void insert(const elemTypek insertItem) =...

  • C++ Binary Search Tree question. I heed help with the level 2 question please, as level...

    C++ Binary Search Tree question. I heed help with the level 2 question please, as level 1 is already completed. I will rate the answer a 100% thumbs up. I really appreciate the help!. Thank you! searching.cpp #include <getopt.h> #include <iostream> #include <sstream> #include <stdlib.h> #include <unistd.h> using namespace std; // global variable for tree operations // use to control tree maintenance operations enum Mode { simple, randomised, avl } mode; // tree type // returns size of tree //...

  • C++ Binary Search Tree question. I heed help with the level 2 question please, as level...

    C++ Binary Search Tree question. I heed help with the level 2 question please, as level 1 is already completed. I will rate the answer a 100% thumbs up. I really appreciate the help!. Thank you! searching.cpp #include <getopt.h> #include <iostream> #include <sstream> #include <stdlib.h> #include <unistd.h> using namespace std; // global variable for tree operations // use to control tree maintenance operations enum Mode { simple, randomised, avl } mode; // tree type // returns size of tree //...

  • Need this in C++ Goals: Your task is to implement a binary search tree of linked...

    Need this in C++ Goals: Your task is to implement a binary search tree of linked lists of movies. Tree nodes will contain a letter of the alphabet and a linked list. The linked list will be an alphabetically sorted list of movies which start with that letter. MovieTree() ➔ Constructor: Initialize any member variables of the class to default ~MovieTree() ➔ Destructor: Free all memory that was allocated void printMovieInventory() ➔ Print every movie in the data structure in...

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