Question: For the BST class provided, implement the findRangeSum() method that returns the sum of the key values of all the nodes in the BST Any

For the BST class provided, implement the findRangeSum() method that returns the sum of the key values of all the nodes in the BST

Any debugging print outs should be commented out

bst.cpp

//FortheBSTclassprovided,implementthefindRangeSum()methodthatreturnsthesumofkeysofallnodesfromlowtohigh(inclusiveoflowandhigh)

//UseadditionalSTLsandprivatemethodsasneeded.

//AssumeinputkeysexistintheBST

#include

usingnamespacestd;

/**********BSTClassdeclaration**************/

classNode

{

private:

intkey;

Node*left;

Node*right;

friendclassBinarySearchTree;

};

classBinarySearchTree

{

public:

BinarySearchTree();

voidinsert(intkey);//Recursive

intfindRangeSum(intlow,inthigh)const;//Returnsthesumofkeysofallnodesfromlowtohigh(inclusiveoflowandhigh)

private:

Node*root;

voidinsertHelper(Node*parent,Node*new_node);

};

/***********BSTClassimplemenation*********/

BinarySearchTree::BinarySearchTree()

{

root=nullptr;

}

voidBinarySearchTree::insert(intkey)

{

Node*new_node=newNode;

new_node->key=key;

new_node->left=nullptr;

new_node->right=nullptr;

if(root==nullptr){

root=new_node;

}else{

insertHelper(root,new_node);

}

}

voidBinarySearchTree::insertHelper(Node*parent,Node*new_node)

{

if(new_node->keykey){

if(parent->left==nullptr){

parent->left=new_node;

}

else{

insertHelper(parent->left,new_node);}

}

elseif(new_node->key>parent->key){

if(parent->right==nullptr){

parent->right=new_node;

}

else{

insertHelper(parent->right,new_node);

}

}

}

intBinarySearchTree::findRangeSum(intlow,inthigh)const

{

//Yourcodehere.FeelfreetoaddadditionalprivatemethodstotheBinarySearchTreeclass

}

/********************Test**************/

intmain()

{

BinarySearchTreet;

t.insert(5);

t.insert(3);

t.insert(7);

t.insert(2);

t.insert(4);

t.insert(6);

t.insert(8);

t.insert(9);

cout

return0;

}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Mathematics Questions!