I haven't started writing the tree implementations yet, I wanted to know before I wrote them if there would be a conflict. It really is a node question because when the tree is destroyed so are the nodes.
Code:
#define NULL 0
class Node
{
public:
Node(int d = 0): left(NULL), right(NULL), data(d) {}
Node(const Node&);
Node& operator=(const Node&);
~Node() { remove_children(this); }
Node *l_child() const { return left; }
Node *r_child() const { return right; }
Node get_data() const { return data; }
bool has_children() const { return (left != NULL) || (right != NULL); }
bool leaf() const { return (left == NULL) && (right == NULL); }
private:
Node *left, *right;
int data;
void remove_children(Node*);
};
Code:
#include "bintree.h"
//========================= Node Implementation ================================
//copy constructor
Node::Node(const Node& aNode)
:left(aNode.left), right(aNode.right), data(aNode.data) {}
Node& Node::operator=(const Node& aNode)
{
if(&aNode != this)
{
if(has_children())
remove_children(this);
left = aNode.left;
right = aNode.right;
data = aNode.data;
}
return *this;
}
void Node::remove_children(Node* n)
{
n->left = n->right = NULL;
}