MUST BE IN C++ Procedural Code Develop a C++ program that will recursively alpha
ID: 3689468 • Letter: M
Question
MUST BE IN C++ Procedural Code
Develop a C++ program that will recursively alphabetize a set of strings in a user-specified file using the tree sort algorithm explained in the lectures while maintaining a balanced binary tree at all times.
The input file will contain the follwoing:
Max Hank Jet Frisky Chata Richard Nan Sam Thomas Karen Gerri Ingrid Alan Dana
**When done print out the contents of the tree in inorder in a tabular format similar to the following**:
NODE
LEFT
RIGHT
HEIGHT
BALANCE
Clarise
null
null
1
0
Fred
Clarise
Henry
2
0
Henry
null
null
1
0
Jane
Fred
Nan
4
-1
Mark
null
null
1
0
Nan
Mark
Susan
3
-1
Ryan
null
null
1
0
Susan
Ryan
Tammy
2
0
Tammy
null
null
1
0
*************************************************************************************************************************
PLEASE USE COMMENTS SO I KNOW WHAT IS HAPPENING IN THE CODE! THANK YOU
NODE
LEFT
RIGHT
HEIGHT
BALANCE
Clarise
null
null
1
0
Fred
Clarise
Henry
2
0
Henry
null
null
1
0
Jane
Fred
Nan
4
-1
Mark
null
null
1
0
Nan
Mark
Susan
3
-1
Ryan
null
null
1
0
Susan
Ryan
Tammy
2
0
Tammy
null
null
1
0
Explanation / Answer
static treenode<RandomAccessIterator>* build(RandomAccessIterator first, RandomAccessIterator last)
{
treenode<RandomAccessIterator>* root = NULL;
for (RandomAccessIterator it = first; it < last; ++it) {
treenode<RandomAccessIterator>* node = new treenode<RandomAccessIterator>(*it);
if (root == NULL) {
root = node;
}
else {
treenode<RandomAccessIterator>* current = root,* previous = NULL;
bool less;
while (current != NULL) {
less = node->value_ < current->value_;
previous = current;
current = less ? current->left_ : current->right_;
}
if (less) {
previous->left_ = node;
}
else {
previous->right_ = node;
}
}
}
return root;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.