I have to update this assignment to store the output into a binary tree instead of a linked list(which it does now).

1 answer below »
I have to update this assignment to store the output into a binary tree instead of a linked list(which it does now).


#include #include #include #include #include #include using namespace std; struct person { int social; int birth_day; string first_name; string last_name; int zip_code; person(int ssn, int bd, string fn, string ln, int zip) { social = ssn; birth_day = bd; first_name = fn; last_name = ln; zip_code = zip; } }; struct link { person *info; link *next; link(person *i = NULL, link *n = NULL) { info = i; next = n; } }; void print_list(link *content) { while (content != NULL) { cout < content-="">info->social < "="" "="">< content-="">info->birth_day < "="" "="">< content-="">info->first_name < "="" "="">< content-="">info->last_name < "="" "="">< content-="">info->zip_code < "\n";="" content="content-">next; } } person *find(link *content, string f, string l) { while (content != NULL) { if (content->info->first_name == f && content->info->last_name == l) return content->info; content = content->next; } return NULL; } void save_to_file(link *content) { auto now = std::chrono::system_clock::now(); auto timestamp = std::chrono::duration_cast(now.time_since_epoch()); std::ostringstream filename; filename < "database_"="">< timestamp.count()="">< ".txt";="" std::ofstream="" file(filename.str());="" if="" (!file.is_open())="" {="" std::cerr="">< "error:="" could="" not="" open="" file="" for="" writing!"="">< std::endl;="" return;="" }="" while="" (content="" !="NULL)" {="" file="">< content-="">info->social < "="" "="">< content-="">info->birth_day < "="" "="">< content-="">info->first_name < "="" "="">< content-="">info->last_name < "="" "="">< content-="">info->zip_code < std::endl;="" content="content-">next; } file.close(); std::cout < "saved="" database="" to="" "="">< filename.str()="">< std::endl;="" }="" void="" sort_name(link="" *content)="" {="" if="" (content="=" null="" ||="" content-="">next == NULL) { return; } link *current = content; link *index = NULL; person *temp = NULL; while (current != NULL) { index = current->next; while (index != NULL) { if (current->info->last_name > index->info->last_name) { temp = current->info; current->info = index->info; index->info = temp; } else if (current->info->last_name == index->info->last_name && current->info->first_name > index->info->first_name) { temp = current->info; current->info = index->info; index->info = temp; } index = index->next; } current = current->next; } cout< "sorted="" list="" based="" on="" name.\n";="" save_to_file(content);="" }="" void="" delete_person(link="" *content,="" string="" f,="" string="" l)="" {="" link="" *prev="NULL;" link="" *curr="content;" while="" (curr="" !="NULL)" {="" if="" (curr-="">info->first_name == f && curr->info->last_name == l) { if (prev == NULL) { content = curr->next; } else { prev->next = curr->next; } delete curr->info; delete curr; cout < "person="" deleted="" successfully.\n";="" save_to_file(content);="" return;="" }="" prev="curr;" curr="curr-">next; } cout < "person="" not="" found="" with=""><><" and=""><><" in="" database.\n";="" }="" void="" find_all(link="" *content,="" string="" s)="" {="" while="" (content="" !="NULL)" {="" if="" (content-="">info->last_name == s) cout < content-="">info->social < "="" "="">< content-="">info->birth_day < "="" "="">< content-="">info->first_name < "="" "="">< content-="">info->last_name < "="" "="">< content-="">info->zip_code < "\n";="" content="content-">next; } } void find_zip(link *content, int a) { link *allzip = NULL; while (content != NULL) { if (content->info->zip_code == a) allzip = new link(content->info, allzip); content = content->next; } print_list(allzip); } void kill_zip(link *content, int zipcode) { if(content == NULL) { return; } while(content != NULL && content->info->zip_code == zipcode) { link *temp = content; content = content->next; delete temp; } link *curr = content; while (curr != NULL && curr->next != NULL) { if (curr->next->info->zip_code == zipcode) { link *temp = curr->next; curr->next = curr->next->next; delete temp; } else { curr = curr->next; } } cout < "person="" found="" with="">< zipcode="">< "got="" deleted.\n";="" save_to_file(content);="" }="" int="" find_oldest(link="" *content)="" {="" int="" oldest="content-">info->birth_day; while (content != NULL) { if (content->info->birth_day < oldest)="" oldest="content-">info->birth_day; content = content->next; } return oldest; } void sort_age(link *content) { bool swapped = true; while (swapped) { swapped = false; link* current = content; link* prev = nullptr; while (current != nullptr && current->next != nullptr) { if (current->info->birth_day < current-="">next->info->birth_day) { link* next = current->next; current->next = next->next; next->next = current; if (prev == nullptr) { content = next; } else { prev->next = next; } prev = next; swapped = true; } else { prev = current; current = current->next; } } } cout< "sorted="" list="" based="" on="" age.\n";="" save_to_file(content);="" }="" void="" read_file(string="" filename,="" link="" *content)="" {="" ifstream="" fin;="" fin.open(filename);="" int="" ssn,="" bd,="" zip;="" string="" fn,="" ln;="" if="" (fin.fail())="" {="" cout="">< "could="" not="" open="" file"="">< "\n";="" }="" else="" {="" while="" (fin="">> ssn >> bd >> fn >> ln >> zip) { person *p = new person(ssn, bd, fn, ln, zip); link *l = new link(p, content); content = l; } } fin.close(); link *all = NULL; while (true) { cout < "\n="" command="" list:="" "="">< "\n"="">< "find="" -="" displays="" all="" information="" about="" the="" named="" person\n"="">< "\n"="">< "all="" -="" displays="" all="" information="" about="" all="" people="" who="" have="" the="" given="" last="" name\n"="">< "\n"="">< "zip="" -="" make="" a="" new="" linked="" list="" of="" all="" people="" living="" in="" the="" given="" zip="" code,="" then="" displays="" the="" entire="" contents="" of="" that="" linked="" list.\n"="">< "\n"="">< "oldest="" -="" print="" the="" name="" and="" zipcode="" of="" the="" oldest="" person="" in="" the="" database.\n"="">< "\n"="">< "exit="" -="" exit="" from="" the="" program.\n"="">< "\n"="">< "delete - "delete="">
Answered 2 days AfterApr 24, 2023

Answer To: I have to update this assignment to store the output into a binary tree instead of a linked...

Sanskar answered on Apr 27 2023
20 Votes
SOLUTION.PDF

Answer To This Question Is Available To Download

Related Questions & Answers

More Questions »

Submit New Assignment

Copy and Paste Your Assignment Here