Question
Need Help implementing the Bold functions feel free to ask any necessary question *****************************************bst.h********************************************* #include #include using namespace std; class treenode { public: string info;
Need Help implementing the Bold functions feel free to ask any necessary question
*****************************************bst.h*********************************************
#include
using namespace std;
class treenode { public: string info; treenode *lchild, *rchild; };
class bst { public: bst();//store the data file (county_data.txt) into initial bst ~bst();//de-allocates dynamic memory allocate for tree bool empty();// checks to see if the tree is empty
void insert(string item); //inserts a county record into bst based on county_name. void insert(treenode * &, string item);
void del_name(string item); //deletes a county record based on county name. This function is called from main. Remember the tree is sorted (based) on county name void del_name(treenode * & loc_ptr, string item); //is an auxiliary function to help delete a county record based on county name. It is a member function that can only be called by another member of the class.
treenode * search_tree(treenode *, string item);//returns pointer to node with county name treenode * search_tree(string item);
treenode * inorder_succ(treenode *); //return pointer to inorder successor (based on population size) treenode * parent(); void print_tree(); void print_tree(treenode *);
// NEED HELP IMPLEMENTING
void population_ranges(const double& min_size, const double & max_size); //accepts two values that represents a population size range and prints all the counties with a population size in the range void sorted_info(); //This function will print the county information (name and population) to the file county_info.txt.
private: treenode *root;
};
*****************************************bst.cpp*********************************************
#include "bst.h"
bool bst::empty() { return (root == 0); }
void bst::insert(string item) {
insert(root, item); }
void bst::insert(treenode * & loc_ptr, string item) { if (loc_ptr == 0) { loc_ptr = new treenode; loc_ptr->lchild = loc_ptr->rchild = 0; loc_ptr->info = item; } else if (loc_ptr->info>item) insert(loc_ptr->lchild, item); else if (loc_ptr->info
treenode * bst::search_tree(string item) { return search_tree(root, item); }
treenode * bst::search_tree(treenode * loc_ptr, string item) { if (loc_ptr != 0) { if (loc_ptr->info == item) return loc_ptr; else if (loc_ptr->info>item) return search_tree(loc_ptr->lchild, item); else return search_tree(loc_ptr->rchild, item); } else return loc_ptr; }
void bst::del_name(string item) { del_name(root, item); }
void bst::del_name(treenode * & loc_ptr, string item) {
if (loc_ptr == 0) cout << "item not in tree ";
else if (loc_ptr->info > item) del_name(loc_ptr->lchild, item);
else if (loc_ptr->info < item) del_name(loc_ptr->rchild, item);
else { treenode * ptr;
if (loc_ptr->lchild == 0) { ptr = loc_ptr->rchild; delete loc_ptr; loc_ptr = ptr; } else if (loc_ptr->rchild == 0) { ptr = loc_ptr->lchild; delete loc_ptr; loc_ptr = ptr; } else { ptr = inorder_succ(loc_ptr); loc_ptr->info = ptr->info; del_name(loc_ptr->rchild, ptr->info); } }
}
treenode * bst::inorder_succ(treenode * loc_ptr) {
treenode *ptr = loc_ptr->rchild;
while (ptr->lchild != 0) { ptr = ptr->lchild; } return ptr; }
void bst::print_tree() { print_tree(root); }
void bst::print_tree(treenode * loc_ptr) { if (loc_ptr != 0) { print_tree(loc_ptr->lchild); cout << loc_ptr->info << endl; print_tree(loc_ptr->rchild); } }
bst::~bst() { while (root != 0) { del_name(root->info); } }
*****************************************bst_driver.cpp*********************************************
#include "bst.h"
int main() {
cout << "Test1: default constructor "; bst myTree;
myTree.print_tree(); cout << "End Test1 ";
cout << "Test2:insert "; myTree.insert("new-county", 234658); myTree.print_tree(); cout << "End Test2 ";
cout << "Test3: population_ranges "; myTree.population_ranges(0, 50000); cout << "End Test3 ";
cout << "Test4: del_name "; myTree.del_name("miami-dade"); cout << "End Test5 ";
cout << "Test5: sorted_info "; myTree.sorted_info(); cout << "End Test6 ";
return 0; }
*******************************county_info.txt*****************************
Alachua 249365 Baker 27154 Bay 169856 Bradford 28255 Brevard 543566 Broward 1780172 Calhoun 14750 Charlotte 160511 Citrus 140031 Clay 192970 Collier 328134 Columbia 67485 DeSoto 34894 Dixie 16486 Duval 870709 Escambia 299114 Flagler 97376 Franklin 11596 Gadsden 46151 Gilchrist 17004 Glades 12635 Gulf 15844 Hamilton 14671 Hardee 27887 Hendry 39089 Hernando 173094 Highlands 98630 Hillsborough 1267775 Holmes 19873 Indian River 138894 Jackson 49292 Jefferson 14658 Lafayette 8942 Lake 301019 Lee 631330 Leon 277971 Levy 40156 Liberty 8314 Madison 19115 Manatee 327142 Marion 332529 Martin 147495 Miami-Dade 2662874 Monroe 73873 Nassau 74195 Okaloosa 183482 Okeechobee 40140 Orange 1169107 Osceola 276163 Palm Beach 1335187 Pasco 466457 Pinellas 917398 Polk 609492 Putnam 74041 Santa Rosa 154104 Sarasota 38213 Seminole 425071 St. Johns 195823 St. Lucie 280379 Sumter 97756 Suwannee 41972 Taylor 22691 Union 15388 Volusia 494804 Wakulla 30978 Walton 55793 Washington 24935
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started