void BinarySearchTree::fromArray(std::vector<unsigned int> vecArray, bool bLeftBranch, std::shared_ptr<BinaryTree> btParent, unsigned int unSmallestParentKey, bool bEverBranchedLeft) { if(btParent && bLeftBranch) { unSmallestParentKey = std::min(unSmallestParentKey, btParent->key()); } else if(!btParent) { unSmallestParentKey = 0; for(unsigned int unI : vecArray) { if(unI > unSmallestParentKey) { unSmallestParentKey = unI; } } } this->setKey(vecArray[0]); // Find next left (next smaller key than this) for(unsigned int unI = 1; unI < vecArray.size(); unI++) { if(vecArray[unI] < this->key()) { std::vector<unsigned int> vecArrayCopy(vecArray.begin() + unI, vecArray.end()); this->left(true)->fromArray(vecArrayCopy, true, this->shared_from_this(), unSmallestParentKey, true); break; } } // Find next right (next bigger key than this, no larger than smallest parent key when last branching left, smaller than parent if in parent's left branch) for(unsigned int unI = 1; unI < vecArray.size(); unI++) { bool bSmallerThanParentOK = (btParent ? vecArray[unI] < btParent->key() : true); bool bNoLargerThanLastLeftBranchParent = (bEverBranchedLeft ? vecArray[unI] < unSmallestParentKey : true); if(vecArray[unI] > this->key() && (!bLeftBranch || (bLeftBranch && bSmallerThanParentOK)) && bNoLargerThanLastLeftBranchParent) { std::vector<unsigned int> vecArrayCopy(vecArray.begin() + unI, vecArray.end()); this->right(true)->fromArray(vecArrayCopy, false, this->shared_from_this(), unSmallestParentKey, bEverBranchedLeft); break; } } }
int main() { std::vector <int> vecIntegers; // Instantiate a vector with 10 elements (it can grow larger) std::vector <int> vecWithTenElements (10); // Instantiate a vector with 10 elements, each initialized to 90 std::vector <int> vecWithTenInitializedElements (10, 90); // Instantiate one vector and initialize it to the contents of another std::vector <int> vecArrayCopy (vecWithTenInitializedElements); // Using iterators instantiate vector to 5 elements from another std::vector <int> vecSomeElementsCopied (vecWithTenElements.cbegin() , vecWithTenElements.cbegin() + 5); return 0; }