This post has been de-listed
It is no longer included in search results and normal feeds (front page, hot posts, subreddit posts, etc). It remains visible only via the author's post history.
Does anyone know why this file won't work? I get no error codes within the code itself but once I try and print the console prints "not found." I've imported "tree1.txt" into C itself
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
struct node {
string name, tele;
node* lchild = NULL;
node* rchild = NULL;
};
class btree {
public:
void print_preorder();
void print_postorder();
btree() {};
void add_node(string n, string t);
string find_tele(string n, node* p);
node* get_root() { return root; };
void print_inorder();
void create_from_file(string fname);
private:
void print_preorder_aux(node n);
void print_postorder_aux(node n);
void print_inorder_aux(node n);
node* root = NULL;
};
void btree::print_preorder() {
if (root == NULL) {
cout << "Tree is empty" << endl;
}
else {
print_preorder_aux(*root);
}
}
void btree::print_preorder_aux(node n) {
cout << n.name << " has telephone " << n.tele << endl;
if (n.lchild != NULL) {
print_preorder_aux(*n.lchild);
}
else {
print_preorder_aux(*n.rchild);
}
}
void btree::print_postorder() {
if (root == NULL) {
cout << "Tree is empty" << endl;
}
else {
print_postorder_aux(*root);
}
}
void btree::print_postorder_aux(node n) {
if (n.lchild != NULL) {
print_postorder_aux(*n.lchild);
}
else {
print_postorder_aux(*n.rchild);
}
cout << n.name << " has telephone " << n.tele << endl;
}
void btree::create_from_file(string fname) {
string t, nam;
node* temp;
ifstream infile;
infile.open(fname);
getline(infile, nam);
while (nam != "done") {
getline(infile, t);
this -> add_node(nam, t);
getline(infile, nam);
}
}
void btree::print_inorder() {
if (root == NULL) {
cout << "tree is empty \n";
}
else {
print_inorder_aux(*root);
}
}
void btree::print_inorder_aux(node n) {
if (n.lchild != NULL) {
print_inorder_aux(*n.lchild);
}
cout << n.name << " has telephone " << n.tele << endl;
if (n.rchild != NULL) {
print_inorder_aux(*n.rchild);
}
}
void btree::add_node(string n, string t) {
node* temp;
node* pos;
if (root == NULL) {
root = new node;
root -> name = n;
root -> tele = t;
}
else {
pos = root;
bool placed = false;
while (!placed) {
if (n < pos->name && pos->lchild != NULL) {
pos = pos -> lchild;
}
else if (n < pos->name) {
pos -> lchild = new node;
pos -> lchild -> name = n;
pos -> lchild -> tele = t;
placed = true;
}
else if (n > pos -> name && pos -> rchild != NULL) {
pos = pos->rchild;
}
else {
pos -> rchild = new node;
pos -> rchild -> name = n;
pos -> rchild -> tele = t;
placed = true;
}
}
}
};
string btree::find_tele(string n, node* p) {
if (p == NULL) {
return "not found";
}
else if (n == p -> name) {
return p -> tele;
}
else if (n < p -> name) {
return find_tele(n, p -> lchild);
}
else {
return find_tele(n, p -> rchild);
}
};
int main() {
btree ex;
ex.add_node("joe", "123");
ex.add_node("mike", "456");
ex.add_node("al", "674");
ex.add_node("mabel", "918");
cout << ex.find_tele("al",ex.get_root()) << endl;
cout << ex.find_tele("wally",ex.get_root()) << endl;
cout << ex.find_tele("mabel", ex.get_root()) << endl;
btree fromfile;
string fname = "tree1.txt";
fromfile.create_from_file(fname);
cout << "fromfile is (\ninnorder form) \n";
fromfile.print_inorder();
cout << "fromfile is (\npostorder form) \n";
fromfile.print_postorder();
cout << "fronfile is (\npreorder form) \n";
fromfile.print_preorder();
return 0;
}
Subreddit
Post Details
- Posted
- 3 years ago
- Reddit URL
- View post on reddit.com
- External URL
- reddit.com/r/AskComputer...