?? tree.cpp
字號(hào):
// Tree.cpp : Defines the entry point for the console application.
//
/*
實(shí)驗(yàn)題目:二叉的遍歷
開(kāi)發(fā)思想:本實(shí)驗(yàn)分別用了遞歸和非遞歸來(lái)實(shí)現(xiàn)前序、中序、后序
方式的遍歷二叉樹(shù),還實(shí)現(xiàn)了層次遍歷,并求出樹(shù)高。
在這些實(shí)現(xiàn)中,主要用到的是隊(duì)列鏈和鏈棧。
開(kāi)發(fā)人員:葛興高
開(kāi)發(fā)日期:2004、10、29
*/
#include "stdafx.h"
void main()
{
BuildTree B;
int Hight;
char y;
int flag=1;
int choice=1;
cout<<"\t\t為了確保程序可以正常進(jìn)行,確認(rèn)你輸入的二叉樹(shù)是正確的!\n";
cout<<"\t\t下面給出幾個(gè)例子,可以參照輸入:\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 *******注意要以 # 結(jié)束*******\n";
cout<<"\n\t\t你想要進(jìn)入程序嗎?(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 ...建立二叉樹(shù)\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 ...求樹(shù)高\(yùn)n";
cout<<"\t\t\t0...退出程序\n";
}
cout<<"\t\t\t請(qǐng)輸入你的選擇(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********請(qǐng)先建二叉樹(shù)!********\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********請(qǐng)先建二叉樹(shù)!********\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********請(qǐng)先建二叉樹(shù)!********\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********請(qǐng)先建二叉樹(shù)!********\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********請(qǐng)先建二叉樹(shù)!********\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********請(qǐng)先建二叉樹(shù)!********\n\n";
break;
case 8:
if(flag==0)
{
cout<<endl<<"層次遍歷:\n";
B.LayerOrder();
cout<<endl;
// getch();
}
else
cout<<"\n\t\t\t********請(qǐng)先建二叉樹(shù)!********\n\n";
break;
case 9:
if(flag==0)
{
cout<<endl<<"樹(shù)高:\n";
Hight=B.GetHigh(B.head,1);
cout<<Hight;
cout<<endl;
// getch();
}
else
cout<<"\n\t\t\t********請(qǐng)先建二叉樹(shù)!********\n\n";
break;
case 0:
break;
default:
choice=1;
}
}
}
}
?? 快捷鍵說(shuō)明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -