//Header file:binaryTreeType.h 二叉树
#ifndef H_BinaryTreeType
#define H_BinaryTreeType
#include
#include
using namespace std;
template
struct nodeType
{
elemType info;
nodeType *llink;
nodeType *rlink;
};
template
class binaryTreeType
{
public:
const binaryTreeType& operator=(const binaryTreeType&);
bool isEmpty();
void inorderTraversal();
void preorderTraversal();
void postorderTraversal();
void inorderTraversal(void (*vist) (elemType&));
int treeHeight();
void destroyTree();
binaryTreeType(const binaryTreeType& otherTree);
binaryTreeType();
~binaryTreeType();
protected:
nodeType *root;
private:
void copyTree(nodeType* &copiedTreeRoot,nodeType* otherTreeRoot);
void destroy(nodeType* &p);
void inorder(nodeType *p);
void preorder(nodeType *p);
void postorder(nodeType *p);
void inorder(nodeType *p,void (*vist) (elemType&));
int height(nodeType *p);
int max(int x,int y);
};
template
bool binaryTreeType::isEmpty()
{
return (root==NULL);
}
template
binaryTreeType::binaryTreeType()
{
root=NULL;
}
template
void binaryTreeType::inorderTraversal()
{
inorder(root);
}
template
void binaryTreeType::preorderTraversal()
{
preorder(root);
}
template
void binaryTreeType::postorderTraversal()
{
postorder(root);
}
template
void binaryTreeType::inorderTraversal(void (*vist) (elemType& item))
{
inorder(root,*vist);
}
template
int binaryTreeType::treeHeight()
{
return height(root);
}
template
void binaryTreeType::inorder(nodeType *p)
{
if (p!=NULL)
{
inorder(p->link);
cout<info<<" ";
inorder(p->rlink);
}
}
template
void binaryTreeType::preorder(nodeType *p)
{
if(p!=NULL)
{
cout<info<<" ";
preorder(p->llink);
preorder(p->rlink);
}
}
template
void binaryTreeType::postorder(nodeType *p)
{
if(p!=NULL)
{
postorder(p->llink);
postorder(p->rlink);
cout<info<<" ";
}
}
template
void binaryTreeType::inorder(nodeType* p,void (*vist) (elemType& item))
{
if (p!=NULL)
{
inorder(p->llink,*vist);
(*vist)(p->info);
inorder(p->rlink,*vist);
}
}
template
int binaryTreeType::height(nodeType *p)
{
if (p==NULL)
return 0;
else
return 1+max(height(p->llink),height(p->rlink));
}
template
int binaryTreeType::max(int x,int y)
{
if (x>=y)
return x;
else
return y;
}
template
void binaryTreeType::copyTree(nodeType* &copiedTreeRoot,
nodeType* otherTreeRoot)
{
if (otherTreeRoot==NULL)
copiedTreeRoot=NULL;
else
{
copiedTreeRoot=new nodeType;
copiedTreeRoot->info=otherTreeRoot->info;
copyTree(copiedTreeRoot->llink,otherTreeRoot->llink);
copyTree(copiedTreeRoot->rlink,otherTreeRoot->rlink);
}
}
template
void binaryTreeType::destroy(nodeType* &p)
{
if (p!=NULL)
{
destroy(p->llink);
destroy(p->rlink);
delete p;
p=NULL;
}
}
template
void binaryTreeType::destroyTree()
{
destroy(root);
}
template
binaryTreeType::binaryTreeType(const binaryTreeType& otherTree)
{
if (otherTree.root==NULL)
root=NULL;
else
copyTree(root,otherTree.root);
}
template
binaryTreeType::~binaryTreeType()
{
destroy(root);
}
template
const binaryTreeType& binaryTreeType::operator=(const binaryTreeType& otherTree)
{
if (this!=&otherTree)
{
if(root!=NULL)
destroy(root);
if (otherTree.root==NULL)
root=NULL;
else
copyTree(root,otherTree.root);
}
return *this;
}
#endif
|