一、樹
1,樹型結構
樹型結構是一種非線性結構,與之前的線性表,堆疊,佇列還有字串,陣列,廣義表不同,相比與之前一對一的線性結構,樹型結構展示為一對多的非線性結構,
2,樹的定義,術語,森林
太多了,不打了,,,直接二叉樹
二、二叉樹
二叉樹,這個樹比較重要,結構簡單,規律性強,普通樹也可以通過轉化為二叉樹進行運算,簡化步驟,其中運用二叉樹的演算法也有很多,像最優二叉樹哈夫曼樹,樹狀陣列,線段樹等等,都是在二叉樹上進行操作,
1.幾個性質
1,一個節點最多只用有兩個孩子,所以在二叉樹的第i層上至多有個結點

2,深度為k的二叉樹至多有個結點
3,對于任何一棵二叉樹T,如果其葉子樹為,度為2的結點數為
,則
咋算的
總邊數:
1,每個孩子和雙親都有一條連線,只有根沒有雙親,所以n個節點對于有條邊,
2,再者總邊數還可以為度為2結點的邊和度為1節點的邊的和
所以,又因為節點數等于度為2,度為1,度為0的
帶入就為
,
4,具有n個結點的完全二叉樹的深度為
1,滿二叉樹:深度為k,且結點數為的二叉樹
2,完全二叉樹:
我們可以聯想到到在排序那章講的堆排序,堆就是一個完全二叉樹,堆中的某個結點的值總 是大于等于(最大堆)或小于等于(最小堆)其孩子結點的值,第 k 層所有的結點都連續集中 在最左邊,
所以二叉樹的深度為k,除第 k 層外,其它各層 (1~k-1) 的結點數都達到最大個數,
第 k 層所有的結點都連續集中在最左邊,且其中每一個結點都能與滿二叉樹匹配的二叉樹.
2,二叉樹的存盤結構
學習了前面的佇列,堆疊,串等,思考存盤結構可以想到兩個方面:順序存盤,鏈式存盤
1,順序存盤
#define MAXTSIZE 100
typedef char SqbiTree[MAXTSIZE];
SqbiTree bt;
| 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 |
| a | e | b | f | d | c | g | h |
空的結點算上,12個結點,有四層
先畫出滿二叉樹,再填上去
2,鏈式存盤
重點還是看鏈式存盤,,,
typedef struct BiNode {
char data;
struct BiNode* lchild, * rchild;
}Binode,*BiTree;

還可以利用雙向鏈表改寫二叉鏈表的指標域為三叉鏈表,使其可以詢問結點的雙親結點
typedef struct BiNode {
char data;
struct BiNode* lchild, * rchild;
struct BiNode* parent;
}Binode,*BiTree;
3,遍歷二叉樹
1,遍歷二叉樹的方法有六種
根左右,左根右,左右根,根右左,右根左,右左根
后三個和前三個對稱,所以只研究前三個
| 根左右 | 先序(根)遍歷 |
| 左根右 | 中序(根)遍歷 |
| 左右根 | 后序(根)遍歷 |
思考:堆疊和佇列的前綴運算式,中綴運算式,后綴運算式,應該是由二叉樹的三種遍歷方式展現出來的

可以看出上次使用入堆疊操作也可以得到三種運算式的結果,猜測三種遍歷方式也可以使用堆疊進行實作,
1.1先根遍歷:訪問根結點——>左子樹——>右子樹
遞回實作:
void Preordertravers(BiTree T) {
if (T == NULL)return ;
else {
cout << T->data;
Preordertravers(T->lchild);
Preordertravers(T->rchild);
}
}
堆疊實作:思路和遞回一致,先將根壓入堆疊,然后左子樹一并壓入堆疊直到空為止開始彈堆疊,取堆疊頂結點判斷右子樹是否存在,如果存在繼續壓入左子樹操作,
void PrevOrder_stack(Binode* T)
{
stack<Binode*> s;
Binode* Bi = T;
while (Bi || !s.empty())
{
if(Bi)
{
cout << Bi->data;
s.push(Bi);
Bi = Bi->lchild;
}
else {
Bi = s.top();
s.pop();
Bi = Bi->rchild;
}
}
cout << "先序遍歷_堆疊" << endl;
}
1.2中根遍歷:訪問左子樹——>根結點——>右子樹
void Inordertraverse(BiTree T) {
if (T == NULL)return;
else {
Inordertraverse(T->lchild);
cout << T->data;
Inordertraverse(T->rchild);
}
}
堆疊實作:和先序差不多,左子樹壓入堆疊直到葉子開始彈堆疊并輸出,然后再將右子樹壓入堆疊中
void InOrder_stack(Binode* T)
{
stack<Binode*> s;
Binode* Bi = T;
while (Bi || !s.empty())
{
if (Bi)
{
s.push(Bi);
Bi = Bi->lchild;
}
else {
Bi = s.top();
cout << Bi->data;
s.pop();
Bi = Bi->rchild;
}
}
cout << "中序遍歷_堆疊" << endl;
}
1.3后根遍歷:訪問左子樹——>右子樹——>根結點
void Postordertraverse(BiTree T) {
if (T == NULL)return;
else {
Postordertraverse(T->lchild);
Postordertraverse(T->rchild);
cout << T->data;
}
}
堆疊實作:也是先把左子樹壓入堆疊直到空,然后判斷是否存在右子樹,如果有用vis記錄該結點,然后開始重復上述操作,繼續將右子樹中的左子樹壓入堆疊,直到為葉子結點
void PastOrder_stack(Binode* T)
{
Binode* Bi = T;
Binode* vis = NULL;
stack<Binode*> s;
while (Bi||!s.empty())
{
if (Bi)
{
s.push(Bi);
Bi = Bi->lchild;
}
else {
Bi = s.top();
if (Bi->rchild && vis != Bi->rchild)
Bi = Bi->rchild;
else if (( Bi->rchild==NULL) || (vis == Bi->rchild))
{
cout << Bi->data;
vis = Bi;
s.pop();
Bi = NULL;
}
}
}
cout << "后序遍歷_堆疊" << endl;
}
1.4層次遍歷:
從上到下,從左到右
void HierarchyOrder(Binode * T)
{
queue<Binode*>q;
q.push(T);
Binode* temp;
while (!q.empty()) {
temp = q.front();
q.pop();
cout << temp->data;
if (temp->lchild != NULL) {
q.push(temp->lchild);
}
if (temp->rchild != NULL) {
q.push(temp->rchild);
}
}
}
2,二叉樹的創建和easyx的可視化操作
二叉樹的創建以先根遍歷的方式
void CreateBiTree(BiTree* T){
char ch;
cin >> ch;
if (ch == '.') {
*T = NULL;
}
else {
*T = (BiTree)malloc(sizeof(Binode));
(*T)->data = ch;
CreateBiTree(&((*T)->lchild));
CreateBiTree(&((*T)->rchild));
}
}
用easyx首先畫一張圖,我想讓這棵樹的根節點從坐標(360,200)的位置開始生成根結點畫個圈,然后先從左子樹開始向他六十度方向連線并為當前結點畫個圈,直到左子樹為空,開始回溯,判斷是否有右子樹,有則向六十度方向畫出右子樹為當前結點畫個圈并重復上述畫左子樹的方法,
當然,因為二叉樹如果左子樹和右子樹的連線角度和線長都相等,這可以理解為一個對稱圖形,那么可能內側的結點會有重疊覆寫,所以,我將角度設為變數,每增加一層,角度都除以2,對應的(x,y)到終點(xf,yf)的距離也縮短1.2倍
程式到這里也這里也只是畫了一張圖,最求高級的我想讓這份作業高級一點,把他變成影片:
線段細化為無數個點排列在一起,我通過兩點公式求出這條顯得函式關系式:
之后我們將x到細化為500份,y由公式可得,通過回圈遍歷每個點可得到生成線段的影片
void PrevOrderDraw(BiTree T, double x, double y, double xt, double yt,double xp,int time,int dep)//xp為角度,
{
if (T)
{
setfillcolor(BLUE);
setbkmode(TRANSPARENT);
fillcircle(x, y, 20);
outtextxy(x - 5, y - 5, T->data);
}
if (T->lchild)//左子樹
{
double i ,j;
for (i = x,j=y; i >=x - xt,j<=y+yt;) {
i -= (x - xt) / 500, j =(-yt/xt)*i-(-yt/xt)*x+y;
line(x,y , i, j);
Sleep(time);
}
setfillcolor(BLUE);
setbkmode(TRANSPARENT);
fillcircle(x, y, 20);
outtextxy(x - 5, y - 5, T->data);
PrevOrderDraw(T->lchild, x - xt, y + yt, xt / 1.2 * abs(cos(xp)), yt / 1.2 * abs(sin(xp)), xp / 2, time + 4 * dep, dep + 1);
}
if (T->rchild)//右子樹
{
double i, j;
for (i = x, j = y; i >= x + xt, j <= y + yt;) {
i += (x + xt) / 500, j = (yt / xt) * i - (yt / xt) * x + y;
line(x, y, i, j);
Sleep(time);
}
setfillcolor(BLUE);
setbkmode(TRANSPARENT);
fillcircle(x, y, 20);
outtextxy(x - 5, y - 5, T->data);
PrevOrderDraw(T->rchild, x + xt, y + yt, xt / 1.2 * abs(cos(xp)), yt / 1.2 * abs(sin(xp)), xp / 2, time + 4 * dep, dep + 1);
}
}
void PrintBiTree(BiTree T)
{
PrevOrderDraw(T, 360,200, 200* abs(sin(PI / 3)), 200* abs(cos(PI / 3)), PI / 3,6,1);
}
考慮到后面線段變短,所以每次睡眠時間通過層數增加,還有每次畫完線需要再畫一次圓蓋掉線的痕跡,不然影響美觀,

二叉樹AB.C..DE.G..F.H..先序遍歷生成影片結束的樣子
當然這只是一棵小二叉樹,對于5層以上的建議大家長度由短到長,角度由小到大,不會顯得太密集,
3,二叉樹的遍歷和easyx可視化
想法很簡單:分別在輸出值上加個結點顏色改變的函式,之前想過用圓變大變小來操作,但是變大變小完線會被蓋掉,這樣我們要重新畫線,代表了我們要訪問他的雙親結點的x,y,這樣需要用到之前說的三叉鏈表存盤,我懶,沒有實作,
注意一下,這里加了xy,所以畫二叉樹那也要更改一下:
typedef struct BiNode {
char data;
double x, y;
struct BiNode* lchild, * rchild;
}Binode,*BiTree;
各種遍歷:
這個變色是閃三下,寫的有點暴力
void circhange(int x, int y, int data)
{
settextcolor(BLACK);
setfillcolor(YELLOW);
setbkmode(TRANSPARENT);
fillcircle(x, y, 20);
outtextxy(x - 5, y - 5, data);
Sleep(400);
setfillcolor(BLUE);
setbkmode(TRANSPARENT);
fillcircle(x, y, 20);
outtextxy(x - 5, y - 5, data);
Sleep(400);
setfillcolor(YELLOW);
setbkmode(TRANSPARENT);
fillcircle(x, y, 20);
outtextxy(x - 5, y - 5, data);
Sleep(400);
}
void Preordertravers(BiTree T) {
if (T == NULL)return ;
else {
cout << T->data;
circhange(T->x, T->y,T->data);
Preordertravers(T->lchild);
Preordertravers(T->rchild);
}
}
void Inordertraverse(BiTree T) {
if (T == NULL)return;
else {
Inordertraverse(T->lchild);
cout << T->data;
circhange(T->x, T->y, T->data);
Inordertraverse(T->rchild);
}
}
void Postordertraverse(BiTree T) {
if (T == NULL)return;
else {
Postordertraverse(T->lchild);
Postordertraverse(T->rchild);
cout << T->data;
circhange(T->x, T->y, T->data);
}
}
void HierarchyOrder(Binode * T)
{
queue<Binode*>q;
q.push(T);
Binode* temp;
while (!q.empty()) {
temp = q.front();
q.pop();
cout << temp->data;
circhange(temp->x, temp->y, temp->data);
if (temp->lchild != NULL) {
q.push(temp->lchild);
}
if (temp->rchild != NULL) {
q.push(temp->rchild);
}
}
}
測驗了一下,每個演算法沒啥問題,接下來就可以實作選擇功能的界面了
4,easyx設計操控界面
首先設定生產二叉樹視窗

在main中打開檔案,利用easyx的滑鼠讀取對四種二叉樹進行讀取并生成
int main() {
initgraph(720, 720, EW_SHOWCONSOLE);
ifstream file;
BiTree T;
file.open("test.txt");
file >> s1 >> s2 >> s3 >> s4;
showjpg();
BeginBatchDraw();
ExMessage msg;
int x[5], y[5], w[5], h[5];
x[0] = 260;
y[0] = 100;
w[0] = 200;
h[0] = 50;
setfillcolor(YELLOW);
fillroundrect(x[0], y[0], x[0] + w[0], y[0] + h[0], 10, 10);
botton_table(x[0], y[0], w[0], h[0], "Bitree_1");
x[1] = 260;
y[1] = 200;
w[1] = 200;
h[1] = 50;
setfillcolor(YELLOW);
fillroundrect(x[1], y[1], x[1] + w[1], y[1] + h[1], 10, 10);
botton_table(x[0], y[0], w[0], h[0], "Bitree_2");
x[2] = 260;
y[2] = 300;
w[2] = 200;
h[2] = 50;
setfillcolor(YELLOW);
fillroundrect(x[2], y[2], x[2] + w[2], y[2] + h[2], 10, 10);
botton_table(x[2], y[2], w[2], h[2], "Bitree_3");
x[3] = 260;
y[3] = 400;
w[3] = 200;
h[3] = 50;
setfillcolor(YELLOW);
fillroundrect(x[3], y[3], x[3] + w[3], y[3] + h[3], 10, 10);
botton_table(x[3], y[3], w[3], h[3], "Bitree_4");
x[4] = 260;
y[4] = 500;
w[4] = 200;
h[4] = 50;
setfillcolor(YELLOW);
fillroundrect(x[4], y[4], x[4] + w[4], y[4] + h[4], 10, 10);
botton_table(x[4], y[4], w[4], h[4], "退出");
bool checkt = 0;
while (1) {
if (peekmessage(&msg, EM_MOUSE)) {
switch (msg.message) {
case WM_LBUTTONDOWN://點擊
if (msg.x >= x[0] && msg.x <= x[0] + w[0] && msg.y >= y[0] && msg.y <= y[0] + h[0] )
{
s = s1;
ip = 0;
cleardevice();
CreateBiTree(&T);
PrintBiTree(T);
Sleep(800);
cleardevice();
showjpg();
Orderchoice(T);
}
if (msg.x >= x[1] && msg.x <= x[1] + w[1] && msg.y >= y[1] && msg.y <= y[1] + h[1] )
{
s = s2;
ip = 0;
cleardevice();
CreateBiTree(&T);
PrintBiTree(T);
Sleep(800);
cleardevice();
showjpg();
Orderchoice(T);
}
if (msg.x >= x[2] && msg.x <= x[2] + w[2] && msg.y >= y[2] && msg.y <= y[2] + h[2])
{
s = s3;
ip = 0;
cleardevice();
CreateBiTree(&T);
PrintBiTree(T);
Sleep(800);
cleardevice();
showjpg();
Orderchoice(T);
}
if (msg.x >= x[3] && msg.x <= x[3] + w[3] && msg.y >= y[3] && msg.y <= y[3] + h[3])
{
s = s4;
ip = 0;
CreateBiTree(&T);
cleardevice();
PrintBiTree(T);
Sleep(800);
cleardevice();
showjpg();
Orderchoice(T);
}
if (msg.x >= x[4] && msg.x <= x[4] + w[4] && msg.y >= y[4] && msg.y <= y[4] + h[4])
{
checkt = true;
}
FlushBatchDraw();
case WM_MOUSEFIRST://移動
if (msg.x >= x[0] && msg.x <= x[0] + w[0] && msg.y >= y[0] && msg.y <= y[0] + h[0])
{
setfillcolor(RED);
fillroundrect(x[0], y[0], x[0] + w[0], y[0] + h[0], 10, 10);
botton_table(x[0], y[0], w[0], h[0], "Bitree_1");
}
else {
setfillcolor(YELLOW);
fillroundrect(x[0], y[0], x[0] + w[0], y[0] + h[0], 10, 10);
botton_table(x[0], y[0], w[0], h[0], "Bitree_1");
}
if (msg.x >= x[1] && msg.x <= x[1] + w[1] && msg.y >= y[1] && msg.y <= y[1] + h[1]) {
setfillcolor(RED);
fillroundrect(x[1], y[1], x[1] + w[1], y[1] + h[1], 10, 10);
botton_table(x[1], y[1], w[1], h[1], "Bitree_2");
}
else {
setfillcolor(YELLOW);
fillroundrect(x[1], y[1], x[1] + w[1], y[1] + h[1], 10, 10);
botton_table(x[1], y[1], w[1], h[1], "Bitree_2");
}
if (msg.x >= x[2] && msg.x <= x[2] + w[2] && msg.y >= y[2] && msg.y <= y[2] + h[2]) {
setfillcolor(RED);
fillroundrect(x[2], y[2], x[2] + w[2], y[2] + h[2], 10, 10);
botton_table(x[2], y[2], w[2], h[2], "Bitree_3");
}
else {
setfillcolor(YELLOW);
fillroundrect(x[2], y[2], x[2] + w[2], y[2] + h[2], 10, 10);
botton_table(x[2], y[2], w[2], h[2], "Bitree_3");
}
if (msg.x >= x[3] && msg.x <= x[3] + w[3] && msg.y >= y[3] && msg.y <= y[3] + h[3]) {
setfillcolor(RED);
fillroundrect(x[3], y[3], x[3] + w[3], y[3] + h[3], 10, 10);
botton_table(x[3], y[3], w[3], h[3], "Bitree_4");
}
else {
setfillcolor(YELLOW);
fillroundrect(x[3], y[3], x[3] + w[3], y[3] + h[3], 10, 10);
botton_table(x[3], y[3], w[3], h[3], "Bitree_4");
}
if (msg.x >= x[4] && msg.x <= x[4] + w[4] && msg.y >= y[4] && msg.y <= y[4] + h[4]) {
setfillcolor(RED);
fillroundrect(x[4], y[4], x[4] + w[4], y[4] + h[4], 10, 10);
botton_table(x[4], y[4], w[4], h[4], "退出");
}
else {
setfillcolor(YELLOW);
fillroundrect(x[4], y[4], x[4] + w[4], y[4] + h[4], 10, 10);
botton_table(x[4], y[4], w[4], h[4], "退出");
}
}
EndBatchDraw();
}
if (checkt) {
break;
}
}
return 0;
}
再定義一個遍歷選擇函式,即選擇并生成完所示二叉樹,并選擇遍歷方式

void Orderchoice(BiTree T) {
showjpg();
BeginBatchDraw();
ExMessage msg;
int x[5], y[5], w[5], h[5];
x[0] = 260;
y[0] = 100;
w[0] = 200;
h[0] = 50;
setfillcolor(YELLOW);
fillroundrect(x[0], y[0], x[0] + w[0], y[0] + h[0], 10, 10);
botton_table(x[0], y[0], w[0], h[0], "先序遍歷");
x[1] = 260;
y[1] = 200;
w[1] = 200;
h[1] = 50;
setfillcolor(YELLOW);
fillroundrect(x[1], y[1], x[1] + w[1], y[1] + h[1], 10, 10);
botton_table(x[0], y[0], w[0], h[0], "中序遍歷");
x[2] = 260;
y[2] = 300;
w[2] = 200;
h[2] = 50;
setfillcolor(YELLOW);
fillroundrect(x[2], y[2], x[2] + w[2], y[2] + h[2], 10, 10);
botton_table(x[2], y[2], w[2], h[2], "后序遍歷");
x[3] = 260;
y[3] = 400;
w[3] = 200;
h[3] = 50;
setfillcolor(YELLOW);
fillroundrect(x[3], y[3], x[3] + w[3], y[3] + h[3], 10, 10);
botton_table(x[3], y[3], w[3], h[3], "層次遍歷");
x[4] = 260;
y[4] = 500;
w[4] = 200;
h[4] = 50;
setfillcolor(YELLOW);
fillroundrect(x[4], y[4], x[4] + w[4], y[4] + h[4], 10, 10);
botton_table(x[4], y[4], w[4], h[4], "退出");
bool checkt = 0;
while (1) {
if (peekmessage(&msg, EM_MOUSE)) {
switch (msg.message) {
case WM_LBUTTONDOWN://點擊
if (msg.x >= x[0] && msg.x <= x[0] + w[0] && msg.y >= y[0] && msg.y <= y[0] + h[0])
{
cleardevice();
Draw(T, 360, 200, 180 * abs(sin(PI / 3)), 180 * abs(cos(PI / 3)), PI / 3);
Preordertravers(T);
cleardevice();
showjpg();
}
if (msg.x >= x[1] && msg.x <= x[1] + w[1] && msg.y >= y[1] && msg.y <= y[1] + h[1])
{
cleardevice();
Draw(T, 360, 200, 180 * abs(sin(PI / 3)), 180 * abs(cos(PI / 3)), PI / 3);
Inordertraverse(T);
cleardevice();
showjpg();
}
if (msg.x >= x[2] && msg.x <= x[2] + w[2] && msg.y >= y[2] && msg.y <= y[2] + h[2])
{
cleardevice();
Draw(T, 360, 200, 180 * abs(sin(PI / 3)), 180 * abs(cos(PI / 3)), PI / 3);
Postordertraverse(T);
cleardevice();
showjpg();
}
if (msg.x >= x[3] && msg.x <= x[3] + w[3] && msg.y >= y[3] && msg.y <= y[3] + h[3])
{
cleardevice();
Draw(T, 360, 200, 180 * abs(sin(PI / 3)), 180 * abs(cos(PI / 3)), PI / 3);
HierarchyOrder(T);
cleardevice();
showjpg();
}
if (msg.x >= x[4] && msg.x <= x[4] + w[4] && msg.y >= y[4] && msg.y <= y[4] + h[4])
{
checkt = true;
}
FlushBatchDraw();
case WM_MOUSEFIRST://移動
if (msg.x >= x[0] && msg.x <= x[0] + w[0] && msg.y >= y[0] && msg.y <= y[0] + h[0])
{
setfillcolor(RED);
fillroundrect(x[0], y[0], x[0] + w[0], y[0] + h[0], 10, 10);
botton_table(x[0], y[0], w[0], h[0], "先序遍歷");
}
else {
setfillcolor(YELLOW);
fillroundrect(x[0], y[0], x[0] + w[0], y[0] + h[0], 10, 10);
botton_table(x[0], y[0], w[0], h[0], "先序遍歷");
}
if (msg.x >= x[1] && msg.x <= x[1] + w[1] && msg.y >= y[1] && msg.y <= y[1] + h[1]) {
setfillcolor(RED);
fillroundrect(x[1], y[1], x[1] + w[1], y[1] + h[1], 10, 10);
botton_table(x[1], y[1], w[1], h[1], "中序遍歷");
}
else {
setfillcolor(YELLOW);
fillroundrect(x[1], y[1], x[1] + w[1], y[1] + h[1], 10, 10);
botton_table(x[1], y[1], w[1], h[1], "中序遍歷");
}
if (msg.x >= x[2] && msg.x <= x[2] + w[2] && msg.y >= y[2] && msg.y <= y[2] + h[2]) {
setfillcolor(RED);
fillroundrect(x[2], y[2], x[2] + w[2], y[2] + h[2], 10, 10);
botton_table(x[2], y[2], w[2], h[2], "后序遍歷");
}
else {
setfillcolor(YELLOW);
fillroundrect(x[2], y[2], x[2] + w[2], y[2] + h[2], 10, 10);
botton_table(x[2], y[2], w[2], h[2], "后序遍歷");
}
if (msg.x >= x[3] && msg.x <= x[3] + w[3] && msg.y >= y[3] && msg.y <= y[3] + h[3]) {
setfillcolor(RED);
fillroundrect(x[3], y[3], x[3] + w[3], y[3] + h[3], 10, 10);
botton_table(x[3], y[3], w[3], h[3], "層次遍歷");
}
else {
setfillcolor(YELLOW);
fillroundrect(x[3], y[3], x[3] + w[3], y[3] + h[3], 10, 10);
botton_table(x[3], y[3], w[3], h[3], "層次遍歷");
}
if (msg.x >= x[4] && msg.x <= x[4] + w[4] && msg.y >= y[4] && msg.y <= y[4] + h[4]) {
setfillcolor(RED);
fillroundrect(x[4], y[4], x[4] + w[4], y[4] + h[4], 10, 10);
botton_table(x[4], y[4], w[4], h[4], "退出");
}
else {
setfillcolor(YELLOW);
fillroundrect(x[4], y[4], x[4] + w[4], y[4] + h[4], 10, 10);
botton_table(x[4], y[4], w[4], h[4], "退出");
}
}
EndBatchDraw();
}
if (checkt) {
break;
}
}
return;
}
總程式
#include <graphics.h>
#include<iostream>
#include<easyx.h>
#include <cassert>
#include<Windows.h>
#include<bits/stdc++.h>
using namespace std;#define PI 3.1415
typedef struct BiNode {
char data;double x, y;
struct BiNode* lchild, * rchild;
struct BiNode* parent;
}Binode,*BiTree;
string s,s1, s2, s3, s4;
void botton_table(int x, int y, int w, int h, const char* text) {settextcolor(RGB(0, 0, 0));
setbkmode(TRANSPARENT);
char text_[50];
strcmp(text_, text);
int tx = x + (w - textwidth(text)) / 2;
int ty = y + (h - textheight(text)) / 2;
outtextxy(tx, ty, text);
}void showjpg() {
IMAGE img;
loadimage(&img, "./1.jpg", 720, 720);
putimage(0, 0, &img);}
void circhange(int x, int y, int data)
{
settextcolor(BLACK);
setfillcolor(YELLOW);
setbkmode(TRANSPARENT);
fillcircle(x, y, 20);
outtextxy(x - 5, y - 5, data);
Sleep(400);
setfillcolor(BLUE);
setbkmode(TRANSPARENT);
fillcircle(x, y, 20);
outtextxy(x - 5, y - 5, data);
Sleep(400);
setfillcolor(YELLOW);
setbkmode(TRANSPARENT);
fillcircle(x, y, 20);
outtextxy(x - 5, y - 5, data);
Sleep(400);
}void Preordertravers(BiTree T) {
if (T == NULL)return ;
else {
cout << T->data;
circhange(T->x, T->y,T->data);
Preordertravers(T->lchild);
Preordertravers(T->rchild);
}
}void Inordertraverse(BiTree T) {
if (T == NULL)return;
else {
Inordertraverse(T->lchild);
cout << T->data;
circhange(T->x, T->y, T->data);
Inordertraverse(T->rchild);
}
}void Postordertraverse(BiTree T) {
if (T == NULL)return;
else {Postordertraverse(T->lchild);
Postordertraverse(T->rchild);
cout << T->data;
circhange(T->x, T->y, T->data);
}
}void HierarchyOrder(Binode * T)
{
queue<Binode*>q;
q.push(T);
Binode* temp;
while (!q.empty()) {
temp = q.front();
q.pop();
cout << temp->data;
circhange(temp->x, temp->y, temp->data);
if (temp->lchild != NULL) {
q.push(temp->lchild);
}
if (temp->rchild != NULL) {
q.push(temp->rchild);
}
}
}
int ip ;
char ch;
void CreateBiTree(BiTree* T){
ch = s[ip++];
if (ch == '.') {
*T = NULL;
}
else {
*T = (BiTree)malloc(sizeof(Binode));
(*T)->data = ch;
CreateBiTree(&((*T)->lchild));
CreateBiTree(&((*T)->rchild));
}
}
void PrevOrder_stack(Binode* T)
{
stack<Binode*> s;
Binode* Bi = T;
while (Bi || !s.empty())
{
if(Bi)
{
cout << Bi->data;
s.push(Bi);
Bi = Bi->lchild;
}
else {
Bi = s.top();
s.pop();
Bi = Bi->rchild;
}
}
cout << "先序遍歷_堆疊" << endl;
}
void InOrder_stack(Binode* T)
{
stack<Binode*> s;Binode* Bi = T;
while (Bi || !s.empty())
{
if (Bi)
{
s.push(Bi);
Bi = Bi->lchild;
}
else {
Bi = s.top();
cout << Bi->data;
s.pop();
Bi = Bi->rchild;
}
}
cout << "中序遍歷_堆疊" << endl;
}
void PastOrder_stack(Binode* T)
{
Binode* Bi = T;
Binode* vis = NULL;
stack<Binode*> s;
while (Bi||!s.empty())
{
if (Bi)
{
s.push(Bi);
Bi = Bi->lchild;
}
else {
Bi = s.top();
if (Bi->rchild && vis != Bi->rchild)
Bi = Bi->rchild;
else if (( Bi->rchild==NULL) || (vis == Bi->rchild))
{
cout << Bi->data;
vis = Bi;
s.pop();
Bi = NULL;
}
}
}
cout << "后序遍歷堆疊" << endl;
}void PrevOrderDraw(BiTree T, double x, double y, double xt, double yt,double xp,int time,int dep)//xp為角度,
{
if (T)
{
T->x = x;
T->y = y;
setfillcolor(BLUE);
settextcolor(WHITE);
setbkmode(TRANSPARENT);
fillcircle(x, y, 20);
outtextxy(x - 5, y - 5, T->data);
}
if (T->lchild)//左子樹
{
double i ,j;
for (i = x,j=y; i >=x - xt,j<=y+yt;) {
i -= (x - xt) / 500, j =(-yt/xt)*i-(-yt/xt)*x+y;
line(x,y , i, j);
Sleep(time);
}
setfillcolor(BLUE);
setbkmode(TRANSPARENT);
fillcircle(x, y, 20);
settextcolor(WHITE);
T->x = x;
T->y = y;
outtextxy(x - 5, y - 5, T->data);
PrevOrderDraw(T->lchild, x - xt, y + yt, xt / 1.1 * abs(cos(xp)), yt / 1.1 * abs(sin(xp)), xp / 2, time + pow(7, dep), dep + 1);
}
if (T->rchild)//右子樹
{
double i, j;
for (i = x, j = y; i >= x + xt, j <= y + yt;) {
i += (x + xt) / 500, j = (yt / xt) * i - (yt / xt) * x + y;
line(x, y, i, j);
Sleep(time);
}
setfillcolor(BLUE);
setbkmode(TRANSPARENT);
fillcircle(x, y, 20);
T->x = x;
T->y = y;
settextcolor(WHITE);
outtextxy(x - 5, y - 5, T->data);
PrevOrderDraw(T->rchild, x + xt, y + yt, xt / 1.1 * abs(cos(xp)), yt / 1.1 * abs(sin(xp)), xp / 2, time + pow(7, dep), dep + 1);
}
}void PrintBiTree(BiTree T)
{
PrevOrderDraw(T, 360,200, 180* abs(sin(PI / 3)), 180* abs(cos(PI / 3)), PI / 3,3,1);
}void Draw(BiTree T, double x, double y, double xt, double yt, double xp)//xp為角度,
{
if (T)
{
T->x = x;
T->y = y;
setfillcolor(BLUE);
setbkmode(TRANSPARENT);
fillcircle(x, y, 20);
outtextxy(x - 5, y - 5, T->data);
}
if (T->lchild)//左子樹
{
line(x, y, x-xt,y+yt);
setfillcolor(BLUE);
setbkmode(TRANSPARENT);
fillcircle(x, y, 20);
T->x = x;
T->y = y;
outtextxy(x - 5, y - 5, T->data);
Draw(T->lchild, x - xt, y + yt, xt / 1.1 * abs(cos(xp)), yt / 1.1 * abs(sin(xp)), xp / 2);
}if (T->rchild)//右子樹
{
line(x, y, x+xt,y+yt);
setfillcolor(BLUE);
setbkmode(TRANSPARENT);
fillcircle(x, y, 20);
T->x = x;
T->y = y;
outtextxy(x - 5, y - 5, T->data);
Draw(T->rchild, x + xt, y + yt, xt / 1.1 * abs(cos(xp)), yt / 1.1 * abs(sin(xp)), xp / 2);
}
}
//int main()
//{
// ifstream file;
// file.open("test.txt");
// file >> s1 >> s2 >> s3 >> s4;
// s = s1;
//
// BiTree T;
//
// CreateBiTree(&T,0);
//
// PrevOrder_stack(T);
// InOrder_stack(T);
// PastOrder_stack(T);
//
// PrintBiTree(T);
//
//
//
//
// HierarchyOrder(T);
// cout << "層次遍歷堆疊" << endl;
// Draw(T, 360, 200, 180 * abs(sin(PI / 3)), 180 * abs(cos(PI / 3)), PI / 3);
// /*Preordertravers(T);
// cout << "先序遍歷" << endl;
// Inordertraverse(T);
// cout << "中序遍歷" << endl;
// Postordertraverse(T);
// cout << "后序遍歷" << endl;*/
//
//
// Sleep(80000);
//}
void Orderchoice(BiTree T) {showjpg();
BeginBatchDraw();ExMessage msg;
int x[5], y[5], w[5], h[5];
x[0] = 260;
y[0] = 100;
w[0] = 200;
h[0] = 50;
setfillcolor(YELLOW);
fillroundrect(x[0], y[0], x[0] + w[0], y[0] + h[0], 10, 10);
botton_table(x[0], y[0], w[0], h[0], "先序遍歷");
x[1] = 260;
y[1] = 200;
w[1] = 200;
h[1] = 50;
setfillcolor(YELLOW);
fillroundrect(x[1], y[1], x[1] + w[1], y[1] + h[1], 10, 10);
botton_table(x[0], y[0], w[0], h[0], "中序遍歷");
x[2] = 260;
y[2] = 300;
w[2] = 200;
h[2] = 50;
setfillcolor(YELLOW);
fillroundrect(x[2], y[2], x[2] + w[2], y[2] + h[2], 10, 10);
botton_table(x[2], y[2], w[2], h[2], "后序遍歷");
x[3] = 260;
y[3] = 400;
w[3] = 200;
h[3] = 50;
setfillcolor(YELLOW);
fillroundrect(x[3], y[3], x[3] + w[3], y[3] + h[3], 10, 10);
botton_table(x[3], y[3], w[3], h[3], "層次遍歷");
x[4] = 260;
y[4] = 500;
w[4] = 200;
h[4] = 50;
setfillcolor(YELLOW);
fillroundrect(x[4], y[4], x[4] + w[4], y[4] + h[4], 10, 10);
botton_table(x[4], y[4], w[4], h[4], "退出");
bool checkt = 0;
while (1) {
if (peekmessage(&msg, EM_MOUSE)) {
switch (msg.message) {
case WM_LBUTTONDOWN://點擊
if (msg.x >= x[0] && msg.x <= x[0] + w[0] && msg.y >= y[0] && msg.y <= y[0] + h[0])
{
cleardevice();
Draw(T, 360, 200, 180 * abs(sin(PI / 3)), 180 * abs(cos(PI / 3)), PI / 3);
Preordertravers(T);
cleardevice();
showjpg();
}
if (msg.x >= x[1] && msg.x <= x[1] + w[1] && msg.y >= y[1] && msg.y <= y[1] + h[1])
{
cleardevice();
Draw(T, 360, 200, 180 * abs(sin(PI / 3)), 180 * abs(cos(PI / 3)), PI / 3);
Inordertraverse(T);
cleardevice();
showjpg();
}
if (msg.x >= x[2] && msg.x <= x[2] + w[2] && msg.y >= y[2] && msg.y <= y[2] + h[2])
{
cleardevice();
Draw(T, 360, 200, 180 * abs(sin(PI / 3)), 180 * abs(cos(PI / 3)), PI / 3);
Postordertraverse(T);
cleardevice();
showjpg();
}
if (msg.x >= x[3] && msg.x <= x[3] + w[3] && msg.y >= y[3] && msg.y <= y[3] + h[3])
{
cleardevice();
Draw(T, 360, 200, 180 * abs(sin(PI / 3)), 180 * abs(cos(PI / 3)), PI / 3);
HierarchyOrder(T);
cleardevice();
showjpg();
}
if (msg.x >= x[4] && msg.x <= x[4] + w[4] && msg.y >= y[4] && msg.y <= y[4] + h[4])
{
checkt = true;
}
FlushBatchDraw();
case WM_MOUSEFIRST://移動
if (msg.x >= x[0] && msg.x <= x[0] + w[0] && msg.y >= y[0] && msg.y <= y[0] + h[0])
{
setfillcolor(RED);
fillroundrect(x[0], y[0], x[0] + w[0], y[0] + h[0], 10, 10);
botton_table(x[0], y[0], w[0], h[0], "先序遍歷");
}
else {
setfillcolor(YELLOW);
fillroundrect(x[0], y[0], x[0] + w[0], y[0] + h[0], 10, 10);
botton_table(x[0], y[0], w[0], h[0], "先序遍歷");
}
if (msg.x >= x[1] && msg.x <= x[1] + w[1] && msg.y >= y[1] && msg.y <= y[1] + h[1]) {
setfillcolor(RED);
fillroundrect(x[1], y[1], x[1] + w[1], y[1] + h[1], 10, 10);
botton_table(x[1], y[1], w[1], h[1], "中序遍歷");
}
else {
setfillcolor(YELLOW);
fillroundrect(x[1], y[1], x[1] + w[1], y[1] + h[1], 10, 10);
botton_table(x[1], y[1], w[1], h[1], "中序遍歷");
}
if (msg.x >= x[2] && msg.x <= x[2] + w[2] && msg.y >= y[2] && msg.y <= y[2] + h[2]) {
setfillcolor(RED);
fillroundrect(x[2], y[2], x[2] + w[2], y[2] + h[2], 10, 10);
botton_table(x[2], y[2], w[2], h[2], "后序遍歷");
}
else {
setfillcolor(YELLOW);
fillroundrect(x[2], y[2], x[2] + w[2], y[2] + h[2], 10, 10);
botton_table(x[2], y[2], w[2], h[2], "后序遍歷");
}
if (msg.x >= x[3] && msg.x <= x[3] + w[3] && msg.y >= y[3] && msg.y <= y[3] + h[3]) {
setfillcolor(RED);
fillroundrect(x[3], y[3], x[3] + w[3], y[3] + h[3], 10, 10);
botton_table(x[3], y[3], w[3], h[3], "層次遍歷");
}
else {
setfillcolor(YELLOW);
fillroundrect(x[3], y[3], x[3] + w[3], y[3] + h[3], 10, 10);
botton_table(x[3], y[3], w[3], h[3], "層次遍歷");
}
if (msg.x >= x[4] && msg.x <= x[4] + w[4] && msg.y >= y[4] && msg.y <= y[4] + h[4]) {
setfillcolor(RED);
fillroundrect(x[4], y[4], x[4] + w[4], y[4] + h[4], 10, 10);
botton_table(x[4], y[4], w[4], h[4], "退出");
}
else {
setfillcolor(YELLOW);
fillroundrect(x[4], y[4], x[4] + w[4], y[4] + h[4], 10, 10);
botton_table(x[4], y[4], w[4], h[4], "退出");
}}
EndBatchDraw();
}
if (checkt) {
break;
}
}
return;
}int main() {
initgraph(720, 720, EW_SHOWCONSOLE);
ifstream file;
BiTree T;
file.open("test.txt");
file >> s1 >> s2 >> s3 >> s4;
showjpg();
BeginBatchDraw();ExMessage msg;
int x[5], y[5], w[5], h[5];
x[0] = 260;
y[0] = 100;
w[0] = 200;
h[0] = 50;
setfillcolor(YELLOW);
fillroundrect(x[0], y[0], x[0] + w[0], y[0] + h[0], 10, 10);
botton_table(x[0], y[0], w[0], h[0], "Bitree_1");
x[1] = 260;
y[1] = 200;
w[1] = 200;
h[1] = 50;
setfillcolor(YELLOW);
fillroundrect(x[1], y[1], x[1] + w[1], y[1] + h[1], 10, 10);
botton_table(x[0], y[0], w[0], h[0], "Bitree_2");
x[2] = 260;
y[2] = 300;
w[2] = 200;
h[2] = 50;
setfillcolor(YELLOW);
fillroundrect(x[2], y[2], x[2] + w[2], y[2] + h[2], 10, 10);
botton_table(x[2], y[2], w[2], h[2], "Bitree_3");
x[3] = 260;
y[3] = 400;
w[3] = 200;
h[3] = 50;
setfillcolor(YELLOW);
fillroundrect(x[3], y[3], x[3] + w[3], y[3] + h[3], 10, 10);
botton_table(x[3], y[3], w[3], h[3], "Bitree_4");
x[4] = 260;
y[4] = 500;
w[4] = 200;
h[4] = 50;
setfillcolor(YELLOW);
fillroundrect(x[4], y[4], x[4] + w[4], y[4] + h[4], 10, 10);
botton_table(x[4], y[4], w[4], h[4], "退出");
bool checkt = 0;
while (1) {
if (peekmessage(&msg, EM_MOUSE)) {
switch (msg.message) {
case WM_LBUTTONDOWN://點擊
if (msg.x >= x[0] && msg.x <= x[0] + w[0] && msg.y >= y[0] && msg.y <= y[0] + h[0] )
{s = s1;
ip = 0;
cleardevice();
CreateBiTree(&T);
PrintBiTree(T);
Sleep(800);
cleardevice();
showjpg();
Orderchoice(T);
}
if (msg.x >= x[1] && msg.x <= x[1] + w[1] && msg.y >= y[1] && msg.y <= y[1] + h[1] )
{
s = s2;
ip = 0;
cleardevice();
CreateBiTree(&T);
PrintBiTree(T);
Sleep(800);
cleardevice();
showjpg();
Orderchoice(T);
}
if (msg.x >= x[2] && msg.x <= x[2] + w[2] && msg.y >= y[2] && msg.y <= y[2] + h[2])
{
s = s3;
ip = 0;
cleardevice();
CreateBiTree(&T);
PrintBiTree(T);
Sleep(800);
cleardevice();
showjpg();
Orderchoice(T);
}
if (msg.x >= x[3] && msg.x <= x[3] + w[3] && msg.y >= y[3] && msg.y <= y[3] + h[3])
{
s = s4;
ip = 0;
CreateBiTree(&T);
cleardevice();
PrintBiTree(T);
Sleep(800);
cleardevice();
showjpg();
Orderchoice(T);
}
if (msg.x >= x[4] && msg.x <= x[4] + w[4] && msg.y >= y[4] && msg.y <= y[4] + h[4])
{
checkt = true;
}
FlushBatchDraw();
case WM_MOUSEFIRST://移動
if (msg.x >= x[0] && msg.x <= x[0] + w[0] && msg.y >= y[0] && msg.y <= y[0] + h[0])
{
setfillcolor(RED);
fillroundrect(x[0], y[0], x[0] + w[0], y[0] + h[0], 10, 10);
botton_table(x[0], y[0], w[0], h[0], "Bitree_1");
}
else {
setfillcolor(YELLOW);
fillroundrect(x[0], y[0], x[0] + w[0], y[0] + h[0], 10, 10);
botton_table(x[0], y[0], w[0], h[0], "Bitree_1");
}
if (msg.x >= x[1] && msg.x <= x[1] + w[1] && msg.y >= y[1] && msg.y <= y[1] + h[1]) {
setfillcolor(RED);
fillroundrect(x[1], y[1], x[1] + w[1], y[1] + h[1], 10, 10);
botton_table(x[1], y[1], w[1], h[1], "Bitree_2");
}
else {
setfillcolor(YELLOW);
fillroundrect(x[1], y[1], x[1] + w[1], y[1] + h[1], 10, 10);
botton_table(x[1], y[1], w[1], h[1], "Bitree_2");
}
if (msg.x >= x[2] && msg.x <= x[2] + w[2] && msg.y >= y[2] && msg.y <= y[2] + h[2]) {
setfillcolor(RED);
fillroundrect(x[2], y[2], x[2] + w[2], y[2] + h[2], 10, 10);
botton_table(x[2], y[2], w[2], h[2], "Bitree_3");
}
else {
setfillcolor(YELLOW);
fillroundrect(x[2], y[2], x[2] + w[2], y[2] + h[2], 10, 10);
botton_table(x[2], y[2], w[2], h[2], "Bitree_3");
}
if (msg.x >= x[3] && msg.x <= x[3] + w[3] && msg.y >= y[3] && msg.y <= y[3] + h[3]) {
setfillcolor(RED);
fillroundrect(x[3], y[3], x[3] + w[3], y[3] + h[3], 10, 10);
botton_table(x[3], y[3], w[3], h[3], "Bitree_4");
}
else {
setfillcolor(YELLOW);
fillroundrect(x[3], y[3], x[3] + w[3], y[3] + h[3], 10, 10);
botton_table(x[3], y[3], w[3], h[3], "Bitree_4");
}
if (msg.x >= x[4] && msg.x <= x[4] + w[4] && msg.y >= y[4] && msg.y <= y[4] + h[4]) {
setfillcolor(RED);
fillroundrect(x[4], y[4], x[4] + w[4], y[4] + h[4], 10, 10);
botton_table(x[4], y[4], w[4], h[4], "退出");
}
else {
setfillcolor(YELLOW);
fillroundrect(x[4], y[4], x[4] + w[4], y[4] + h[4], 10, 10);
botton_table(x[4], y[4], w[4], h[4], "退出");
}
}
EndBatchDraw();
}
if (checkt) {
break;
}
}
return 0;
}
總結
第一次寫這個,,,沒啥經驗,單純想發發玩,有什么說錯的地方可以指出來,
總體操作不算繁瑣,600行輕松解決作業 OvO
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/374855.html
標籤:其他
上一篇:紅黑樹(C++實作)
