To modify main's root, you need to pass a reference to root (&root). This means a little re-work of all of your code (so insert
accepts a tNode** and knows how to use it).
Forget about that though, we don't need main to have a root.
The first thing to understand is that you have two objects, a node and a tree. A node holds the data, and left/right pointers, and the tree is the way these nodes are organized.
These are two different classes. The basic set-up would be:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
|
typedef string tType;
class tNode
{
// For this I just keep these public.
public:
tType value;
tNode* left;
tNode* right;
// Classes can have constructors, so this saves some code later
tNode(tType v)
{
value = v;
left = NULL;
right = NULL;
}
// This is a destructor, it's called automattically when the class
// goes out of scope. Don't call it yourself.
~tNode(void)
{
// We don't have to do anything
}
};
class tTree
{
private:
tNode* root; // The tree's root node. Note that it is a pointer
// This function destroys the tree. It will be recursive
void destroy(tNode* current);
public:
// Construct
tTree(void)
{
root = NULL; // very important
}
// Destruct
~tTree(void)
{
//destroy(root); uncomment when we create destroy
}
// Public access to the destroy function. Main has no idea
// about root, so there are no arguments
void clear(void);
};
|
Have you learned about header files? If so, that would be the next thing to know about. Otherwise, we can now do this:
1 2 3 4 5
|
int main(void)
{
tTree myTree;
return 0;
}
|
One thing that your code is missing is all of the
deletes
that are needed when you make
new
. This is called a memory leak, and it is bad.
tTree makes
new
when a node is created, and then
edelete
s all of the nodes in the destructor.
To really show this, we need an insert and print method. Note that tNode is untouched (and will remain untouched... until it gets the "count" variable).
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
|
#include <iomanip> // among other things
class tTree
{
private:
tNode* root; // The tree's root node. Note that it is a pointer
// This function destroys the tree. It will be recursive
void destroy(tNode* current)
{
delete current; // It is okay to delete a NULL pointer, but not a garbage one
}
public:
// Construct
tTree(void)
{
root = NULL; // very important
}
// Destruct
~tTree(void)
{
destroy(root);
}
// Public access to the destroy function. Main has no idea
// about root, so there are no arguments
void clear(void){
destroy(root);
root = NULL;
}
void print(void)
{
cout << "Address: " << setw(8) << root
<< " Value: " << setw(12) << left << root->value
<< " Left: " << setw(8) << root->left
<< " Right: " << setw(8) << root->right
<< endl;
}
void insert(tType newValue)
{
root = new tNode(newValue);
}
};
int main(void)
{
tTree myTree;
myTree.insert("Hello");
myTree.print();
return 0;
}
|
Granted, It's not very useful. This is the general structure of the tree though. If you found this helpful I can go further.
Edit:: I'm actually having some trouble getting this to work at all without a tNode**, we'll see.