need comments for the following code: Node* BSTree::Search_State(string s,Node*
ID: 3826760 • Letter: N
Question
need comments for the following code:
Node* BSTree::Search_State(string s,Node* node){
if(node==NULL) return NULL;
else{
if(node->getdata()->getState()==s) return node;
return Search_State(s,node->Left());
return Search_State(s,node->Right());
}
return NULL;
}
Node* BSTree::Search_Zip_code(string s,Node* node){
if(node==NULL) return NULL;
else{
if(node->getdata()->getZip_code()==s) return node;
return Search_Zip_code(s,node->Left());
return Search_Zip_code(s,node->Right());
}
return NULL;
}
Node* BSTree::Search_Country(string s,Node* node){
if(node==NULL) return NULL;
else{
if(node->getdata()->getCountry()==s) return node;
return Search_Country(s,node->Left());
return Search_Country(s,node->Right());
}
return NULL;
}
}
void Main::Add_Entry(){
if(head==NULL) {
cout<<"You not choice database!please choice database!"<<endl;
cout<<"Press any key to continue:"<<endl;
getchar();
getchar();
}
else{
Datatype *data=new Datatype();
Affiliates *affiliates=new(Affiliates);
affiliates->next=NULL;
affiliates->num=1;
cout<<"Input number:";
int number;
cin>>number;
data->setnumber(number);
cout<<"Input First name:";
string First_Name;
cin>>First_Name;
data->setFirst_name(First_Name);
cout<<"Input Middle name:";
string Middle_Name;
cin>>Middle_Name;
data->setMiddle_name(Middle_Name);
cout<<"Input Last_name:";
string Last_Name;
cin>>Last_Name;
data->setLast_name(Last_Name);
cout<<"Input Company_name:";
string Company_Name;
cin>>Company_Name;
data->setCompany_name(Company_Name);
cout<<"Input Home_phone:";
string Home_Phone;
cin>>Home_Phone;
data->setHome_phone(Home_Phone);
cout<<"Input Office_phone:";
string Office_Phone;
cin>>Office_Phone;
data->setOffice_phone(Office_Phone);
cout<<"Input Email:";
string email;
cin>>email;
data->setEmail(email);
cout<<"Input Mobile_number:";
string Mobile_Number;
cin>>Mobile_Number;
data->setMobile_number(Mobile_Number);
cout<<"Input Street_address:";
string Street_Address;
cin>>Street_Address;
data->setStreet_address(Street_Address);
cout<<"Input City:";
string city;
cin>>city;
data->setCity(city);
cout<<"Input State:";
string state;
cin>>state;
data->setState(state);
cout<<"Input Zip_code:";
string Zip_Code;
cin>>Zip_Code;
data->setZip_code(Zip_Code);
cout<<"Input Country:";
string country;
cin>>country;
data->setCountry(country);
cout<<"Input affiliates end with#:";
while(1){
string x;
cin>>x;
int i;
if(x=="#") break;
else{
int value=0;
for(i=0;i<x.length();i++){
if(x[i]==',') value++;
}
if(value<2) {
cout<<"Input error!Try agin!"<<endl;
}
else{
Affiliates *p;
Affiliates *tail=new(Affiliates);
tail->data=x;
tail->num=affiliates->num;
for(p=affiliates;p->next!=NULL;p=p->next);
tail->next=p->next;
p->next=tail;
affiliates->num++;
}
}
}
data->setaffiliates(affiliates);
head->addNode(data->getnumber());
head->findNode(data->getnumber(),head->Root())->setdata(data);
}
}
void Main::Find_Entry(){
cout<<"Choice exact or contains(1.extact/2.contains):";
string s;
while(cin>>s){
if(s=="1"){
int num;
cout<<"Input number:";
cin>>num;
Node *node=head->findNode(num,head->Root());
if(node==NULL){
cout<<"Not found this one!";
getchar();
getchar();
return;
}
cout<<"The Message:";
printNode(node);
break;
}
else if(s=="2"){
Fuzzy_Search();
break;
}
else{
cout<<"Input error! try again(1.extact/2.contains):";
}
}
cout<<"enter any to continue:";
getchar();
getchar();
}
void Main::Fuzzy_Search(){
string s;
while(1){
cout<<"Choice hou to search"<<endl;
cout<<"1:First_name"<<endl;
cout<<"2:Middle_name"<<endl;
cout<<"3:Last_name"<<endl;
cout<<"4:Company_name"<<endl;
cout<<"5:Home_phone"<<endl;
cout<<"6:Office_phone"<<endl;
cout<<"7:Email"<<endl;
cout<<"8:Mobile_number"<<endl;
cout<<"9:Street_address"<<endl;
cout<<"10:City"<<endl;
cout<<"11:State"<<endl;
cout<<"12:Zip_code"<<endl;
cout<<"13:Country"<<endl;
cin>>s;
if(s=="1"){
string x;
cout<<"Input First_name:";
cin>>x;
Node* node=head->Search_First_name(x,head->Root());
if(node==NULL) cout<<"not found"<<endl;
else {
printNode(node);
}
break;
}
else if(s=="2"){
string x;
cout<<"Input Middle_name:";
cin>>x;
Node* node=head->Search_Middle_name(x,head->Root());
if(node==NULL) cout<<"not found"<<endl;
else {
printNode(node);
}
break;
}
else if(s=="3"){
string x;
cout<<"Input Last_name:";
cin>>x;
Node* node=head->Search_Last_name(x,head->Root());
if(node==NULL) cout<<"not found"<<endl;
else {
printNode(node);
}
break;
}
else if(s=="4"){
string x;
cout<<"Input Company_name:";
cin>>x;
Node* node=head->Search_Company_name(x,head->Root());
if(node==NULL) cout<<"not found"<<endl;
else {
printNode(node);
}
break;
}
else if(s=="5"){
string x;
cout<<"Input Home_phone:";
cin>>x;
Node* node=head->Search_Home_phone(x,head->Root());
if(node==NULL) cout<<"not found"<<endl;
else {
printNode(node);
}
break;
}
else if(s=="6"){
string x;
cout<<"Input Office_phone:";
cin>>x;
Node* node=head->Search_Office_phone(x,head->Root());
if(node==NULL) cout<<"not found"<<endl;
else {
printNode(node);
}
break;
}
else if(s=="7"){
string x;
cout<<"Input Email:";
cin>>x;
Node* node=head->Search_Email(x,head->Root());
if(node==NULL) cout<<"not found"<<endl;
else {
printNode(node);
}
break;
}
else if(s=="8"){
string x;
cout<<"Input Mobile_number:";
cin>>x;
Node* node=head->Search_Mobile_number(x,head->Root());
if(node==NULL) cout<<"not found"<<endl;
else {
printNode(node);
}
break;
}
else if(s=="9"){
string x;
cout<<"Input Street_address:";
cin>>x;
Node* node=head->Search_Street_address(x,head->Root());
if(node==NULL) cout<<"not found"<<endl;
else {
printNode(node);
}
break;
}
else if(s=="10"){
string x;
cout<<"Input City:";
cin>>x;
Node* node=head->Search_City(x,head->Root());
if(node==NULL) cout<<"not found"<<endl;
else {
printNode(node);
}
break;
}
else if(s=="11"){
string x;
cout<<"Input State:";
cin>>x;
Node* node=head->Search_State(x,head->Root());
if(node==NULL) cout<<"not found"<<endl;
else {
printNode(node);
}
break;
}
else if(s=="12"){
string x;
cout<<"Input Zip_code:";
cin>>x;
Node* node=head->Search_Zip_code(x,head->Root());
if(node==NULL) cout<<"not found"<<endl;
else {
printNode(node);
}
break;
}
else if(s=="12"){
string x;
cout<<"Input Country:";
cin>>x;
Node* node=head->Search_Country(x,head->Root());
if(node==NULL) cout<<"not found"<<endl;
else {
printNode(node);
}
break;
}
else{
cout<<"Input error! try again:";
}
}
}
void Main::Edit_Entry(){
int num;
cout<<"Input the number:";
cin>>num;
Node* node=head->findNode(num,head->Root());
if(node==NULL) cout<<"not found!";
else{
cout<<"Input First name:";
string First_Name;
cin>>First_Name;
node->getdata()->setFirst_name(First_Name);
cout<<"Input Middle name:";
string Middle_Name;
cin>>Middle_Name;
node->getdata()->setMiddle_name(Middle_Name);
cout<<"Input Last_name:";
string Last_Name;
cin>>Last_Name;
node->getdata()->setLast_name(Last_Name);
cout<<"Input Company_name:";
string Company_Name;
cin>>Company_Name;
node->getdata()->setCompany_name(Company_Name);
cout<<"Input Home_phone:";
string Home_Phone;
cin>>Home_Phone;
node->getdata()->setHome_phone(Home_Phone);
cout<<"Input Office_phone:";
string Office_Phone;
cin>>Office_Phone;
node->getdata()->setOffice_phone(Office_Phone);
cout<<"Input Email:";
string email;
cin>>email;
node->getdata()->setEmail(email);
cout<<"Input Mobile_number:";
string Mobile_Number;
cin>>Mobile_Number;
node->getdata()->setMobile_number(Mobile_Number);
cout<<"Input Street_address:";
string Street_Address;
cin>>Street_Address;
node->getdata()->setStreet_address(Street_Address);
cout<<"Input City:";
string city;
cin>>city;
node->getdata()->setCity(city);
cout<<"Input State:";
string state;
cin>>state;
node->getdata()->setState(state);
cout<<"Input Zip_code:";
string Zip_Code;
cin>>Zip_Code;
node->getdata()->setZip_code(Zip_Code);
cout<<"Input Country:";
string country;
cin>>country;
node->getdata()->setCountry(country);
cout<<"Changed!";
}
cout<<"enter any to continue:";
getchar();
getchar();
}
void Main::Delete_Entry(){
cout<<"Input delete number:";
int num;
cin>>num;
head->deleteNode(num);
cout<<"Deleted!";
cout<<"enter any to continue:";
getchar();
getchar();
}
void Main::Save(){
cout<<"Input an address book file to save:";
string file;
cin>>file;
int i;
for(i=0;i<file.length();i++) url[i]=file[i];
url[file.length()]='';
out.open(url,ofstream::out);
print_file(head->Root());
out.close();
cout<<"save sucess enter any to continue:";
getchar();
getchar();
}
void Main::print_file(Node* node){
if(node==NULL) return;
else{
out<<node->Key()<<endl<<node->getdata()->getFirst_name()<<endl<<node->getdata()->getMiddle_name()<<endl<<node->getdata()->getLast_name()
<<endl<<node->getdata()->getCompany_name()<<endl<<node->getdata()->getHome_phone()<<endl<<node->getdata()->getOfiice_phone()
<<endl<<node->getdata()->getEmail()<<endl<<node->getdata()->getMobile_number()<<endl<<node->getdata()->getStreet_address()<<endl
<<node->getdata()->getCity()<<endl<<node->getdata()->getState()<<endl<<node->getdata()->getZip_code()<<endl<<node->getdata()->getCountry()
<<endl;
Affiliates *p=node->getdata()->getaffiliates();
p=p->next;
while(p!=NULL){
out<<p->data<<endl;
p=p->next;
}
out<<"|"<<endl;
print_file(node->Left());
print_file(node->Right());
}
}
Explanation / Answer
/* search for state in Tree by using node if it finds return result otherwise return null*/
Node* BSTree::Search_State(string s,Node* node){
if(node==NULL) return NULL;
else{
if(node->getdata()->getState()==s) return node;
return Search_State(s,node->Left());
return Search_State(s,node->Right());
}
return NULL;
/* search for ZipCode in Tree by using node if it finds return result otherwise return null*/
}
Node* BSTree::Search_Zip_code(string s,Node* node){
if(node==NULL) return NULL;
else{
if(node->getdata()->getZip_code()==s) return node;
return Search_Zip_code(s,node->Left());
return Search_Zip_code(s,node->Right());
}
return NULL;
/* search for Country in Tree by using node if it finds return result otherwise return null*/
}
Node* BSTree::Search_Country(string s,Node* node){
if(node==NULL) return NULL;
else{
if(node->getdata()->getCountry()==s) return node;
return Search_Country(s,node->Left());
return Search_Country(s,node->Right());
}
return NULL;
}
/* To Add the new record or Data (Comlete profile details) */
}
void Main::Add_Entry(){
if(head==NULL) {
cout<<"You not choice database!please choice database!"<<endl;
cout<<"Press any key to continue:"<<endl;
getchar();
getchar();
}
else{
Datatype *data=new Datatype();
Affiliates *affiliates=new(Affiliates);
affiliates->next=NULL;
affiliates->num=1;
cout<<"Input number:";
int number;
cin>>number;
data->setnumber(number);
cout<<"Input First name:";
string First_Name;
cin>>First_Name;
data->setFirst_name(First_Name);
cout<<"Input Middle name:";
string Middle_Name;
cin>>Middle_Name;
data->setMiddle_name(Middle_Name);
cout<<"Input Last_name:";
string Last_Name;
cin>>Last_Name;
data->setLast_name(Last_Name);
cout<<"Input Company_name:";
string Company_Name;
cin>>Company_Name;
data->setCompany_name(Company_Name);
cout<<"Input Home_phone:";
string Home_Phone;
cin>>Home_Phone;
data->setHome_phone(Home_Phone);
cout<<"Input Office_phone:";
string Office_Phone;
cin>>Office_Phone;
data->setOffice_phone(Office_Phone);
cout<<"Input Email:";
string email;
cin>>email;
data->setEmail(email);
cout<<"Input Mobile_number:";
string Mobile_Number;
cin>>Mobile_Number;
data->setMobile_number(Mobile_Number);
cout<<"Input Street_address:";
string Street_Address;
cin>>Street_Address;
data->setStreet_address(Street_Address);
cout<<"Input City:";
string city;
cin>>city;
data->setCity(city);
cout<<"Input State:";
string state;
cin>>state;
data->setState(state);
cout<<"Input Zip_code:";
string Zip_Code;
cin>>Zip_Code;
data->setZip_code(Zip_Code);
cout<<"Input Country:";
string country;
cin>>country;
data->setCountry(country);
cout<<"Input affiliates end with#:";
while(1){
string x;
cin>>x;
int i;
if(x=="#") break;
else{
int value=0;
for(i=0;i<x.length();i++){
if(x[i]==',') value++;
}
if(value<2) {
cout<<"Input error!Try agin!"<<endl;
}
else{
Affiliates *p;
Affiliates *tail=new(Affiliates);
tail->data=x;
tail->num=affiliates->num;
for(p=affiliates;p->next!=NULL;p=p->next);
tail->next=p->next;
p->next=tail;
affiliates->num++;
}
}
}
data->setaffiliates(affiliates);
head->addNode(data->getnumber());
head->findNode(data->getnumber(),head->Root())->setdata(data);
}
}
/* To find certain record or entry (or) search for certain record or entry */
void Main::Find_Entry(){
cout<<"Choice exact or contains(1.extact/2.contains):";
string s;
while(cin>>s){
if(s=="1"){
int num;
cout<<"Input number:";
cin>>num;
Node *node=head->findNode(num,head->Root());
if(node==NULL){
cout<<"Not found this one!";
getchar();
getchar();
return;
}
cout<<"The Message:";
printNode(node);
break;
}
else if(s=="2"){
Fuzzy_Search();
break;
}
else{
cout<<"Input error! try again(1.extact/2.contains):";
}
}
cout<<"enter any to continue:";
getchar();
getchar();
}
/* searching record based on certain single value by entering the choice */
void Main::Fuzzy_Search(){
string s;
while(1){
cout<<"Choice hou to search"<<endl;
cout<<"1:First_name"<<endl;
cout<<"2:Middle_name"<<endl;
cout<<"3:Last_name"<<endl;
cout<<"4:Company_name"<<endl;
cout<<"5:Home_phone"<<endl;
cout<<"6:Office_phone"<<endl;
cout<<"7:Email"<<endl;
cout<<"8:Mobile_number"<<endl;
cout<<"9:Street_address"<<endl;
cout<<"10:City"<<endl;
cout<<"11:State"<<endl;
cout<<"12:Zip_code"<<endl;
cout<<"13:Country"<<endl;
cin>>s;
if(s=="1"){
string x;
cout<<"Input First_name:";
cin>>x;
Node* node=head->Search_First_name(x,head->Root());
if(node==NULL) cout<<"not found"<<endl;
else {
printNode(node);
}
break;
}
else if(s=="2"){
string x;
cout<<"Input Middle_name:";
cin>>x;
Node* node=head->Search_Middle_name(x,head->Root());
if(node==NULL) cout<<"not found"<<endl;
else {
printNode(node);
}
break;
}
else if(s=="3"){
string x;
cout<<"Input Last_name:";
cin>>x;
Node* node=head->Search_Last_name(x,head->Root());
if(node==NULL) cout<<"not found"<<endl;
else {
printNode(node);
}
break;
}
else if(s=="4"){
string x;
cout<<"Input Company_name:";
cin>>x;
Node* node=head->Search_Company_name(x,head->Root());
if(node==NULL) cout<<"not found"<<endl;
else {
printNode(node);
}
break;
}
else if(s=="5"){
string x;
cout<<"Input Home_phone:";
cin>>x;
Node* node=head->Search_Home_phone(x,head->Root());
if(node==NULL) cout<<"not found"<<endl;
else {
printNode(node);
}
break;
}
else if(s=="6"){
string x;
cout<<"Input Office_phone:";
cin>>x;
Node* node=head->Search_Office_phone(x,head->Root());
if(node==NULL) cout<<"not found"<<endl;
else {
printNode(node);
}
break;
}
else if(s=="7"){
string x;
cout<<"Input Email:";
cin>>x;
Node* node=head->Search_Email(x,head->Root());
if(node==NULL) cout<<"not found"<<endl;
else {
printNode(node);
}
break;
}
else if(s=="8"){
string x;
cout<<"Input Mobile_number:";
cin>>x;
Node* node=head->Search_Mobile_number(x,head->Root());
if(node==NULL) cout<<"not found"<<endl;
else {
printNode(node);
}
break;
}
else if(s=="9"){
string x;
cout<<"Input Street_address:";
cin>>x;
Node* node=head->Search_Street_address(x,head->Root());
if(node==NULL) cout<<"not found"<<endl;
else {
printNode(node);
}
break;
}
else if(s=="10"){
string x;
cout<<"Input City:";
cin>>x;
Node* node=head->Search_City(x,head->Root());
if(node==NULL) cout<<"not found"<<endl;
else {
printNode(node);
}
break;
}
else if(s=="11"){
string x;
cout<<"Input State:";
cin>>x;
Node* node=head->Search_State(x,head->Root());
if(node==NULL) cout<<"not found"<<endl;
else {
printNode(node);
}
break;
}
else if(s=="12"){
string x;
cout<<"Input Zip_code:";
cin>>x;
Node* node=head->Search_Zip_code(x,head->Root());
if(node==NULL) cout<<"not found"<<endl;
else {
printNode(node);
}
break;
}
else if(s=="12"){
string x;
cout<<"Input Country:";
cin>>x;
Node* node=head->Search_Country(x,head->Root());
if(node==NULL) cout<<"not found"<<endl;
else {
printNode(node);
}
break;
}
else{
cout<<"Input error! try again:";
}
}
}
/* To edit the record or entry (or) to update data for certain record or entry */
void Main::Edit_Entry(){
int num;
cout<<"Input the number:";
cin>>num;
Node* node=head->findNode(num,head->Root());
if(node==NULL) cout<<"not found!";
else{
cout<<"Input First name:";
string First_Name;
cin>>First_Name;
node->getdata()->setFirst_name(First_Name);
cout<<"Input Middle name:";
string Middle_Name;
cin>>Middle_Name;
node->getdata()->setMiddle_name(Middle_Name);
cout<<"Input Last_name:";
string Last_Name;
cin>>Last_Name;
node->getdata()->setLast_name(Last_Name);
cout<<"Input Company_name:";
string Company_Name;
cin>>Company_Name;
node->getdata()->setCompany_name(Company_Name);
cout<<"Input Home_phone:";
string Home_Phone;
cin>>Home_Phone;
node->getdata()->setHome_phone(Home_Phone);
cout<<"Input Office_phone:";
string Office_Phone;
cin>>Office_Phone;
node->getdata()->setOffice_phone(Office_Phone);
cout<<"Input Email:";
string email;
cin>>email;
node->getdata()->setEmail(email);
cout<<"Input Mobile_number:";
string Mobile_Number;
cin>>Mobile_Number;
node->getdata()->setMobile_number(Mobile_Number);
cout<<"Input Street_address:";
string Street_Address;
cin>>Street_Address;
node->getdata()->setStreet_address(Street_Address);
cout<<"Input City:";
string city;
cin>>city;
node->getdata()->setCity(city);
cout<<"Input State:";
string state;
cin>>state;
node->getdata()->setState(state);
cout<<"Input Zip_code:";
string Zip_Code;
cin>>Zip_Code;
node->getdata()->setZip_code(Zip_Code);
cout<<"Input Country:";
string country;
cin>>country;
node->getdata()->setCountry(country);
cout<<"Changed!";
}
cout<<"enter any to continue:";
getchar();
getchar();
}
/* To delete the entry by passing node number */
void Main::Delete_Entry(){
cout<<"Input delete number:";
int num;
cin>>num;
head->deleteNode(num);
cout<<"Deleted!";
cout<<"enter any to continue:";
getchar();
getchar();
}
/* fuction to save the record or data for an entry */
void Main::Save(){
cout<<"Input an address book file to save:";
string file;
cin>>file;
int i;
for(i=0;i<file.length();i++) url[i]=file[i];
url[file.length()]='';
out.open(url,ofstream::out);
print_file(head->Root());
out.close();
cout<<"save sucess enter any to continue:";
getchar();
getchar();
}
/* To print cerain entry or record, if no record it returns null other returns that particular entry data */
void Main::print_file(Node* node){
if(node==NULL) return;
else{
out<<node->Key()<<endl<<node->getdata()->getFirst_name()<<endl<<node->getdata()->getMiddle_name()<<endl<<node->getdata()->getLast_name()
<<endl<<node->getdata()->getCompany_name()<<endl<<node->getdata()->getHome_phone()<<endl<<node->getdata()->getOfiice_phone()
<<endl<<node->getdata()->getEmail()<<endl<<node->getdata()->getMobile_number()<<endl<<node->getdata()->getStreet_address()<<endl
<<node->getdata()->getCity()<<endl<<node->getdata()->getState()<<endl<<node->getdata()->getZip_code()<<endl<<node->getdata()->getCountry()
<<endl;
Affiliates *p=node->getdata()->getaffiliates();
p=p->next;
while(p!=NULL){
out<<p->data<<endl;
p=p->next;
}
out<<"|"<<endl;
print_file(node->Left());
print_file(node->Right());
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.