#include<iostream>
#include<cstdlib>
#include<stack>
#include<queue>
using namespace std;
typedef struct BinTreeNode
{
char data;
struct BinTreeNode *leftChild;
struct BinTreeNode *rightChild;
}*myTree;
void CreateBinTree(myTree tree);
void preorderTraversal(myTree tree);
void inorderTraversal(myTree tree);
void orderTraversal(myTree tree);
void PreOrder_NoRecurve1(myTree p); //先序遍历
void InOrder_NoRecurve(myTree p); //中序遍历
void PostOrder_NoRecurve(myTree p); //后序遍历
void LevelOrder(myTree p);
int main()
{
cout<<"请输入你要创建的二叉树(以#结束):";
myTree root = NULL;
CreateBinTree(root);
return 0;
}
void CreateBinTree(myTree root)
{
stack<myTree> s;
int k;
myTree p,t;
char ch;
cin>>ch;
while(ch != '#')
{
switch(ch)
{
case '(':
s.push(p);
k=1;
break;
case ')':
s.pop();
break;
case ',':
k=2;
break;
default:
p = (myTree)malloc(sizeof(BinTreeNode));
p->leftChild = NULL;
p->rightChild = NULL;
if (root == NULL)
{
root = p;
p->data = ch;
}
else if (k == 1)
{
t = s.top();
t->leftChild = p;
p->data = ch;
}
else
{
t = s.top();
t->rightChild = p;
p->data = ch;
}
}
cin>>ch;
}
cout<<endl<<"采用递归遍历二叉树"<<endl;
cout<<"使用中左右输出:";
preorderTraversal(root);
cout<<endl;
cout<<"使用左中右输出:";
inorderTraversal(root);
cout<<endl;
cout<<"使用左右中输出:";
orderTraversal(root);
cout<<endl<<endl<<"采用栈遍历二叉树:"<<endl;
cout<<"使用先序输出:";
PreOrder_NoRecurve1(root);
cout<<endl;
cout<<"使用中序输出: ";
InOrder_NoRecurve(root);
cout<<endl;
cout<<"使用后序输出:";
PostOrder_NoRecurve(root);
cout<<endl;
cout<<endl<<"采用层次遍历(队列)二叉树:" <<endl;
LevelOrder(root);
cout<<endl;
}
myTree constructABinaryTree() { //创建一个二叉树
myTree root = (myTree)malloc(sizeof(BinTreeNode));
root->data = 5;
root->leftChild = NULL;
root->leftChild = NULL;
myTree leftChild = (myTree)malloc(sizeof(BinTreeNode));
leftChild->data = 7;
leftChild->leftChild = NULL;
leftChild->rightChild = NULL;
root->leftChild = leftChild;
myTree rightChild = (myTree)malloc(sizeof(BinTreeNode));
rightChild->data = 6;
rightChild->leftChild = NULL;
rightChild->rightChild = NULL;
root->rightChild = rightChild;
myTree twoLeftChild = (myTree)malloc(sizeof(BinTreeNode));
twoLeftChild->data = 4;
twoLeftChild->leftChild = NULL;
twoLeftChild->rightChild = NULL;
leftChild->leftChild = twoLeftChild;
myTree leftRightChild = (myTree)malloc(sizeof(BinTreeNode));
leftRightChild->data = 3;
leftRightChild->leftChild = NULL;
leftRightChild->rightChild = NULL;
leftChild->rightChild = leftRightChild;
return root;
}
void preorderTraversal(myTree tree)
{
cout << tree->data << " ";
if (tree->leftChild != NULL) preorderTraversal(tree->leftChild);
if (tree->rightChild != NULL)preorderTraversal(tree->rightChild);
}
void inorderTraversal(myTree tree)
{
if (tree->leftChild != NULL) inorderTraversal(tree->leftChild);
cout << tree->data << " ";
if (tree->rightChild != NULL)inorderTraversal(tree->rightChild);
}
void orderTraversal(myTree tree)
{
if (tree->leftChild != NULL) orderTraversal(tree->leftChild);
if (tree->rightChild != NULL)orderTraversal(tree->rightChild);
cout << tree->data<<" ";
}
//前序遍历
void PreOrder_NoRecurve1(myTree p)
{
stack<myTree> s;
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 InOrder_NoRecurve(myTree p)
{
stack<myTree> s;
do
{
while (p!=NULL)
{
s.push(p);
p = p->leftChild;
}
if (!s.empty())
{
p = s.top();
s.pop();
cout << p->data << " ";
p = p->rightChild;
}
}
while (p!=NULL||!s.empty());
}
//后序遍历
void PostOrder_NoRecurve(myTree p)
{
if (p == NULL)
return ;
stack<myTree> s;
s.push(p);
myTree lastPop = NULL;
while (!s.empty())
{
while (s.top()->leftChild != NULL)
s.push(s.top()->leftChild);
while (!s.empty())
{
if (lastPop == s.top()->rightChild || s.top()->rightChild == NULL)
{
cout << s.top()->data << " ";
lastPop = s.top();
s.pop();
}
else if (s.top()->rightChild != NULL)
{
s.push(s.top()->rightChild);
break;
}
}
}
}
void LevelOrder(myTree p)
{
queue<myTree> Q;
Q.push(p);
myTree t;
while (!Q.empty())
{
t = Q.front();
Q.pop();
cout << t->data << " ";
if (t->leftChild != NULL)
{
Q.push(t->leftChild);
}
if (t->rightChild != NULL)
{
Q.push(t->rightChild);
}
}
}