#include<iostream>
using namespace std;
//二叉树节点的创建
template<class Type>
class BinTreeNode
{
//friend class BinTree<Type>;
public:
BinTreeNode():data(Type()),leftChild(NULL),rightChild(NULL)
{}
BinTreeNode(Type d,BinTreeNode<Type> *l=NULL,BinTreeNode<Type> *r=NULL):
data(d),leftChild(l),rightChild(r)
{}
~BinTreeNode()
{}
private:
Type data;
BinTreeNode<Type> *leftChild;
BinTreeNode<Type> *rightChild;
};
//二叉树的创建
template <class Type>
class BinTree
{
public:
BinTree():root(NULL)
{}
BinTree(Type ref):StopFlag(ref),root(NULL)
{}
BinTree(const BinTree<Type> &bt)
{
root = Copy(bt.root);
StopFlag = bt.StopFlag;
}
~BinTree()
{
Destroy(root);
}
private:
BinTreeNode<Type> *root;
Type StopFlag;
};
//二叉树功能的实现
template<class Type>
class BinTree
{
public:
//根据中序、后序创建二叉树
void CreateBinTreeByMidPost(BinTreeNode<Type> *&t,
char *LVR,
char *LRV,
int n)
{
if(n == 0)
t = NULL;
else
{
int i=0;
while(LRV[n-1] != LVR[i])
{i++;}
t = new BinTreeNode<Type>(LVR[i]);
CreateBinTreeByMidPost(t->rightChild,LVR+i+1,LRV+i,n-1-i);
CreateBinTreeByMidPost(t->leftChild,LVR,LRV,i);
}
}
//根据前序、中序创建二叉树
void CreateBinTreeByPreMid(BinTreeNode<Type> *&t,
char *VLR,
char *LVR,
int n)
{
if(n == 0)
t = NULL;
else
{
int i=0;
while(VLR[0] != LVR[i])
{
i++;
}
t = new BinTreeNode<Type>(LVR[i]);
CreateBinTreeByPreMid(t->leftChild,VLR+1,LVR,i);
CreateBinTreeByPreMid(t->rightChild,VLR+i+1,LVR+i+1,n-i-1);
}
}
//求深度
int Height(BinTreeNode<Type> *t)const
{
if(!t)
return 0;
int len1=Height(t->leftChild);
int len2=Height(t->rightChild);
return len1>len2 ? (len1+1) :(len2+1);
}
//二叉树元素的个数
int Size(BinTreeNode<Type> *t)const
{
if(t == NULL)
return 0;
else
{
return Size(t->leftChild)+Size(t->rightChild)+1;
}
}
//判等
bool Equal(BinTreeNode<Type> *t1,BinTreeNode<Type> *t2)
{
if(t1==NULL && t2==NULL)
return true;
else if(t1!=NULL && t2!=NULL
&& t1->data==t2->data
&& Equal(t1->leftChild,t2->leftChild)
&& Equal(t1->rightChild,t2->rightChild))
return true;
else
return false;
}
//拷贝
BinTreeNode<Type>* Copy(BinTreeNode<Type> *t)
{
if(t == NULL)
return NULL;
else
{
BinTreeNode<Type> *s = new BinTreeNode<Type>(t->data);
s->leftChild = Copy(t->leftChild);
s->rightChild = Copy(t->rightChild);
return s;
}
}
//求父节点
BinTreeNode<Type>* Parent(BinTreeNode<Type> *t,BinTreeNode<Type> *p)
{
if(t==NULL || p==NULL)
return NULL;
if(t->leftChild==p || t->rightChild==p)
return t;
else
{
//
BinTreeNode<Type> *q = Parent(t->leftChild,p);
if(q != NULL)
return q;
return Parent(t->rightChild,p);
}
}
//查找二叉树的元素
BinTreeNode<Type>* Find(BinTreeNode<Type>*t,Type key)
{
if(t==NULL)
return NULL;
if(t->data == key)
return t;
else
{
BinTreeNode<Type> *p = Find(t->leftChild,key);
if(p != NULL)
return p;
return Find(t->rightChild,key);
}
}
//右节点
BinTreeNode<Type>* RightChild(BinTreeNode<Type> *t,BinTreeNode<Type> *p)
{
if(t==NULL || p==NULL)
return NULL;
else
return p->rightChild;
}
//左节点
BinTreeNode<Type>* LeftChild(BinTreeNode<Type> *t,BinTreeNode<Type> *p)
{
if(t==NULL || p==NULL)
return NULL;
else
return p->leftChild;
}
bool IsEmpty(BinTreeNode<Type> *t)
{
return t==NULL?true:false;
}
BinTreeNode<Type>* Root(BinTreeNode<Type> *t)
{
//return t;
if(t == NULL)
return NULL;
else
return t;
}
//层次遍历二叉树 -- 队列创建
void LevelOrder(BinTreeNode<Type> *t)const
{
if(t == NULL)
return;
else
{
queue<BinTreeNode<Type>* > Q;
BinTreeNode<Type> *p = t;
Q.push(p);
while(!Q.empty())
{
cout<<p->data;
if(p->leftChild != NULL)
Q.push(p->leftChild);
if(p->rightChild != NULL)
Q.push(p->rightChild);
Q.pop();
if(!Q.empty())
{p = Q.front(); }
}
}
}
//先序遍历
void PreOrder(BinTreeNode<Type> *t)const
{
if(t != NULL)
{
cout<<t->data;
PreOrder(t->leftChild);
PreOrder(t->rightChild);
}
}
//中序遍历
void InOrder(BinTreeNode<Type> *t)const
{
if(t != NULL)
{
InOrder(t->leftChild);
cout<<t->data;
InOrder(t->rightChild);
}
}
//后序遍历
void PostOrder(BinTreeNode<Type> *t)const
{
if(t != NULL)
{
PostOrder(t->leftChild);
PostOrder(t->rightChild);
cout<<t->data;
}
}
//非递归前序遍历二叉树
void NPreOrder(BinTreeNode<Type> *t)const //非递归 stack
{
if(t == NULL)
return;
stack<BinTreeNode<Type>*> S;
BinTreeNode<Type> *p = t;
while(!S.empty() || p)
{
if(p != NULL)
{
S.push(p); //入栈
cout<<p->data; //访问根节点
p = p->leftChild; //访问左节点
}
else
{
p = S.top();
S.pop();
p = p->rightChild;
}
}
}
//非递归 中序遍历二叉树
void NInOrder(BinTreeNode<Type> *t)const
{
if(t == NULL)
return;
stack<BinTreeNode<Type>*> S;
BinTreeNode<Type> *p = t;
while(!S.empty() || p)
{
if(p != NULL)
{
S.push(p);
p = p->leftChild;
}
else
{
p = S.top();
cout<<p->data;
S.pop();
p = p->rightChild;
}
}
}
//非递归后序遍历二叉树
void NPostOrder(BinTreeNode<Type> *t)const
{
stack<BinTreeNode<Type> *> S;
BinTreeNode<Type> *p=t; //当前节点
BinTreeNode<Type> *pre=NULL; //指向前一个被访问的节点
while(p || !S.empty())
{
while(p != NULL) //一直走到左子树为空时
{
S.push(p);
p=p->leftChild;
}
//当前节点的右子树为空或者已被访问,则访问当前节点
p=S.top();
if(p->rightChild==NULL || p->rightChild == pre)
{
// p=S.top();
cout<<p->data;
pre = p;
S.pop();
p=NULL;
}
else //否则访问右子树
p=p->rightChild;
}
}
private:
//先序创建二叉树
BinTreeNode<Type>* CreateBinTree1(char *&str)
{
if(*str == StopFlag)
return NULL;
else
{
BinTreeNode<Type> *t = new BinTreeNode<Type>(*str);
t->leftChild = CreateBinTree1(++str);
t->rightChild = CreateBinTree1(++str);
return t;
}
}
void CreateBinTree(BinTreeNode<Type> *&t, const char *&str)
{
if(*str == StopFlag)
t = NULL;
else
{
t = new BinTreeNode<Type>(*str);
CreateBinTree(t->leftChild,++str);
CreateBinTree(t->rightChild,++str);
}
}
void CreateBinTree(BinTreeNode<Type> *&t)
{
Type item;
cin>>item;
if(item == StopFlag)
t = NULL;
else
{
t = new BinTreeNode<Type>(item);
CreateBinTree(t->leftChild);
CreateBinTree(t->rightChild);
}
}
//ABC##DE##F##G#H##
//ABCDEFGH –前序
//CBEDFAGH –中序
//CEFDBHGA – 后序
//主函数功能的实现
void main()
{
char *str = "ABC##DE##F##G#H##";
char *VLR = "ABCDEFGH";
char *LVR = "CBEDFAGH";
char *LRV = "CEFDBHGA";
BinTree<char> mytree('#');
mytree.CreateBinTree();
//mytree.CreateBinTree(str);
mytree.CreateBinTree(str);
cout<<mytree.Height()<<endl;
mytree.PreOrder();
cout<<endl;
mytree.InOrder();
cout<<endl;
mytree.PostOrder();
cout<<endl;
mytree.CreateBinTreeByPreMid(VLR,LVR,8);
mytree.CreateBinTreeByMidPost(LVR,LRV,8);
}