I have the following BST for reference. BST
Assume that min: 9 and max: 20 It satisfies all the criteria such that for each node (X), all the nodes in the left substree are smaller than, and all the nodes in the right substree are bigger than the value of X.
I'm having trouble creating a function (member function so it has access to the root node) that prints ALL the values. Specifically, say that my current Node is 10, but I would still need to check both the left and right subtree. I cannot pass the node in the parameter (otherwise I could do something like this?), so I have to implement this function
void printBetween(int min, int max);
Also, the function should only visits a subtree where the values may be valid.
Assume that the node struct looks like this:
struct Node{
T data_;
Node* left_;
Node* right_;
Node(const T& data, Node* left=nullptr, Node* right=nullptr){
data_=data;
left_=left;
right_=right;
}
};
How would I go about if both the left child and the right child could have a value between min and max?
void printBetween(int min, int max){
// left child: smaller than
// right child: bigger than
// This function prints every value in the tree that is between min and max inclusive.
if( root_ == nullptr)
cout << "No values found" << endl;
Node* currNode = root_;
// check until currNode is a valid node
while(currNode != nullptr){
// print value if currNode's data value is between min and max inclusive,
if(currNode->data_ > min && currNode->data_ <= max){
cout << "Value: " << data_ << "," << endl;
fnd = true;
// since it falls in the range, have to check both children
// not sure what to do here??
if(currNode != nullptr && currNode->left_->data_ > min && currNode->left_->data_ <= max){
currNode = currNode->left_;
} else if(currNode != nullptr && currNode->right_->data_ > min && currNode->right_->data_ <= max){
currNode = currNode->right_;
}
}
// current node's data is too big, so go to its left child
if(currNode->data_ > max){
currNode = currNode->left_;
}
// current node's data is too small, so go to its right child
else if(currNode->data_ < min){
currNode = currNode->right_;
}
}
}