Hey_后森 Hey_后森
关注数: 12 粉丝数: 29 发帖数: 724 关注贴吧数: 38
求大神改正画图~ #include <stdio.h> #include <stdlib.h> #include <graphics.h> #include <windows.h> #include <conio.h> #include <malloc.h> #include <time.h> typedef char datatype; typedef struct node { datatype data; struct node *lchild,*rchild; }bitree; //按前序建立二叉树 bitree *CREAT() { bitree *t; char ch; ch=getchar(); if(ch==' ')t=NULL; else { t=((bitree*)malloc(sizeof(bitree))); t->data=ch; t->lchild=CREAT(); t->rchild=CREAT(); } return t; } //按前序输出二叉树 void OUTT_B(bitree *t) { if(t) {printf("%c",t->data); OUTT_B(t->lchild); OUTT_B(t->rchild); } } //按中序输出二叉树 void OUTT_M(bitree *t) { if(t) { OUTT_M(t->lchild); printf("%c",t->data); OUTT_M(t->rchild); } } //按后序输出二叉树 void OUTT_A(bitree *t) { if(t) { OUTT_A(t->lchild); OUTT_A(t->rchild); printf("%c",t->data); } } void gotoxy(int x,int y) //光标定位 { HANDLE hCon=GetStdHandle(STD_OUTPUT_HANDLE); COORD c; c.X=x; c.Y=y; SetConsoleCursorPosition(hCon, c); // 设置光标位置 } void DRAW(bitree *t,int x,int y,int m) { int n; if(t) { gotoxy(x,y); putchar(t->data); Sleep(1000); if(t->lchild) {gotoxy(x-2,y+1); n=m; while((n--)!=0){printf("/");gotoxy(x-2,y+1);} DRAW(t->lchild,x-2-1,y+2,m-1);} if(t->rchild) {gotoxy(x+2,y+1); n=m; while((n--)!=0){printf("\\");gotoxy(x-2,y+1);} DRAW(t->rchild,x+2+1,y+2,m-1);} } } HEIGHT(bitree *t)//算树深度 { int m,n; if(t==NULL) return 0; else{ m=HEIGHT(t->lchild); n=HEIGHT(t->rchild); if (m>n) return m+1; else return n+1; } } LEAF(bitree *t)//算叶节点 { if(!t)return 0; else if(!t->lchild&&!t->rchild)return 1; else return LEAF(t->lchild)+LEAF(t->rchild); } int main() { int a,b,y,m; bitree *p; p=((bitree*)malloc(sizeof(bitree))); printf("please input a string:"); p=CREAT(); printf("************************\n"); printf("*1.前序输出二叉树\n"); printf("*2.中序输出二叉树\n"); printf("*3.后序输出二叉树\n"); printf("*4.求二叉树高度\n"); printf("*5.求二叉树广度\n"); printf("*6.画图\n"); printf("*7.退出\n"); printf("************************\n"); do { printf("please make a choice(1-7):"); scanf("%d",&a); switch(a) { case 1: printf("THE OUT_B string is:\n"); OUTT_B(p); printf("\n"); break; case 2: printf("THE OUT_M string is:\n"); OUTT_M(p); printf("\n"); break; case 3: printf("THE OUT_A string is:\n"); OUTT_A(p); printf("\n"); break; case 4: printf("二叉树树高:"); m=HEIGHT(p); printf("%d\n",m); break; case 5: printf("二叉树广度:"); b=LEAF(p); printf("%d\n",b); break; case 6://画图输出不正确 m=HEIGHT(p); DRAW(p,50,9,m); getch(); break; case 7:break; default:printf("输入错误,请重新输入!\n"); } getch(); }while(a!=7); printf("thank you~\n"); return 0; }
1 下一页