?? 二叉樹的遍歷ywc.cpp
字號:
#include<string.h>
#include<ctype.h>
#include<malloc.h>
#include<limits.h>
#include<stdio.h>
#include<stdlib.h>
#include<io.h>
#include<math.h>
#include<process.h>
#include<iostream.h>
// 函數結果狀態代碼
#define TRUE 1
#define FALSE 0
#define OK 1
#define ERROR 0
#define INFEASIBLE -1
typedef int Status; // Status是函數的類型,其值是函數結果狀態代碼,如OK等
typedef int Boolean;
#define CHAR 0 // 整型(二者選一)
#if CHAR
typedef char TElemType;
TElemType Nil=' '; // 設字符型以空格符為空
#else
typedef int TElemType;
TElemType Nil=0; // 設整型以0為空
#endif
#define MAX_TREE_SIZE 100 // 二叉樹的最大結點數
typedef TElemType SqBiTree[MAX_TREE_SIZE]; // 0號單元存儲根結點
struct position
{
int level,order; // 結點的層,本層序號(按滿二叉樹計算)
};
#include"樹.cpp"
Status visit(TElemType e)
{
cout<<e<<' ';
return OK;
}
void main()
{
Status i;
int j;
position p;
TElemType e;
SqBiTree T,s;
InitBiTree(T);
CreateBiTree(T);
cout<<"建立二叉樹后,樹空否?"<<BiTreeEmpty(T)<<"(1:是 0:否) 樹的深度="<<BiTreeDepth(T)<<endl;
i=Root(T,e);
if(i)
cout<<"二叉樹的根為:"<<e<<endl;
else
cout<<"樹空,無根"<<endl;
cout<<"層序遍歷二叉樹:"<<endl;
LevelOrderTraverse(T,visit);
cout<<"中序遍歷二叉樹:"<<endl;
InOrderTraverse(T,visit);
cout<<"后序遍歷二叉樹:"<<endl;
PostOrderTraverse(T,visit);
cout<<"請輸入待修改結點的層號 本層序號: ";
cin>>p.level>>p.order;
e=Value(T,p);
cout<<"待修改結點的原值為"<<e<<"請輸入新值: ";
cin>>e;
Assign(T,p,e);
cout<<"先序遍歷二叉樹:"<<endl;
PreOrderTraverse(T,visit);
cout<<"結點"<<e<<"的雙親為"<<Parent(T,e)<<",左右孩子分別為";
cout<<LeftChild(T,e)<<","<<RightChild(T,e)<<",左右兄弟分別為";
cout<<LeftSibling(T,e)<<","<<RightSibling(T,e)<<endl;
InitBiTree(s);
cout<<"建立右子樹為空的樹s:"<<endl;
CreateBiTree(s);
cout<<"樹s插到樹T中,請輸入樹T中樹s的雙親結點 s為左(0)或右(1)子樹: ";
cin>>e>>j;
InsertChild(T,e,j,s);
Print(T);
cout<<"刪除子樹,請輸入待刪除子樹根結點的層號 本層序號 左(0)或右(1)子樹: ";
cin>>p.level>>p.order>>j;
DeleteChild(T,p,j);
Print(T);
ClearBiTree(T);
cout<<"清除二叉樹后,樹空否?"<<BiTreeEmpty(T)<<"(1:是 0:否) 樹的深度="<<BiTreeDepth(T)<<endl;
i=Root(T,e);
if(i)
cout<<"二叉樹的根為:"<<e<<endl;
else
cout<<"樹空,無根"<<endl;
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -