📄 tree.cpp
字号:
// Tree.cpp : Defines the entry point for the console application.
//
/*
实验题目:二叉的遍历
开发思想:本实验分别用了递归和非递归来实现前序、中序、后序
方式的遍历二叉树,还实现了层次遍历,并求出树高。
在这些实现中,主要用到的是队列链和链栈。
开发人员:葛兴高
开发日期:2004、10、29
*/
#include "stdafx.h"
void main()
{
BuildTree B;
int Hight;
char y;
int flag=1;
int choice=1;
cout<<"\t\t为了确保程序可以正常进行,确认你输入的二叉树是正确的!\n";
cout<<"\t\t下面给出几个例子,可以参照输入:\n";
cout<<"\t\t\t ABD..E..C.F..#\n";
cout<<"\t\t\t ABDF..G..E..C#\n";
cout<<"\t\t\t ABD..EG..H..CF...#\n\n";
cout<<"\t\t *******注意要以 # 结束*******\n";
cout<<"\n\t\t你想要进入程序吗?(y/n):";
cin>>y;
cout<<"\n\n\n\n\n\n\n\n\n\n\n\n\n\n";
if(y=='y'||y=='Y')
{
while(choice!=0)
{
if(flag==1){
cout<<"\t\t\t1 ...建立二叉树\n";
cout<<"\t\t\t2 ...递归前序遍历\n";
cout<<"\t\t\t3 ...非递归前序遍历\n";
cout<<"\t\t\t4 ...递归中序遍历\n";
cout<<"\t\t\t5 ...非递归中序遍历\n";
cout<<"\t\t\t6 ...递归后序遍历\n";
cout<<"\t\t\t7 ...非递归后序遍历\n";
cout<<"\t\t\t8 ...层次遍历\n";
cout<<"\t\t\t9 ...求树高\n";
cout<<"\t\t\t0...退出程序\n";
}
cout<<"\t\t\t请输入你的选择(0-9):";
cin>>choice;
switch(choice)
{
case 1:
B.Create();
flag=0;
break;
case 2:
if(flag==0)
{
cout<<"递归前序遍历:\n";
B.PreOrder_1(B.head,1,0);
cout<<endl;
// getch();
}
else
cout<<"\n\t\t\t********请先建二叉树!********\n\n";
break;
case 3:
if(flag==0)
{
cout<<endl<<"非递归前序遍历:\n";
B.PreOrder_2(B.head);
cout<<endl;
// getch();
}
else
cout<<"\n\t\t\t********请先建二叉树!********\n\n";
break;
case 4:
if(flag==0)
{
cout<<endl<<"递归中序遍历:\n"<<endl;
B.InOrder_1(B.head,1,0);
cout<<endl;
// getch();
}
else
cout<<"\n\t\t\t********请先建二叉树!********\n\n";
break;
case 5:
if(flag==0)
{
cout<<endl<<"非递归中序遍历:\n"<<endl;
B.InOrder_2(B.head);
cout<<endl;
// getch();
}
else
cout<<"\n\t\t\t********请先建二叉树!********\n\n";
break;
case 6:
if(flag==0)
{
cout<<endl<<"递归后序遍历:\n";
B.PostOrder_1(B.head,1,0);
cout<<endl;
// getch();
}
else
cout<<"\n\t\t\t********请先建二叉树!********\n\n";
break;
case 7:
if(flag==0)
{
cout<<endl<<"非递归后序遍历:\n";
B.PostOrder_1(B.head,1,0);
cout<<endl;
// getch();
}
else
cout<<"\n\t\t\t********请先建二叉树!********\n\n";
break;
case 8:
if(flag==0)
{
cout<<endl<<"层次遍历:\n";
B.LayerOrder();
cout<<endl;
// getch();
}
else
cout<<"\n\t\t\t********请先建二叉树!********\n\n";
break;
case 9:
if(flag==0)
{
cout<<endl<<"树高:\n";
Hight=B.GetHigh(B.head,1);
cout<<Hight;
cout<<endl;
// getch();
}
else
cout<<"\n\t\t\t********请先建二叉树!********\n\n";
break;
case 0:
break;
default:
choice=1;
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -