Last active
February 20, 2018 07:11
-
-
Save vardanator/c58059b7edb1af09b27b32fdbbea4e49 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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