#include "iostream.h"
#include "string.h"
#define maxstacksize 100
typedef struct
{//the struct of the Student
char name[20];
int num;
}Student;
Student student[100];
typedef struct TreeNode
{//define the structure of the chain binary chain table
Student data;
TreeNode *LChild;
TreeNode *RChild;
}BinaryTreeNode;
typedef struct
{//the structure of the Stype
BinaryTreeNode *ptr;
bool B;
}Stype;
typedef struct
{//the structure of the stack
Stype *element;
int top;
int maxsize;
}Stack;
Stack S;
void CreatStack (Stack &S)
{//creat a stack S whose capasity is maxstacksize
S.maxsize=maxstacksize;
S.element=new Stype[S.maxsize];
S.top=-1;
}
int IsEmpty(Stack &S)
{//judge the stack S is empty or not
if(S.top==-1) return 1;
return 0;
}
int IsFull(Stack &S)
{//judge the stack S is full or not
if(S.top>=S.maxsize-1) return 1;
return 0;
}
int Push(Stack &S,Stype &x)
{//put X into stack S, and returned to the post - stack - state values
if(IsFull(S)) return 0;
S.top++;
S.element[S.top]=x;
return 0;
}
int Pop(Stack &S,Stype &x)
{//taking S stack top value to X and return after the stack state
if(IsEmpty(S)) return 0;
x=S.element[S.top];
S.top--;
return 1;
}
BinaryTreeNode *MakeNode(Student &x)
{//constant nodes
BinaryTreeNode *ptr;
ptr = new BinaryTreeNode;
if(!ptr) return NULL;
ptr->data=x;
ptr->LChild=NULL;
ptr->RChild=NULL;
return ptr;
}
void MakeBinaryTree(BinaryTreeNode *root,BinaryTreeNode *left,BinaryTreeNode *right)
{//construct a Binarytree
root->LChild=left;
root->RChild=right;
}
void PreOrder(BinaryTreeNode *BT)
{//The preorder traversal of the non - recursion algorithm of the BinaryTree
Stype temp;
Stack S;
BinaryTreeNode *p=BT;
CreatStack(S);
while(p||!IsEmpty(S))
{
if (p)
{
cout<<" "<<p->data.num << " "<<'\t'; //access the root node
cout<<" "<<p->data.name << " "<<'\t';
cout<<endl;
temp.ptr =p;
Push(S,temp);
p = p->LChild;
}
else
if (!IsEmpty(S))
{
Pop(S, temp);
p=temp.ptr;
p=p->RChild;
}
}
cout<<endl;
}
void InOrder(BinaryTreeNode *BT)
{//The inorder traversal of the non - recursion algorithm of the BinaryTree
Stack S;
Stype temp;
BinaryTreeNode *p=BT;
CreatStack(S);
do
{
while(p)
{
temp.ptr=p;
Push(S,temp);
p=p->LChild;
}
if(!IsEmpty(S))
{
Pop(S,temp);
p=temp.ptr;
cout<<" "<<p->data.num<<'\t';
cout<<" "<<p->data.name<<'\t';
cout<<endl;
p=p->RChild;
}
}
while((p)||!IsEmpty(S));
cout<<endl;
}
void PostOrder(BinaryTreeNode *BT)
{//The postorder traversal of the non - recursion algorithm of the BinaryTree
Stype temp;
Stack S;
BinaryTreeNode *p=BT;
CreatStack(S);
while ((p)||!IsEmpty(S))
if (p)
{
temp.B = false;
temp.ptr =p;
Push (S,temp);
p = p->LChild;
}
else
if (!IsEmpty(S))
{
Pop(S,temp);
p=temp.ptr;
if (temp.B)
{
cout<<" "<<p->data.num<<'\t';
cout<<" "<<p->data.name<<'\t';
cout<<endl;
p = NULL;
}
else
{
temp.B = true;
Push(S, temp);
p= p->RChild;
}
}
cout<<endl;
}
void Information_Producer() //the information of the producer
{
cout<<" ********************************************************* "<<endl;
cout<<" The Algorithm of BianryTree "<<endl;
cout<<" Producer : Liao Jiaping "<<endl;
cout<<" Class : IM & IS 0904 "<<endl;
cout<<" Number : 0909030406 "<<endl;
cout<<" Teacher : Wang Shaobo "<<endl;
cout<<" ********************************************************* "<<endl;
}
void Menu()
{
cout<<"*******************************************************************************"<<endl;
cout<<"* 请做出您的选择: *"<<endl;
cout<<"* 1.二叉树前序遍历(非递归) *"<<endl;
cout<<"* 2.二叉树中序遍历(非递归) *"<<endl;
cout<<"* 3.二叉树后续遍历(非递归) *"<<endl;
cout<<"* 0.退出. *"<<endl;
cout<<"*******************************************************************************"<<endl;
}
int main()
{
char re_choose[]={"\n选择非法,请输入正确的编号...\n"};
Information_Producer();
int i;
BinaryTreeNode *treeNode[10];
char name[20][20]={"LEBRON","IVERSON","NUSH","DUNCAN","YAO","KOBY","PARK","ROY","ALLAN","PUAL"};
int num[10]={23,3,13,21,11,24,17,7,25,3};
for(i=0;i<10;i++)
{
student[i].num=num[i];
strcpy(student[i].name,name[i]);
}
for(i=0;i<10;i++)
{
treeNode[i]=MakeNode(student[i]);
}
MakeBinaryTree(treeNode[0],treeNode[1],treeNode[2]);
MakeBinaryTree(treeNode[1],treeNode[3],treeNode[4]);
MakeBinaryTree(treeNode[2],treeNode[5],treeNode[6]);
MakeBinaryTree(treeNode[3],treeNode[7],treeNode[8]);
MakeBinaryTree(treeNode[4],NULL,treeNode[9]);
cout<<"This is the binarytree"<<endl;
cout<<"\t\t 23.LEBRON\n";
cout<<"\t\t |--------|--------|\n";
cout<<"\t\t 3.IVERSON 13.NUSH \n";
cout<<"\t\t |----|----| |----|----|\n";
cout<<"\t\t 21.DUNCAN 11.YAO 24.KOBY 2.PARK \n";
cout<<"\t\t |----|----| |---|\n";
cout<<"\t\t 7.ROY 25.ALLAN 3.PUAL \n";
cout<<endl;
Menu();
while(1)
{
int j;
cout<<"请选择"<<endl;
cin>>j;
switch(j)
{
case 1:
cout<<"The preorder traversal of the non - recursion algorithm of the BinaryTree :\n";
cout<<"The answer is : "<<endl;
PreOrder(treeNode[0]); // Binary Tree preorder traversal of the non - recursion algorithm
break;
case 2:
cout<<"The inorder traversal of the non - recursion algorithm of the BinaryTree :\n";
cout<<"The answer is : "<<endl;
InOrder(treeNode[0]);//Binary tree the foreword spreads the calendar the non - recursion algorithm
break;
case 3:
cout<<"The postorder traversal of the non - recursion algorithm of the BinaryTree :\n";
cout<<"The answer is : "<<endl;
PostOrder(treeNode[0]);
break;
case 0:
return 0;
break;
default:
cout <<re_choose<<endl;
break;
}
}
return 0;
}