Please help me with this C++ program. You are to write a C++ program to that wil
ID: 652108 • Letter: P
Question
Please help me with this C++ program. You are to write a C++ program to that will create store emails. The structure to use is a binary tree. After creating the tree print out the tree in ‘inorder’ and ‘preorder’.
Now delete all the nodes in the tree that are ‘.net’ and print out the tree in ‘inorder’ and ‘postorder’. I will supply you a list of emails to test your code with.
Restrictions:
You are to store the emails in an ordered binary tree structure.
Contents of emailListTree.txt file:
naberc455@charter.net, wdfarmboy@bellsouth.net, jaustin6@hotmail.com,
sidbaniel@aol.com,
sberger@ua.edu,
myborland@yahoo.com, guickbuckner@eleinc.com,
richpoor@att.net jennifer.copenahgen@charter.net, davie@builders.com, princdrane@comcast.net, pepper@bham.rr.com, hazedurant@knology.net, durantty@knology.net,
foustet@mont.com,
tigerp@gmail.com, ananana.fuller@gmail.com, tomhark@bellsouth.net, karendrisrael@bellsouth.net, bigham@comcast.net, sholder@partners.com, kholtty@partners.com, ahienry.deco@att.net,
denismiss46@att.net,
well@bellsouth.net,
kessler@aol.com,
nkirkpat@bellsouth.net, ericgreat@eleinc.com,
clong@yahoo.com,
sharsmile@earthlink.net, padronone@comcast.net, padrontwo@comcast.net, jillstark@eleinc.com,
perryside@gmail.com, harleydone@bellsouth.net, daranO@eleinc.com, rmrobertvill@yahoo.com martysnot@att.net, Stylesandmore@hotmail.com brendatobe@att.net, kafer@mindspring.com, emmes@bellsouth.net,
emmu@bellsouth.net, Sheadina@knology.net, sheaba@madison.com, sshawman@smhg.com,
bbjjcc@aol.com,
dainagirl@aol.com,
jrhay@jewelers.com, jimmydolittle@jewelers.com,
bark@aol.com,
windom@jewelers.com,
robersom@att.net,
sumrrak@aol.com,
sprainankle@charter.net, tom.dana@gmail.com,
ssshark@aol.com, steinmart@windstream.net, sugardaddy@bellsouth.net, richarthegreat@att.net, rhondafonda@att.net, Weaveralittle@bellsouth.net, weaveralot@bellsouth.net, rwilsonball@lumber.net,
chasem@gmail.com,
hibowolff@hotmail.com
turpintin@uab.edu, caomelong@charter.net,
mitchum@me.com,
welby@bellsouth.net,
nancyfancy@att.net,
freewilly@live.com,
bellaroo@aol.com,
lickmims@realty.com,
brenhandy@att.com,
gapglobal@aol.com,
lobster@gmail.com,
zookria@yahoo.com, ricepatty@bellsouth.net, hollisjmacaroo@southern.com, aturblue@charter.net, julesvern@yahoo.com, warren.kughnal@gmail.com, hardclayton@gmail.com, hollyman@gmail.com, whitmoreorless@busa.com, callmeman@yahoo.com, bamawin@gmail.com,
kirkship@comcast.net, tim.dorson@regions.com, stellsgrove@global.net, cullmanman@gmail.com bellaaroast@att.net, carol.spriker@healthsouth.com, Rollontide@gmail.com, wade.toots@company.com, breeornotbree@lobster.com, premierfashions@gmail.com, karen.stillman@mc.com, scothsman@wheel.com, grahamslaw@comcast.net, b.briev@comcast.net,
laydown@lynn.com
Explanation / Answer
//please provide emailListTree.txt text file
#include<iostream>
#include<fstream>
#include<cstring>
using namespace std;
struct node {
string data;
node *left;
node *right;
node(string d) {
data = string(d);
left = right = NULL;
}
};
node* insert(node *root, string d) {
if (root == NULL) {
root = new node(d);
} else {
if (strcmp(root->data.c_str(), d.c_str()) <= 0) {
root->left = insert(root->left, d);
} else {
root->right = insert(root->right, d);
}
}
return root;
}
void inOrder(node *root) {
if (root == NULL)
return;
inOrder(root->left);
cout << root->data << ", ";
inOrder(root->right);
}
void preOrder(node *root) {
if (root == NULL)
return;
cout << root->data << ", ";
inOrder(root->left);
inOrder(root->right);
}
node* inOrderSuccessor(node* root) {
struct node* current = root;
/* loop down to find the leftmost leaf */
while (current->left != NULL)
current = current->left;
return current;
}
node* removedotNET(node *root) {
if (root == NULL) return root;
root->left = removedotNET(root->left);
root->right = removedotNET(root->right);
if (strcmp((root->data).substr(strlen((root->data).c_str()) - 4, 4).c_str(), ".net") == 0) {
if (root->left == NULL) {
struct node *temp = root->right;
free(root);
return temp;
} else if (root->right == NULL) {
struct node *temp = root->left;
free(root);
return temp;
}
struct node* temp = inOrderSuccessor(root->right);
root->data = temp->data;
// Delete the inorder successor
root->right = removedotNET(root->right);
}
return root;
}
int main() {
ifstream inFile("emailListTree.txt");
node *root;
if (inFile) {
while (!inFile.eof()) {
string d;
inFile>>d;
root = insert(root, d);
}
} else
cout << "File cannot be read !!!";
cout << "Data in INORDER: ";
inOrder(root);
cout << " Data in PREORDER: ";
preOrder(root);
cout << " ";
removedotNET(root);
cout << "After deletion, INORDER: ";
inOrder(root);
cout << " After deletion, PREORDER: ";
preOrder(root);
cout << " ";
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.