-
Notifications
You must be signed in to change notification settings - Fork 0
/
TESTBT.C
328 lines (278 loc) · 4.71 KB
/
TESTBT.C
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
#include "bintree.h"
#include<GRAPHICS.H>
#include <stdio.h>
#include <math.h>
typedef struct
{
BinTree bt;
int x;
int y;
}BTreeXY;
void showTraverse(BinTree bt,char key);
void DrawBT(BinTree bt);
void FileOp(char a[]);
void ShowTree(BinTree bt);
void FilledCircle(int x,int y,DataType cc);
void SignFilledCircle(int x,int y,DataType cc);
void CLine(int x1, int y1, int x2, int y2,int color);
void BTreeToArr(BinTree bt,BTreeXY BTXY[]);
void GetBTArrXY(BTreeXY BTXY[]);
void showLeaf(BinTree bt);
BTreeXY BTXY[31];
int MAX_X,MAX_Y;/*屏幕宽度*/
int STATIC_X,STATIC_Y;/*线索化xy*/
int TRAVERSE_SIGN;/*线索化起始标记*/
typedef struct
{
int x;
int y;
}Rxy;
int main()
{
BinTree bt,p;
char a[100];
int driver=DETECT,mode;
MAX_X=getmaxx(),MAX_Y=getmaxy();
initgraph(&driver,&mode,"c:\\bgi");
settextstyle(2,0,6);
FileOp(a);
PreCreateBTree(&bt,a);
ShowTree(bt);
getch();
/*
showTraverse(bt,PRE);
getch();
cleardevice();
ShowTree(bt);
showTraverse(bt,IN);
getch();
cleardevice();
ShowTree(bt);
showTraverse(bt,POST);
getch();
*/
InsertBiTree(bt,'n','w','r');
cleardevice();
ShowTree(bt);
DeleteBTree(bt,'n');
cleardevice();
ShowTree(bt);
showTraverse(bt,PRE);
getch();
cleardevice();
ShowTree(bt);
showLeaf(bt);
getch();
closegraph();
return 1;
}
/*
函数名:FileOp
功能:从tree。txt中获取字符串
参数:字符串地址
*/
void FileOp(char a[])
{
int i=0;
FILE *fp;
fp=fopen("tree.txt","r");
if(fp==NULL){puts("file error");return ;}
while((a[i++]=fgetc(fp))!=EOF);
a[--i]='\0';
fclose(fp);
}
void showLeaf(BinTree bt)
{
int i,j;
BinTree p[16];
FindLeaf(bt,p);
for(i=0;p[i];i++)
{
j=0;
while(p[i]!=BTXY[j].bt&&j<31)j++;
SignFilledCircle(BTXY[j].x,BTXY[j].y,BTXY[j].bt->data);
delay(200);
}
}
void showTraverse(BinTree bt,char key)
{
BTreeToArr(bt,BTXY);
GetBTArrXY(BTXY);
TRAVERSE_SIGN=0;
if(key==PRE)
{
PreOrderTraverse(bt,DrawBT);
}else if(key==IN)
{
InOrderTraverse(bt,DrawBT);
}else if(key==POST)
{
PostOrderTraverse(bt,DrawBT);
}
}
void DrawBT(BinTree bt)
{
int i=0;
while(bt!=BTXY[i].bt&&i<31)i++;
if(TRAVERSE_SIGN==0)
{
STATIC_X=BTXY[i].x;
STATIC_Y=BTXY[i].y;
TRAVERSE_SIGN++;
}
SignFilledCircle(BTXY[i].x,BTXY[i].y,BTXY[i].bt->data);
CLine(STATIC_X,STATIC_Y,BTXY[i].x,BTXY[i].y,RED);
delay(200);
STATIC_X=BTXY[i].x;
STATIC_Y=BTXY[i].y;
}
/*
函数名:
功能:标记圆
参数:
*/
void SignFilledCircle(int x,int y,DataType cc)
{
char ch[2]={0};
ch[0]=cc;
setfillstyle(1,RED);
setfillstyle(1,RED);
setcolor(RED);
circle(x,y,15);
floodfill(x, y, RED);
setcolor(YELLOW);
outtextxy(x-3,y-9,ch);
}
/*
函数名:
功能:画圆
参数:
*/
void FilledCircle(int x,int y,DataType cc)
{
char ch[2]={0};
ch[0]=cc;
setfillstyle(1,GREEN);
setcolor(GREEN);
circle(x,y,12);
floodfill(x, y, GREEN);
setcolor(YELLOW);
outtextxy(x-3,y-9,ch);
}
/*
函数名:
功能:画线
参数:
*/
void CLine(int x1, int y1, int x2, int y2,int color)
{
setcolor(color);
line(x1,y1,x2,y2);
}
/*
函数名:
功能:
参数:
*/
void ShowTree(BinTree bt)
{
int i,j,old_rx,old_ry,sum=0;
MAX_X=getmaxx(),MAX_Y=getmaxy();
BTreeToArr(bt,BTXY);
GetBTArrXY(BTXY);
/*两个循环是建模时是二维数组,但实践中二维数组错误,于是用了累加 等算法来查找*/
for(i=0;i<5;i++)
{
for(j=0;j<pow(2,i);j++)
{
if(i==0)
{
FilledCircle(BTXY[sum].x,BTXY[sum].y,BTXY[sum].bt->data);
}else if(BTXY[sum].bt->data)
{
old_ry=30+(i-1)*MAX_Y/7;
old_rx=pow(-1,j)*MAX_X/pow(2,i+1)+BTXY[sum].x;
CLine(old_rx,old_ry+10,BTXY[sum].x,BTXY[sum].y,WHITE);
FilledCircle(BTXY[sum].x,BTXY[sum].y,BTXY[sum].bt->data);
}
sum++;
}
}
}
/*
函数名:
功能:给结构体确定xy值
参数:
*/
void GetBTArrXY(BTreeXY BTXY[])
{
int i,j;
for(i=0;i<5;i++)
{
for(j=0;j<pow(2,i);j++)
{
if(i==0&&BTXY[0].bt!=0)
{
BTXY[0].x=(2*j+1)*MAX_X/pow(2,i+1);
BTXY[0].y=30+i*MAX_Y/7;
}else if(BTXY[leijia(i-1)+j].bt!=0)
{
BTXY[leijia(i-1)+j].x=(2*j+1)*MAX_X/pow(2,i+1);
BTXY[leijia(i-1)+j].y=30+i*MAX_Y/7;
}
}
}
}
/*
函数名:
功能:
参数:
*/
void BTreeToArr(BinTree bt,BTreeXY BTXY[])
{
int i,j,k;
BinTree newp;
BTXY[0].bt=bt;
for(i=0;i<5;i++)
{
if(i==0)
{
BTXY[0].bt=bt;
}else if(i==1)
{
BTXY[1].bt=BTXY[0].bt->lchild;
BTXY[2].bt=BTXY[0].bt->rchild;
}else
{
k=0;
j=0;
while(j<pow(2,i)&&k<pow(2,i-1))
{
if(BTXY[leijia(i-2)+k].bt==0)
{
BTXY[leijia(i-1)+j].bt=0;
j++;
BTXY[leijia(i-1)+j].bt=0;
j++;
k++;
}else
{
newp=BTXY[leijia(i-2)+k].bt;
BTXY[leijia(i-1)+j].bt=newp->lchild;
j++;
BTXY[leijia(i-1)+j].bt=newp->rchild;
j++;
k++;
}
}
}
}
}
int leijia(int i)
{
if(i==0)
{
return 1;
}
return pow(2,i)+leijia(i-1);
}