Skip to content

Instantly share code, notes, and snippets.

@vardanator
Last active February 20, 2018 07:11
Show Gist options
  • Select an option

  • Save vardanator/c58059b7edb1af09b27b32fdbbea4e49 to your computer and use it in GitHub Desktop.

Select an option

Save vardanator/c58059b7edb1af09b27b32fdbbea4e49 to your computer and use it in GitHub Desktop.
template <typename BlaBla>
class BST
{
public:
// other code ...
vector<BlaBla*> InOrderProduceVector()
{
vector<BlaBla*> result;
result.reserve(1000); // magic number, reserving a space to avoid reallocation on inserts
InOrderProduceVectorHelper_(root_, result); // passing vector by reference
return result;
}
protected:
// takes a reference to vector
void InOrderProduceVectorHelper_(BSTNode* node, vector<BlaBla*>& destination)
{
if (!node) return;
InOrderProduceVectorHelper_(node->left, destination);
destination.push_back(node->item);
InOrderProduceVectorHelper_(node->right, destination);
}
private:
BSTNode* root_;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment