Use the text Binary Search Tree class as a starter, and add an iterator so my te
ID: 663297 • Letter: U
Question
Use the text Binary Search Tree class as a starter, and add an iterator so my test code can iterate linearly through a tree (without recursion). Specifically, I require that you remove all uses of the Class or any other STL auxiliary storage. The iterator actually iterates through the actual tree. Limit the #include lines to only and Submit one file, named BST.h A possible starter code from the text is BST.h http://bit.ly/1L9QUqs
#include "BST.h"
using namespace std;
// CS212, Spring 2014, Program #5
void main()
{
// Create a binary search tree for strings
BST tree;
// Add elements to the tree
tree.insert("America");
tree.insert("Canada");
tree.insert("Russia");
tree.insert("France");
// Create an Iterator
Iterator iterator = tree.begin();
tree.insert("Texas"); // modify the tree
// Traverse a binary tree using iterators
while (iterator != tree.end())
{
cout << *iterator << " ";
iterator++;
}
// Texas is missing, FIX IT!!!!
system("pause");
}
Explanation / Answer
struct BST{
vector<string> myvec ("",2000);
int maximum = -1;
void insert(string s){
int i = 1;
string temp = myvec[i-1];
while (temp != ""){
if (temp > s)
i = i * 2;
else
i = i*2 + 1;
temp = myvec[i];
}
myvec[i] = s;
maximum = max(i,maximum);
}
vector<int>::iterator begin(){
return myvector.begin();
}
vector<int>::iterator end(){
return myvector.end();
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.