Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

22.4 Generate a Balanced BST Given a sorted vector, generate a binary search tree (BST) with minimal height BST. Feel free to write other helper

22.4 Generate a Balanced BST

Given a sorted vector, generate a binary search tree (BST) with minimal height BST. Feel free to write other helper functions if you need.

Please put the answer in BOLD.

#include #include using namespace std;

template struct TreeNode{ T data; TreeNode * right; TreeNode * left; };

template class Tree{ public: void createTree(const vector& sortedVec); // Implement this below void fillVectorWithInorderTraverse(vector& vec); int height(); private: int height(TreeNode * node); TreeNode* root; void fillVectorWithInorderTraverse(TreeNode* treeNode,vector& vec); };

template int Tree::height(){ return height(root); }

template int Tree::height(TreeNode * node){ if(node == NULL) return -1; return max(height(node->left), height(node->right)) + 1; }

template void Tree::fillVectorWithInorderTraverse(vector& vec){ if(root == NULL) return; fillVectorWithInorderTraverse(root, vec); }

template void Tree::fillVectorWithInorderTraverse(TreeNode* treeNode,vector& vec){ if(treeNode == NULL) return; fillVectorWithInorderTraverse(treeNode->left, vec); vec.push_back(treeNode->data); fillVectorWithInorderTraverse(treeNode->right, vec); }

template void Tree::createTree(const vector& sortedVec) { // IMPLEMENT HERE }

int main() {

return 0; }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions

Question

LO5 Illustrate the steps in developing a base pay system.

Answered: 1 week ago

Question

LO3 Outline strategic compensation decisions.

Answered: 1 week ago