I have a tree program that (works) creates a tree, inorder traverses it, and then prints out messages before deleting the tree (and printing out more messages). That all works.
What doesn't work is the interface from the file to the tree. Uh, let me explain that. When I started this program, I just asked the user to input the tree from the console, easy peasy, right? Well, I actually need to take the data from a file that looks like this...
{ ROOT } //this is one tree
{ ROOT { FRED } } //this is one tree
{ ROOT { FRED { FRED_JUNIOR1 } { FRED_JUNIOR2} } }//this is one tree
{ ROOT
{ FRED
{ FRED_JUNIOR1}
{ FRED_JUNIOR2}
}
{ BARNEY
{ BARNEY_JUNIOR1}
{ BARNEY_JUNIOR2}
}
}// and this is the final tree
Since C++ is uhhh primative, I can't just use a file delimiter and a string tokenizer to strip out the whitespace and add to the tree properly.
I am getting some kind of error in " void dispose_of_tree(tree_node *current_node) " function.
Please help me.
Here is my code...
Expand|Select|Wrap|Line Numbers
- #include <iostream>
- #include <fstream>
- #include <string>
- using namespace std;
- struct tree_node
- {
- char string[100];
- tree_node *left;
- tree_node *right;
- };
- tree_node *root;
- void add_to_tree(char string[100]);
- void attach_node(tree_node *new_node_ptr);
- void pre_order(tree_node *current_node);
- void dispose_of_tree(tree_node *current_node);
- int main()
- {
- root = NULL; // initialize root of tree to NULL
- string line;
- char string[100];
- int length;
- int oCount = 0;
- bool flag = true;
- int target, target1, target2;
- ifstream textFile ("tree.txt");
- if (textFile.is_open())
- {
- while (! textFile.eof())
- {
- getline (textFile, line);
- for (int x = 0; x < line.length(); x++)
- {
- if (line.substr(x) == "R")
- {
- cout << line.substr(x) << endl;
- }
- }//close for
- }//close top while
- } //close if
- pre_order(root);
- dispose_of_tree(root);
- textFile.close();
- return 0;
- }
- void add_to_tree(char string[20])
- {
- tree_node *new_node_ptr;
- new_node_ptr = new tree_node;
- // Initialize new node.
- strcpy(new_node_ptr->string, string);
- new_node_ptr->left = NULL;
- new_node_ptr->right = NULL;
- if(root == NULL)
- {
- root = new_node_ptr;
- }
- else
- {
- attach_node(new_node_ptr);
- }
- }
- void attach_node(tree_node *new_node_ptr)
- {
- tree_node *search_ptr;
- tree_node *follow_ptr;
- search_ptr = root;
- follow_ptr = root;
- while(search_ptr != NULL)
- {
- follow_ptr = search_ptr;
- if(strcmp(new_node_ptr->string, search_ptr->string) < 0)
- {
- search_ptr = search_ptr->left;
- }
- else
- {
- search_ptr = search_ptr->right;
- }
- }
- if(strcmp(new_node_ptr->string, follow_ptr->string) < 0)
- {
- follow_ptr->left = new_node_ptr;
- }
- else
- {
- follow_ptr->right = new_node_ptr;
- }
- }
- void pre_order(tree_node *current_node)
- {
- if(current_node != NULL)
- {
- cout << "HELLO" <<current_node-> string << endl;
- pre_order(current_node->left);
- pre_order(current_node->right);
- }
- }
- void dispose_of_tree(tree_node *current_node)
- {
- if(!((current_node->left == NULL) && (current_node->right == NULL)))
- {
- if(current_node->right != NULL)
- {
- dispose_of_tree(current_node->right);
- }
- if(current_node->left != NULL)
- {
- dispose_of_tree(current_node->left);
- }
- }
- cout << "GOODBYE" <<current_node-> string << endl;
- delete current_node;
- }