构造一个生成随机二叉树的类,二叉树的结点 数、结点所存放的数据类型等都可以是任意的:
TreeElement.h描述的是二叉树的结点信息,Tree.h描述二叉树本身,代码如下:
TreeElement.h:
TreeElement.h描述的是二叉树的结点信息,Tree.h描述二叉树本身,代码如下:
TreeElement.h:
#ifndef TREEELEMENT_H
#define TREEELEMENT_H
template < class Type >
class TreeElement
{
private:
Type &elementData;
TreeElement *leftElement;
TreeElement *rightElement;
public:
TreeElement(Type &elementData);
TreeElement* getLeft();
TreeElement* getRight();
Type& getElementData();
bool setLeft(TreeElement*);
bool setRight(TreeElement*);
bool setElementData(Type &elementData);
} ;
template < class Type >
TreeElement < Type > ::TreeElement(Type & elementData):elementData(elementData)
{
this->leftElement = NULL;
this->rightElement = NULL;
}
template < class Type >
TreeElement < Type >* TreeElement < Type > ::getLeft()
{
return leftElement;
}
template < class Type >
TreeElement < Type >* TreeElement < Type > ::getRight()
{
return rightElement;
}
template < class Type >
Type & TreeElement < Type > ::getElementData()
{
return elementData;
}
template < class Type >
bool TreeElement < Type > ::setLeft(TreeElement <
#define TREEELEMENT_H
template < class Type >
class TreeElement
{
private:
Type &elementData;
TreeElement *leftElement;
TreeElement *rightElement;
public:
TreeElement(Type &elementData);
TreeElement* getLeft();
TreeElement* getRight();
Type& getElementData();
bool setLeft(TreeElement*);
bool setRight(TreeElement*);
bool setElementData(Type &elementData);
} ;
template < class Type >
TreeElement < Type > ::TreeElement(Type & elementData):elementData(elementData)
{
this->leftElement = NULL;
this->rightElement = NULL;
}
template < class Type >
TreeElement < Type >* TreeElement < Type > ::getLeft()
{
return leftElement;
}
template < class Type >
TreeElement < Type >* TreeElement < Type > ::getRight()
{
return rightElement;
}
template < class Type >
Type & TreeElement < Type > ::getElementData()
{
return elementData;
}
template < class Type >
bool TreeElement < Type > ::setLeft(TreeElement <