I want to construct an initially empty class and populate it later, when I receive values to add from an input file. My project currently compiles and runs; just not with expected results. There is something wrong with the way I am constructing the tree and /or the way I handle cases in the add_node function where the "root" parameter received by that function always evalutates as NULL.
I have tried different variations of the constructor. In previous versions, when I started the tree in main.cpp with a value, the rest of the program's functions worked as expected - but my tree would never match the expected output because I had to start it with some arbitrary integer. Now, when I start with an initially empty tree (as I have the code snippets shown doing now), it never gets rid of the idea that the root is NULL. I'm not quite sure how to solve this.
I have 3 files in my project: main.cpp, avltree.h, and avltree.cpp. I think I've included all relevant snippets from main.cpp and avltree.cpp.
//********
main.cpp
//********
include <cstdio>
include <cstdlib>
include <iostream>
include <fstream>
include "avltree.h"
using namespace std;
using namespace Trees;
int main(int argc, const char argv[])
{
//
node root = new node();
tree myTree(root);
...
...
case 'A': //Attempt to add the following integer to the tree;
value = atoi(&buffer[1]);
myTree.add_node(root, value);
break;
...
return 0;
}
//********
avltree.cpp
//********
node::node(int x)
{
data = x;
//the integer passed to the constructor becomes
//the data in the node
left = NULL;
right = NULL;
parent = NULL;
tree::tree(node *currptr)
{
root = currptr; //the pointer to the node used as the argument becomes
//the root of the tree
// std::cout << root->data << std::endl;
// std::cout << "Constructing an empty tree." << std::endl;
count_lh = 0;
count_equal = 1;
count_rh = 0;
}
void tree::add_node(node *currptr, int num)
{
if(currptr == NULL) //create the root node of the tree
{
//I expect currptr to be NULL
//only the first time the add_node function is called
//in actuality, each time this function is called
//currptr is evaluating as NULL
//Icreatedthishelperfunctiontosetthevalueofthe//root,butitdoesn't seem to do anything root->set_data(num); //I also tried other things here like: //node * root = new node(num); taller = true; std::cout << "The value entered (" << num << ") was used as the root of the tree." << std::endl;}else{//doesn'tmatterwhathappensherebecausethiscodeneverexecutes!}
}
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
> I want to construct an initially empty class and populate it later,
You cannot have an empty class. You can have an ininitialised one, or one initialised to defaults that have no effect, but it cannot be 'empty'.
> node* root = new node();
How does that work when you have no default constructor? Or is it defined in-line in the header?
Since you have drastically edited the code and not really provided a clear explanation of the problem it is hard to assist; but it looks to me like you only ever instantiate one node object and pass it to both the constructor and to add_node(). To be honest you have either edited this code so drastically that it no longer represents a tree class, or the code is nonsense.
> //I expect currptr to be NULL
Why do you expect that? You never pass it NULL, you pass it 'root' instantiated in main().
Clifford
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Hi,
I want to construct an initially empty class and populate it later, when I receive values to add from an input file. My project currently compiles and runs; just not with expected results. There is something wrong with the way I am constructing the tree and /or the way I handle cases in the add_node function where the "root" parameter received by that function always evalutates as NULL.
I have tried different variations of the constructor. In previous versions, when I started the tree in main.cpp with a value, the rest of the program's functions worked as expected - but my tree would never match the expected output because I had to start it with some arbitrary integer. Now, when I start with an initially empty tree (as I have the code snippets shown doing now), it never gets rid of the idea that the root is NULL. I'm not quite sure how to solve this.
I have 3 files in my project: main.cpp, avltree.h, and avltree.cpp. I think I've included all relevant snippets from main.cpp and avltree.cpp.
//********
main.cpp
//********
include <cstdio>
include <cstdlib>
include <iostream>
include <fstream>
include "avltree.h"
using namespace std;
using namespace Trees;
int main(int argc, const char argv[])
{
//
node root = new node();
tree myTree(root);
...
...
case 'A': //Attempt to add the following integer to the tree;
value = atoi(&buffer[1]);
myTree.add_node(root, value);
break;
...
return 0;
}
//********
avltree.cpp
//********
node::node(int x)
{
data = x;
//the integer passed to the constructor becomes
//the data in the node
left = NULL;
right = NULL;
parent = NULL;
}
node::~node()
{
delete left;
delete right;
delete parent;
}
tree::tree()
{
root = NULL;
right_tree = NULL;
left_tree = NULL;
count_lh = 0;
count_equal = 1;
count_rh = 0;
}
tree::tree(node *currptr)
{
root = currptr; //the pointer to the node used as the argument becomes
//the root of the tree
// std::cout << root->data << std::endl;
// std::cout << "Constructing an empty tree." << std::endl;
count_lh = 0;
count_equal = 1;
count_rh = 0;
}
void tree::add_node(node *currptr, int num)
{
if(currptr == NULL) //create the root node of the tree
{
//I expect currptr to be NULL
//only the first time the add_node function is called
//in actuality, each time this function is called
//currptr is evaluating as NULL
}
> I want to construct an initially empty class and populate it later,
You cannot have an empty class. You can have an ininitialised one, or one initialised to defaults that have no effect, but it cannot be 'empty'.
> node* root = new node();
How does that work when you have no default constructor? Or is it defined in-line in the header?
Since you have drastically edited the code and not really provided a clear explanation of the problem it is hard to assist; but it looks to me like you only ever instantiate one node object and pass it to both the constructor and to add_node(). To be honest you have either edited this code so drastically that it no longer represents a tree class, or the code is nonsense.
> //I expect currptr to be NULL
Why do you expect that? You never pass it NULL, you pass it 'root' instantiated in main().
Clifford