template?<?typename? ?K?, ?typename? ?V? > struct? ?Node { Node() : parent_(0),
ID: 3713128 • Letter: T
Question
template?<?typename? ?K?, ?typename? ?V? >
struct? ?Node
{
Node() : parent_(0), left_(0), right_(0) { }
bool? IsRoot() ?const? { ?return?(parent_ == NULL); }
bool? IsExternal() ?const? { ?return?((left_ == NULL) && (right_ == NULL)); }
K? key_;
Node?* left_;
Node?* parent_;
Node?* right_;
V? value_;
};
template?<?typename? ?K?, ?typename? ?V? >
class? ?BinarySearchTree
{
public? :
bool? Find(?const? ?K?& ?key?);
int? size(); // NEW MEMBER FUNCTION TO BE IMPLEMENTED
private? :
Node?<?K?, ?V?>* Finder(?const? ?K?& ?key?, ?Node?<?K?, ?V?>* ?node?);
Node?<?K?, ?V?>* root_;
// DECLARE ANY HELPER FUNCTIONS
};
template?<?typename? ?K?, ?typename? ?V? >
bool? ?BinarySearchTree?<?K?, ?V?>::Find(?const? ?K?& ?key?) {
Node?<?K?, ?V?>* node = Finder(?key?, root_);
if? (!node->IsExternal()) ?// found ?
return? ?true? ;
else? ?// not found
?return? ?false? ;
}
template?<?typename? ?K?, ?typename? ?V? >
Node?<?K?, ?V?>* ?BinarySearchTree?<?K?, ?V?>::Finder(?const? ?K?& ?key?, ?Node?<?K?, ?V?>* ?node?) {
if? (?node?->IsExternal())
?return?(?node?);
if? (?key? == ?node?->key_)
?return?(?node?);
else? ?if? (?key? < ?node?->key_)
?return?(Finder(?key?, ?node?->left_));
else
?return?(Finder(?key?, ?node?->right_));
}
//new function to be implemented
template?<?typename? ?K?, ?typename? ?V? >
int BinarySearchTree::size() { }
Space for any helper functions:
Explanation / Answer
int BinarySearchTree::size() {
}
The above code shows a recursive funtion that calculates the size of the BST. It starts with the Root node and goes on traversing through the tree and returns it's size. The size() function calls itself recursively untill it has traversed each node and thus returns the size of the BST.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.