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 a client program of BinarySearchTree class.
(b) Implement the getNumberOfNodes method in the BinarySearchTree class. Test your implementation of getNumberOfNodes using a client program of BinarySearchTree class.
#include <iostream>
using namespace std;
template<typename Comparable>
class BinarySearchTree
{
public:
BinarySearchTree()
{
root =
NULL;
}
BinarySearchTree(const
BinarySearchTree & rhs);
~BinarySearchTree()
{
makeEmpty();
}
const Comparable & findMin()
const
{
return
findMin(root);
}
const Comparable & findMax()
const
{
return
findMax(root);
}
bool contains(const Comparable
& x) const
{
return
contains(x, root);
}
bool isEmpty() const
{
return ((root ==
NULL) ? true : false);
}
void printTree(ostream & out =
cout) const
{
out <<
"Start Traversing" << endl;
if
(isEmpty())
out << "Empty tree" << endl;
else
printTree(root, out);
out <<
"End Traversing" << endl;
}
void makeEmpty()
{
makeEmpty(root);
}
void insert(const Comparable &
x)
{
insert(x,
root);
}
void remove(const Comparable &
x)
{
remove(x,
root);
}
const BinarySearchTree &
operator=(const BinarySearchTree & rhs)
{
if (this !=
&rhs)
{
makeEmpty();
root = clone(rhs.root);
}
return
*this;
}
private:
struct BinaryNode
{
Comparable element;
BinaryNode *left;
BinaryNode *right;
BinaryNode(const Comparable & theElement,
BinaryNode *lt, BinaryNode *rt) :
element(theElement), left(lt), right(rt)
{
}
};
BinaryNode *root;
void insert(const Comparable &
x, BinaryNode * & t)
{
if (t ==
NULL)
t = new BinaryNode(x, NULL, NULL);
else if (x <
t->element)
insert(x, t->left);
else if
(t->element < x)
insert(x, t->right);
else
; // Duplicate; do nothing
}
void remove(const Comparable &
x, BinaryNode * & t)
{
if (t ==
NULL)
return; // Item not found; do nothing
if (x <
t->element)
remove(x, t->left);
else if
(t->element < x)
remove(x, t->right);
else if
(t->left != NULL && t->right != NULL) // Two
children
{
t->element =
findMin(t->right)->element;
remove(t->element, t->right);
}
else
{
BinaryNode *oldNode = t;
t = (t->left != NULL) ? t->left :
t->right;
delete oldNode;
}
}
BinaryNode * findMin(BinaryNode *t)
const
{
if (t ==
NULL)
return NULL;
if (t->left
== NULL)
return t;
return
findMin(t->left);
}
BinaryNode * findMax(BinaryNode *t)
const
{
if (t !=
NULL)
while (t->right != NULL)
t = t->right;
return t;
}
bool contains(const Comparable
& x, BinaryNode *t) const
{
if (t ==
NULL)
return false;
else if (x <
t->element)
return contains(x, t->left);
else if
(t->element < x)
return contains(x, t->right);
else
return true; // Match
}
void makeEmpty(BinaryNode * &
t)
{
if (t !=
NULL)
{
makeEmpty(t->left);
makeEmpty(t->right);
delete t;
}
t = NULL;
}
int height(BinaryNode *t)
{
if (t ==
NULL)
return -1;
else
return 1 + max(height(t->left),
height(t->right));
}
void printTree(BinaryNode *t,
ostream & out) const
{
if (t !=
NULL)
{
// out << t->element <<
"\n";
printTree(t->left, out);
// out << t->element <<
"\n";
printTree(t->right, out);
out << t->element << "\n";
}
}
BinaryNode * clone(BinaryNode *t)
const
{
if (t ==
NULL)
return NULL;
return new
BinaryNode(t->element, clone(t->left),
clone(t->right));
}
};
int main()
{
cout << "Starting!" << endl;
BinarySearchTree<int> myTree;
myTree.insert(11);
cout << "Inserted 11" << endl;
myTree.insert(12);
cout << "Inserted 12" << endl;
myTree.insert(6);
cout << "Inserted 6" << endl;
myTree.insert(8);
cout << "Inserted 8" << endl;
myTree.insert(7);
cout << "Inserted 7" << endl;
myTree.insert(9);
cout << "Inserted 9" << endl;
myTree.insert(2);
cout << "Inserted 2" << endl;
myTree.insert(4);
cout << "Inserted 4" << endl;
myTree.insert(10);
cout << "Inserted 10" << endl;
myTree.insert(5);
cout << "Inserted 5" << endl;
myTree.insert(14);
cout << "Inserted 14" << endl;
myTree.insert(13);
cout << "Inserted 13" << endl;
myTree.printTree(cout);
myTree.remove(13);
cout << "Removed 13" << endl;
myTree.printTree(cout);
myTree.remove(5);
cout << "Inserted 5" << endl;
myTree.printTree(cout);
cout << "Ending!" << endl;
return 0;
#include <iostream>
using namespace std;
template<typename Comparable>
class BinarySearchTree
{
public:
BinarySearchTree()
{
root = NULL;
}
BinarySearchTree(const BinarySearchTree & rhs);
~BinarySearchTree()
{
makeEmpty();
}
const Comparable & findMin() const
{
return findMin(root);
}
const Comparable & findMax() const
{
return findMax(root);
}
bool contains(const Comparable & x) const
{
return contains(x, root);
}
bool isEmpty() const
{
return ((root == NULL) ? true : false);
}
bool replace(const Comparable & item, const Comparable & replacementItem){
return replace(item,replacementItem, root);
}
int getNumberOfNodes() const{
return getNumberOfNodes(root);
}
void printTree(ostream & out = cout) const
{
out << "Start Traversing" << endl;
if (isEmpty())
out << "Empty tree" << endl;
else
printTree(root, out);
out << "End Traversing" << endl;
}
void makeEmpty()
{
makeEmpty(root);
}
void insert(const Comparable & x)
{
insert(x, root);
}
void remove(const Comparable & x)
{
remove(x, root);
}
const BinarySearchTree & operator=(const BinarySearchTree & rhs)
{
if (this != &rhs)
{
makeEmpty();
root = clone(rhs.root);
}
return *this;
}
private:
struct BinaryNode
{
Comparable element;
BinaryNode *left;
BinaryNode *right;
BinaryNode(const Comparable & theElement, BinaryNode *lt, BinaryNode *rt) :
element(theElement), left(lt), right(rt)
{
}
};
BinaryNode *root;
void insert(const Comparable & x, BinaryNode * & t)
{
if (t == NULL)
t = new BinaryNode(x, NULL, NULL);
else if (x < t->element)
insert(x, t->left);
else if (t->element < x)
insert(x, t->right);
else
; // Duplicate; do nothing
}
void remove(const Comparable & x, BinaryNode * & t)
{
if (t == NULL)
return; // Item not found; do nothing
if (x < t->element)
remove(x, t->left);
else if (t->element < x)
remove(x, t->right);
else if (t->left != NULL && t->right != NULL) // Two children
{
t->element = findMin(t->right)->element;
remove(t->element, t->right);
}
else
{
BinaryNode *oldNode = t;
t = (t->left != NULL) ? t->left : t->right;
delete oldNode;
}
}
BinaryNode * findMin(BinaryNode *t) const
{
if (t == NULL)
return NULL;
if (t->left == NULL)
return t;
return findMin(t->left);
}
BinaryNode * findMax(BinaryNode *t) const
{
if (t != NULL)
while (t->right != NULL)
t = t->right;
return t;
}
bool contains(const Comparable & x, BinaryNode *t) const
{
if (t == NULL)
return false;
else if (x < t->element)
return contains(x, t->left);
else if (t->element < x)
return contains(x, t->right);
else
return true; // Match
}
void makeEmpty(BinaryNode * & t)
{
if (t != NULL)
{
makeEmpty(t->left);
makeEmpty(t->right);
delete t;
}
t = NULL;
}
int height(BinaryNode *t)
{
if (t == NULL)
return -1;
else
return 1 + max(height(t->left), height(t->right));
}
bool replace(const Comparable & item, const Comparable & replacementItem, BinaryNode *t){
if (t == NULL)
return false;
else if (item < t->element)
return replace(item, replacementItem,t->left);
else if (t->element < item)
return replace(item, replacementItem,t->right);
else{
t->element = replacementItem;
return true; // Match
}
}
int getNumberOfNodes(BinaryNode *t) const{
if (t == NULL)
return 0;
else
return 1 + getNumberOfNodes(t->left) + getNumberOfNodes(t->right);
}
void printTree(BinaryNode *t, ostream & out) const
{
if (t != NULL)
{
// out << t->element << "\n";
printTree(t->left, out);
// out << t->element << "\n";
printTree(t->right, out);
out << t->element << "\n";
}
}
BinaryNode * clone(BinaryNode *t) const
{
if (t == NULL)
return NULL;
return new BinaryNode(t->element, clone(t->left), clone(t->right));
}
};
int main()
{
cout << "Starting!" << endl;
BinarySearchTree<int> myTree;
myTree.insert(11);
cout << "Inserted 11" << endl;
myTree.insert(12);
cout << "Inserted 12" << endl;
myTree.insert(6);
cout << "Inserted 6" << endl;
myTree.insert(8);
cout << "Inserted 8" << endl;
myTree.insert(7);
cout << "Inserted 7" << endl;
myTree.insert(9);
cout << "Inserted 9" << endl;
myTree.insert(2);
cout << "Inserted 2" << endl;
myTree.insert(4);
cout << "Inserted 4" << endl;
myTree.insert(10);
cout << "Inserted 10" << endl;
myTree.insert(5);
cout << "Inserted 5" << endl;
myTree.insert(14);
cout << "Inserted 14" << endl;
myTree.insert(13);
cout << "Inserted 13" << endl;
myTree.printTree(cout);
myTree.remove(13);
cout << "Removed 13" << endl;
myTree.printTree(cout);
myTree.remove(5);
cout << "Removed 5" << endl;
myTree.printTree(cout);
cout << "Number of Nodes: "<<myTree.getNumberOfNodes() <<endl;
myTree.replace(10,100);
cout << "Replaced 10 with 100" << endl;
myTree.printTree(cout);
cout << "Ending!" << endl;
return 0;
}
====================================================
DONE WITH BOTH THE METHOD, PLEASE CHECK, PLEASE UPVOTE
SEE OUTPUT

PLEASE COMMENT if there is any concern.
==========================================
Take the following code for a binary source tree and make it include the following operations....
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,...
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...
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...
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() {...
After the header, each line of the database file rebase210.txt contains the name of a restriction enzyme and possible DNA sites the enzyme may cut (cut location is indicated by a ‘) in the following format: enzyme_acronym/recognition_sequence/…/recognition_sequence// For instance the first few lines of rebase210.txt are: AanI/TTA'TAA// AarI/CACCTGCNNNN'NNNN/'NNNNNNNNGCAGGTG// AasI/GACNNNN'NNGTC// AatII/GACGT'C// AbsI/CC'TCGAGG// AccI/GT'MKAC// AccII/CG'CG// AccIII/T'CCGGA// Acc16I/TGC'GCA// Acc36I/ACCTGCNNNN'NNNN/'NNNNNNNNGCAGGT// … That means that each line contains one enzyme acronym associated with one or more recognition sequences. For example on line 2:The enzyme acronym...
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...
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...
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;...
C++ Assignment Project 1 - NodeList Building upon the the ListNode/List code I would like you to extend the interface of a list to have these member functions as well. struct ListNode { int element; ListNode *next; } Write a function to concatenate two linked lists. Given lists l1 = (2, 3, 1)and l2 = (4, 5), after return from l1.concatenate(l2)the list l1should be changed to be l1 = (2, 3, 1, 4, 5). Your function should not change l2and...
C++ Assignment Project 1 - NodeList Building upon the the ListNode/List code I would like you to extend the interface of a list to have these member functions as well. struct ListNode { int element; ListNode *next; } Write a function to concatenate two linked lists. Given lists l1 = (2, 3, 1)and l2 = (4, 5), after return from l1.concatenate(l2)the list l1should be changed to be l1 = (2, 3, 1, 4, 5). Your function should not change l2and...