(Solved) : Partial Implementation Binary Search Tree Class Given Add Public Member Function Class Re Q37065133 . . .
A partial implementation of a Binary Search Tree class is givenbelow. Add a public member function to this class that returns thesmallest value in the tree. You may add private helper functions ifthat makes your solution easier.
So i’m currently confused, I know the BST stores a pair whichconsists of the key and value but i have no idea how to return thesmallest value. Since this is being organized by the key thenwouldn’t it be possible for the values associated with the key berandomly sorted? (i.e <key, value> (a, 15) (b, 10)) ioriginally did it by iterating through the nodes until it reachedthe node farthest to the left since it’s a binary search tree but ican’t for the life of me figure out if that would give the smallestvalue. Smallest key sure, but not the value.
struct node {
Node(char key, int value, Node* parent)
: key_(key), value_(value), parent_(parent),
left_(nullptr), right_(nullptr) {}
char key_;
int value_;
Node* left_;
Node* parent_;
Node* right_;
};
class BinarySearchTree {
private:
Node* root_;
public:
int GetSmallest() {
//Answer Here
Node* currNode = root_;
while(currNode->left_ != nullptr) {
currNode = currNode->left_;
}
return currNode->key;
}
bool insert(char key, int value)
{
Node* node;
bool success = true;
if (root_ == nullptr) {
root_ = new Node(key, value, nullptr);
}
else
{
node = root_;
while (true) {
if (key < node->key_) {
if (node->left_ == nullptr) {
node->left_ = new Node(key, value, node);
break;
} else {
node = node->left_;
}
} else if (key > node->key_) {
if (node->right_ == nullptr) {
node->right_ = new Node(key, value, node);
break;
} else {
node = node->right_;
}
} else {
success = false;
break;
}
}
return(success);
}
};
Expert Answer
Answer to Partial Implementation Binary Search Tree Class Given Add Public Member Function Class Re Q37065133 . . .
OR