Header Files
First, break your constructor declarations into a header file and then define them in a separate CPP file.
Copy Constructor Recursive Problem
Your copy constructor is recursive in that it keeps calling itself until the program runs out of available memory. This is bad.
Cloning (Copying) Tree
Based on the comments below, it seems you need to clone your tree. See example:
BinStrTreeNode* BinStrTreeNode::clone()
{
if (BinStrTreeNode* tmp = new BinStrTreeNode)
{
tmp->value = value;
if (leftchild) tmp->leftchild = leftchild->clone();
if (rightchild) tmp->rightchild = rightchild->clone();
return tmp;
}
return 0;
}
Tree Explanation
If I remember correctly from my college Computer Science days, I maintained a global copy of tree and "tagged" each node as either left or right. That way when I mapped it graphically in Qt, I knew how to display the tree.
You are going to have to maintain a left and right node for the tree also.
To get your code to work, I modified it as follows (ignore the TForm stuff, I just used a different IDE):
Unit1.Cpp
---------
#include "Unit1.h"
#include "File1.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
BinStrTreeNode *root;
root = NULL;
}
//---------------------------------------------------------------------------
File1.cpp
---------
#include <iostream>
#include <string>
#include "File1.h"
using namespace std;
BinStrTreeNode::BinStrTreeNode(string val) {
value = val;
leftchild = NULL;
rightchild = NULL;
cout << "ctor" << endl;
}
File1.h
-------
#include <iostream>
#include <string>
using namespace std;
class BinStrTreeNode{
public:
BinStrTreeNode(string val);
private:
string value;
BinStrTreeNode leftchild;
BinStrTreeNode rightchild;
};
A good example of a binary sort tree can be defined like this:
struct TreeNode {
// An object of type TreeNode represents one node
// in a binary tree of strings.
string item; // The data in this node.
TreeNode left; // Pointer to left subtree.
TreeNode right; // Pointer to right subtree.
TreeNode(string str) {
// Constructor. Make a node containing str.
item = str;
left = NULL;
right = NULL;
}
}; // end struct Treenode
You then have to create the root and everything is added from there:
TreeNode *root; // Pointer to the root node in the tree.
root = NULL; // Start with an empty tree.
Then if you want to add an item to the binary sort tree, then you do something like this:
void treeInsert(TreeNode *&root, string newItem) {
// Add the item to the binary sort tree to which the parameter
// "root" refers. Note that root is passed by reference since
// its value can change in the case where the tree is empty.
if ( root == NULL ) {
// The tree is empty. Set root to point to a new node containing
// the new item. This becomes the only node in the tree.
root = new TreeNode( newItem );
// NOTE: The left and right subtrees of root
// are automatically set to NULL by the constructor.
// This is important!
return;
}
else if ( newItem < root->item ) {
treeInsert( root->left, newItem );
}
else {
treeInsert( root->right, newItem );
}
} // end treeInsert()