下面是树的节点数据类型的实现
这里节点的成员数据域全部使用指针变量
BinaryTreeNode.h 文件
//此类为二叉查找树的树节点类
//定义的关键子,值,父节点和儿子节点
#ifndef BINARY_TREE_NODE_H
#define BINARY_TREE_NODE_H
#include "objectclass.h"//通用类
class BinaryTreeNode
{
private:
ObjectClass *theKey;//关键字
ObjectClass *theValue;//值
BinaryTreeNode *parent;//父亲节点
BinaryTreeNode *left;//左儿子
BinaryTreeNode *right;//右儿子
//定义左右子树的宽度以便打印
int leftWidth;
int rightWidth;
//定义当前节点应该输出的位子,从左起点到右的宽度
int leftOutPutLen;
public:
BinaryTreeNode();
BinaryTreeNode(ObjectClass *theKey,ObjectClass *theValue);
ObjectClass *getKey();
ObjectClass *getValue();
BinaryTreeNode *getLeft();
BinaryTreeNode *getRight();
BinaryTreeNode *getParent();
int getLeftWidth();
int getRightWidth();
int getLeftOutPutLen();
void setKey(ObjectClass *theKey);
void setValue(ObjectClass *theValue);
void setLeft(BinaryTreeNode *left);
void setRight(BinaryTreeNode *Right);
void setParent(BinaryTreeNode *parent);
void setWidth(int,int);//设置子树宽度
void setLeftOutPutLen(int len);
};
#endif
BinaryTreeNode.cpp 文件
#include "BinaryTreeNode.h"
BinaryTreeNode::BinaryTreeNode()
{
theKey = NULL;
theValue = NULL;
parent = NULL;
left = NULL;
right = NULL;
leftWidth=0;
rightWidth=0;
leftOutPutLen=0;
}
BinaryTreeNode::BinaryTreeNode(ObjectClass *theKey,ObjectClass *theValue)
{
this->theKey = theKey;
this->theValue = theValue;
parent = NULL;
left = NULL;
right = NULL;
leftWidth=0;
rightWidth=0;
leftOutPutLen=0;
}
int BinaryTreeNode::getLeftWidth()
{
return leftWidth;
}
int BinaryTreeNode::getRightWidth()
{
return rightWidth;
}
ObjectClass *BinaryTreeNode::getKey()
{
return theKey;
}
ObjectClass *BinaryTreeNode::getValue()
{
return theValue;
}
BinaryTreeNode *BinaryTreeNode::getLeft()
{
return left;
}
BinaryTreeNode *BinaryTreeNode::getRight()
{
return right;
}
BinaryTreeNode *BinaryTreeNode::getParent()
{
return parent;
}
void BinaryTreeNode::setWidth(int leftWidth, int rightWidth)
{
this->leftWidth=leftWidth;
this->rightWidth=rightWidth;
}
void BinaryTreeNode::setValue(ObjectClass *theValue)
{
this->theValue = theValue;
}
void BinaryTreeNode::setKey(ObjectClass *theKey)
{
this->theKey = theKey;
}
void BinaryTreeNode::setLeft(BinaryTreeNode *left)
{
this->left = left;
}
void BinaryTreeNode::setRight(BinaryTreeNode *right)
{
this->right = right;
}
void BinaryTreeNode::setParent(BinaryTreeNode *parent)
{
this->parent=parent;
}
int BinaryTreeNode::getLeftOutPutLen()
{
return leftOutPutLen;
}
void BinaryTreeNode::setLeftOutPutLen(int len)
{
this->leftOutPutLen = len;
}