#include<iostream>
#include<queue>
#include<stack>
using namespace std;
struct BinaryTreeNode
{
char data;
BinaryTreeNode* leftChild;
BinaryTreeNode* rightChild;
};
struct stackNode
{
BinaryTreeNode* ptr;
char tag;//tag=0标志进入左子树,tag=1标志进入右子树
};
class BinaryTree
{
public:
//根据完全前序遍历创建二叉树
void createBinaryTree(BinaryTreeNode* &root)
{
root=new BinaryTreeNode();
char newData;
cin>>newData;
if(newData=='@')
{
root=NULL;
}
else
{
root->data=newData;
createBinaryTree(root->leftChild);
createBinaryTree(root->rightChild);
}
}
//递归实现前序遍历
void preTraversal(BinaryTreeNode* root)
{
if(root!=NULL)
{
cout<<root->data;
preTraversal(root->leftChild);
preTraversal(root->rightChild);
}
}
//递归实现中序遍历
void midTraversal(BinaryTreeNode* root)
{
if(root!=NULL)
{
preTraversal(root->leftChild);
cout<<root->data;
preTraversal(root->rightChild);
}
}
//递归实现后续遍历
void lastTraversal(BinaryTreeNode* root)
{
if(root!=NULL)
{
lastTraversal(root->leftChild);
lastTraversal(root->rightChild);
cout<<root->data;
}
}
//非递归实现前序遍历
void pre(BinaryTreeNode* root)
{
if(root!=NULL)
{
stack<BinaryTreeNode*> S;
BinaryTreeNode* p=root;
S.push(NULL);
while(p!=NULL)
{
cout<<p->data;
if(p->rightChild!=NULL)
{
S.push(p->rightChild);
}
if(p->leftChild!=NULL)
{
p=p->leftChild;
}
else
{
p=S.top();
S.pop();
}
}
}
}
//非递归实现中序遍历
void mid(BinaryTreeNode* root)
{
stack<BinaryTreeNode*> S;
BinaryTreeNode* p=root;
do
{
while(p!=NULL)
{
S.push(p);
p=p->leftChild;
}
if(!S.empty())
{
p=S.top();
cout<<p->data;
S.pop();
p=p->rightChild;
}
}
while(p!=NULL||!S.empty());
}
//非递归实现后序遍历
void last(BinaryTreeNode* root)
{
stack<stackNode> S;
stackNode sn;
BinaryTreeNode* p=root;
do
{
while(p!=NULL)
{
sn.ptr=p;
sn.tag='L';
S.push(sn);
p=p->leftChild;
}
while(!S.empty())
{
sn=S.top();
p=sn.ptr;
S.pop();
if(sn.tag=='L')
{
sn.tag='R';
S.push(sn);
p=p->rightChild;
break;
}
if(sn.tag=='R')
{
cout<<p->data;
}
}
}
while(!S.empty());
}
//层次遍历
void levelTraversal(BinaryTreeNode* root)
{
if(root!=NULL)
{
queue<BinaryTreeNode*> Q;
BinaryTreeNode* p=root;
Q.push(p);
while(!Q.empty())
{
p=Q.front();
Q.pop();
cout<<p->data;
if(p->leftChild!=NULL)
{
Q.push(p->leftChild);
}
if(p->rightChild!=NULL)
{
Q.push(p->rightChild);
}
}
}
}
//计算节点总数
int nodeCount(BinaryTreeNode* &root)
{
if(root==NULL)
{
return 0;
}
else
{
return nodeCount(root->leftChild)+nodeCount(root->rightChild)+1;
}
}
//计算二叉树的高度
int treeHight(BinaryTreeNode* &root)
{
if(root==NULL)
{
return 0;
}
else
{
int LH=treeHight(root->leftChild);
int RH=treeHight(root->rightChild);
return LH > RH ? LH+1 : RH+1;
}
}
};
int main()
{
BinaryTree tree;
BinaryTreeNode* treeRoot;
cout<<"完全前序序列创建二叉树:";
tree.createBinaryTree(treeRoot);
cout<<endl;
cout<<"前序遍历序列(递归):";
tree.preTraversal(treeRoot);
cout<<endl;
cout<<"前序遍历序列(非递归):";
tree.pre(treeRoot);
cout<<endl;
cout<<"中序遍历序列(递归):";
tree.midTraversal(treeRoot);
cout<<endl;
cout<<"中序遍历序列(非递归):";
tree.mid(treeRoot);
cout<<endl;
cout<<"后序遍历序列(递归):";
tree.lastTraversal(treeRoot);
cout<<endl;
cout<<"后序遍历序列(非递归):";
tree.last(treeRoot);
cout<<endl;
cout<<"层次遍历序列:";
tree.levelTraversal(treeRoot);
cout<<endl;
cout<<"节点总个数:";
cout<<tree.nodeCount(treeRoot);
cout<<endl;
cout<<"二叉树的高度:";
cout<<tree.treeHight(treeRoot);
cout<<endl;
return 0;
}
//完全前序序列创建二叉树:ab@d@@c@@
//
//前序遍历序列:abdc
//中序遍历序列:bdac
//后序遍历序列:dbca
//节点总个数:4
//二叉树的高度:3
//层次遍历序列:abcd