其中的 linkStack.h 和 linkQueue 分别在 以下两篇博文里:linkStack 、linkQueue
#include <iostream>
#include "linkStack.h"
#include "linkQueue.h"
using namespace std;
//定义节点
template<class T>
struct node
{
T data; //结点中保存的数据
node *left; //结点的左儿子
node *right; //结点的右儿子
node():left(NULL),right(NULL){} //构造函数,无参数
//构造函数,带参数
node(const T &x,node *l=NULL,node *r=NULL):data(x),left(l),right(r){}
~node(){} //析构函数
};
//定义二叉树
template<class T>
class BT
{
public:
BT(); //构造空二叉树
BT(const T &x); //构造以数据值为x的结点为根结点的二叉树
BT(const node<T> *p); //构造以p为根结点的二叉树
node<T> * getRoot(); //返回根结点地址
//创建一个以数据值为x的结点为根结点,以lt为左子树、rt为右子树的二叉树
void makeTree(const T &x,BT <,BT &rt);
void delLeft(); //删除左子树
void delRight(); //删除右子树
bool empty() const; //判断是否为空树
void clear(); //删除二叉树
int size() const; //求二叉树的规模
int height() const; //求二叉树的高度
int leafCount() const; //求二叉树叶子结点个数
void preOrder() const; //前序遍历二叉树
void midOrder() const; //中序遍历二叉树
void postOrder() const; //后续遍历二叉树
void levelOrder() const; //层次遍历二叉树
bool isCompBT() const; //判断是否为完全二叉树
void createTree(T flag); //创建二叉树,输入flag表示为空
void copyTree(BT &tree); //复制二叉树
~BT(); //析构函数
private:
node<T> *root; //二叉树的根结点
void clear(node<T> *t); //删除以t为根结点的二叉树
int size(node<T> *t) const; //求以t为根结点的二叉树的规模
int height(node<T> *t) const; //求以t为根结点的二叉树的高度
//复制以t为根结点的二叉树为以r为根结点的新二叉树
void copyTree(node<T> *&r,node<T> *t);
};
/*二叉树的实现*/
//构造空二叉树
template <class T>
BT<T>::BT()
{
root=NULL;
}
//构造以数据值为x的结点为根结点的二叉树
template <class T>
BT<T>::BT(const T &x)
{
root=new node<T>(x);
}
//构造以p为根结点的二叉树
template <class T>
BT<T>::BT(const node<T> *p)
{
root=p;
}
//返回根结点地址
template <class T>
node<T> * BT<T>::getRoot()
{
return root;
}
//创建一个以数据值为x的结点为根结点,以lt为左子树、rt为右子树的二叉树
template <class T>
void BT<T>::makeTree(const T &x,BT <,BT &rt)
{
root=new node<T>(x,lt.root,rt.root);
lt.root=NULL;
rt.root=NULL;
}
//删除左子树
template <class T>
void BT<T>::delLeft()
{
if(root&&root->left)
{
BT tmp=root->left;
root->left=NULL;
tmp.clear();
}
}
//删除右子树
template <class T>
void BT<T>::delRight()
{
if(root&&root->right)
{
BT tmp=root->right;
root->right=NULL;
tmp.clear();
}
}
//判断是否为空树
template <class T>
bool BT<T>::empty() const
{
return root==NULL;
}
//删除二叉树
template <class T>
void BT<T>::clear()
{
if(root)
{
clear(root);
root=NULL;
}
}
//删除以t为根结点的二叉树
template <class T>
void BT<T>::clear(node<T> *t)
{
if(t)
{
if(t->left) clear(t->left);
if(t->right) clear(t->right);
delete t;
}
}
//求二叉树的规模
template <class T>
int BT<T>::size() const
{
return size(root);
}
//求以t为根结点的二叉树的规模
template <class T>
int BT<T>::size(node<T> *t) const
{
if(t) return 1+size(t->left)+size(t->right);
return 0;
}
//求二叉树的高度
template <class T>
int BT<T>::height() const
{
return height(root);
}
//求以t为根结点的二叉树的高度
template <class T>
int BT<T>::height(node<T> *t) const
{
if(t)
{
int lt=height(t->left),rt=height(t->right);
return 1+(lt>rt ? lt : rt);
}
return -1;
}
//求二叉树叶子结点个数
template <class T>
int BT<T>::leafCount() const
{
/*递归实现:
if(!t) return 0;
if(!t->left&&!t->right) return 1;
return leavesCount(t->left)+leavesCount(t->right);
*/
//非递归实现:
int cnt=0;
linkStack<node<T> *> stack;
node<T> *t=root;
if(t)
{
stack.push(t);
while(!stack.empty())
{
node<T> *tmp=stack.pop();
if(!tmp->left&&!tmp->right) ++cnt;
if(tmp->right) stack.push(tmp->right);
if(tmp->left) stack.push(tmp->left);
}
}
return cnt;
}
//前序遍历二叉树
template <class T>
void BT<T>::preOrder() const
{
/*递归实现:
if(t)
{
cout<<t->data<<' ';
preOrder(t->left);
preOrder(t->right);
}
*/
//非递归实现:
if(root)
{
cout<<"\n前序遍历:";
linkStack<node<T> *> stack;
node<T> *t=root;
if(t)
{
stack.push(t);
while(!stack.empty())
{
node<T> *tmp=stack.pop();
cout<<tmp->data<<' ';
if(tmp->right) stack.push(tmp->right);
if(tmp->left) stack.push(tmp->left);
}
}
}
}
//中序遍历二叉树
template <class T>
void BT<T>::midOrder() const
{
/*递归实现:
if(t)
{
midOrder(t->left);
cout<<t->data<<' ';
midOrder(t->right);
}
*/
//非递归实现:
if(root)
{
cout<<"\n中序遍历:";
linkStack<node<T> *> stack;
node<T> *t=root;
if(t)
{
while(t)
{
stack.push(t);
t=t->left;
}
while(!stack.empty())
{
node<T> *tmp=stack.pop();
cout<<tmp->data<<' ';
if(tmp->right)
{
stack.push(tmp->right);
node<T> *tmp1=tmp->right;
while(tmp1->left)
{
stack.push(tmp1->left);
tmp1=tmp1->left;
}
}
}
}
}
}
//后续遍历二叉树
template <class T>
void BT<T>::postOrder() const
{
/*递归实现:
if(t)
{
postOrder(t->left);
postOrder(t->right);
cout<<t->data<<' ';
}
*/
//非递归实现:
if(root)
{
cout<<"\n后续遍历:";
linkStack<node<T> *> stack;
node<T> *t=root;
do
{
while(t)
{
stack.push(t);
t=t->left;
}
node<T> *tmp=NULL;
bool flag=1;
while(!stack.empty()&&flag)
{
t=stack.top();
if(t->right==tmp)
{
cout<<t->data<<' ';
stack.pop();
tmp=t;
}
else
{
t=t->right;
flag=0;
}
}
}while(!stack.empty());
}
}
//层次遍历二叉树
template <class T>
void BT<T>::levelOrder() const
{
if(root)
{
cout<<"\n层次遍历:";
linkQueue<node<T> *> queue;
node<T> *t=root;
queue.enQueue(t);
while(!queue.empty())
{
t=queue.deQueue();
cout<<t->data<<' ';
if(t->left) queue.enQueue(t->left);
if(t->right) queue.enQueue(t->right);
}
}
}
//判断是否为完全二叉树
template <class T>
bool BT<T>::isCompBT() const
{
if(!root) return 1;
bool judge=1;
bool flag=0;
linkQueue<node<T> *> queue;
queue.enQueue(root);
while(!queue.empty())
{
node<T> *tmp=queue.getHead();
if(flag)
{
if(tmp->left||tmp->right)
{
judge=0;
break;
}
}
else
{
if(tmp->left&&tmp->right)
{
queue.enQueue(tmp->left);
queue.enQueue(tmp->right);
}
else if(!tmp->left&&tmp->right)
{
judge=0;
break;
}
else
{
if(tmp->left) queue.enQueue(tmp->left);
flag=1;
}
}
queue.deQueue();
}
while(!queue.empty())
queue.deQueue();
return judge;
}
//创建二叉树,输入flag表示为空
template <class T>
void BT<T>::createTree(T flag)
{
linkQueue<node<T> *> queue;
node<T> *tmp;
T x,ldata,rdata;
//创建树,输入flag表示空
cout<<"\n输入根结点:";
cin>>x;
if(x==flag) root=NULL;
else
{
root=new node<T>(x);
queue.enQueue(root);
while(!queue.empty())
{
tmp=queue.deQueue();
cout<<"\n输入"<<tmp->data<<"的两个儿子("<<flag<<"表示空结点):";
cin>>ldata>>rdata;
if(ldata!=flag) queue.enQueue(tmp->left=new node<T>(ldata));
if(rdata!=flag) queue.enQueue(tmp->right=new node<T>(rdata));
}
}
cout<<"\n创建完成!\n\n";
}
//复制二叉树
template <class T>
void BT<T>::copyTree(BT &tree)
{
copyTree(root,tree.root);
}
//复制以t为根结点的二叉树为以r为根结点的新二叉树
template <class T>
void BT<T>::copyTree(node<T> *&r,node<T> *t)
{
if(t)
{
r=new node<T>(t->data);
copyTree(r->left,t->left);
copyTree(r->right,t->right);
}
else r=NULL;
}
//析构函数
template <class T>
BT<T>::~BT()
{
clear();
}