2015年第六届蓝桥杯C/C++ A组国赛 —— 第三题:显示二叉树
生活随笔
收集整理的這篇文章主要介紹了
2015年第六届蓝桥杯C/C++ A组国赛 —— 第三题:显示二叉树
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
標題:顯示二叉樹
排序二叉樹的特征是:
某個節點的左子樹的所有節點值都不大于本節點值。
某個節點的右子樹的所有節點值都不小于本節點值。
為了能形象地觀察二叉樹的建立過程,小明寫了一段程序來顯示出二叉樹的結構來。
#include <stdio.h> #include <string.h> #define N 1000 #define HEIGHT 100 #define WIDTH 1000struct BiTree {int v;struct BiTree* l;struct BiTree* r; };int max(int a, int b) {return a>b? a : b; }struct BiTree* init(struct BiTree* p, int v) {p->l = NULL;p->r = NULL;p->v = v;return p; }void add(struct BiTree* me, struct BiTree* the) {if(the->v < me->v){if(me->l==NULL) me->l = the;else add(me->l, the);}else{if(me->r==NULL) me->r = the;else add(me->r, the);} }//獲得子樹的顯示高度 int getHeight(struct BiTree* me) {int h = 2;int hl = me->l==NULL? 0 : getHeight(me->l);int hr = me->r==NULL? 0 : getHeight(me->r);return h + max(hl,hr); }//獲得子樹的顯示寬度 int getWidth(struct BiTree* me) {char buf[100];sprintf(buf,"%d",me->v); int w = strlen(buf);if(me->l) w += getWidth(me->l);if(me->r) w += getWidth(me->r);return w; }int getRootPos(struct BiTree* me, int x){return me->l==NULL? x : x + getWidth(me->l); }//把緩沖區當二維畫布用 void printInBuf(struct BiTree* me, char buf[][WIDTH], int x, int y) {int p1,p2,p3,i;char sv[100];sprintf(sv, "%d", me->v);p1 = me->l==NULL? x : getRootPos(me->l, x);p2 = getRootPos(me, x);p3 = me->r==NULL? p2 : getRootPos(me->r, p2+strlen(sv));buf[y][p2] = '|';for(i=p1; i<=p3; i++) buf[y+1][i]='-';for(i=0; i<strlen(sv); i++) buf[y+1][p2+i]=sv[i];if(p1<p2) buf[y+1][p1] = '/';if(p3>p2) buf[y+1][p3] = '\\';if(me->l) printInBuf(me->l,buf,x,y+2);if(me->r) ____________________________________; //填空位置 }void showBuf(char x[][WIDTH]) {int i,j;for(i=0; i<HEIGHT; i++){for(j=WIDTH-1; j>=0; j--){if(x[i][j]==' ') x[i][j] = '\0';else break;}if(x[i][0]) printf("%s\n",x[i]);else break;} }void show(struct BiTree* me) {char buf[HEIGHT][WIDTH];int i,j;for(i=0; i<HEIGHT; i++)for(j=0; j<WIDTH; j++) buf[i][j] = ' ';printInBuf(me, buf, 0, 0);showBuf(buf); }int main() { struct BiTree buf[N]; //存儲節點數據 int n = 0; //節點個數 init(&buf[0], 500); n++; //初始化第一個節點 add(&buf[0], init(&buf[n++],200)); //新的節點加入樹中 add(&buf[0], init(&buf[n++],509));add(&buf[0], init(&buf[n++],100));add(&buf[0], init(&buf[n++],250));add(&buf[0], init(&buf[n++],507));add(&buf[0], init(&buf[n++],600));add(&buf[0], init(&buf[n++],650));add(&buf[0], init(&buf[n++],450));add(&buf[0], init(&buf[n++],440));add(&buf[0], init(&buf[n++],220));show(&buf[0]); return 0; }對于上邊的測試數據,應該顯示出:
|/--------------500---\| | /--200---\ /--509\ | | | | 100 /--250---\ 507 600\| | |220 /--450 650|440(如有對齊問題,請參考【圖1.png】)
請分析程序邏輯,填寫劃線部分缺失的代碼。
注意,只填寫缺少的部分,不要填寫已有的代碼或符號,也不要加任何說明文字。
Code
/*^....0^ .1 ^1^.. 011.^ 1.0^ 1 ^ ^0.11 ^ ^..^0. ^ 0^.0 1 .^.1 ^0 .........001^.1 1. .111100....01^00 11^ ^1. .1^1.^ ^0 0^.^ ^0..1.1 1..^1 .0 ^ ^00. ^^0.^^ 0 ^^110.^0 0 ^ ^^^10.01^^ 10 1 1 ^^^1110.101 10 1.1 ^^^1111110010 01 ^^ ^^^1111^1.^ ^^^10 10^ 0^ 1 ^^111^^^0.1^ 1....^11 0 ^^11^^^ 0.. ....1^ ^ ^1. 0^ ^11^^^ ^ 1 111^ ^ 0.10 00 11 ^^^^^ 1 0 1.0^ ^0 ^0 ^^^^ 0 0.0^ 1.0 .^ ^^^^ 1 1 .0^.^ ^^ 0^ ^1 ^^^^ 0. ^.11 ^ 11 1. ^^^ ^ ^ ..^^..^ ^1 ^.^ ^^^ .0 ^.00..^ ^0 01 ^^^ .. 0..^1 .. .1 ^.^ ^^^ 1 ^ ^0001^ 1. 00 0. ^^^ ^.0 ^.1. 0^. ^.^ ^.^ ^^^ ..0.01 .^^. .^ 1001 ^^ ^^^ . 1^. ^ ^. 11 0. 1 ^ ^^ 0.0 ^. 0 ^0 1 ^^^ 0.0.^ 1. 0^ 0 .1 ^^^ ...1 1. 00 . .1 ^^^ ..1 1. ^. 0 .^ ^^ ..0. 1. .^ . 0 ..1 1. 01 . . ^ 0^.^ 00 ^0 1. ^ 1 1.0 00 . ^^^^^^ ..^ 00 01 ..1. 00 10 1 ^^.1 00 ^. ^^^ .1.. 00 .1 1..01 ..1.1 00 1. ..^ 10^ 1^ 00 ^.1 0 1 1.1 00 00 ^ 1 ^. 00 ^.^ 10^ ^^1.1 00 00 10^..^ 1. ^. 1.0 1 ^. 00 00 .^^ ^. ^ 1 00 ^0000^ ^ 011 0 ^. 00.0^ ^00000 1.00.1 11. 1 0 1^^0.01 ^^^ 01.^ ^ 1 1^^ ^.^1 1 0... 1 ^1 1^ ^ .01 ^ 1.. 1.1 ^0.0^ 0 1..01^^100000..0^1 1 ^ 1 ^^1111^ ^^0 ^ ^ 1 1000^.1 ^.^ . 00.. 1.1 0. 01. . 1. .^1. 1 1. ^0^ . ^.1 00 01^.0 001. .^*//* Procedural objectives:Variables required by the program:Procedural thinking:Functions required by the program:Determination algorithm:Determining data structure:*/ /* My dear Max said: "I like you, So the first bunch of sunshine I saw in the morning is you, The first gentle breeze that passed through my ear is you, The first star I see is also you. The world I see is all your shadow."FIGHTING FOR OUR FUTURE!!! */ #include <stdio.h> #include <string.h> #define N 1000 #define HEIGHT 100 #define WIDTH 1000struct BiTree {int v;struct BiTree* l;struct BiTree* r; };int max(int a, int b) {return a>b? a : b; }struct BiTree* init(struct BiTree* p, int v) {p->l = NULL;p->r = NULL;p->v = v;return p; }void add(struct BiTree* me, struct BiTree* the) {if(the->v < me->v){ //如果要插入的值小于節點的值 if(me->l==NULL) me->l = the; //如果要插入的節點的左子樹為空,則插入值 else add(me->l, the); //否則將值添加到節點的左子樹 }else{if(me->r==NULL) me->r = the;else add(me->r, the);} }//獲得子樹的顯示高度 int getHeight(struct BiTree* me) {int h = 2;int hl = me->l==NULL? 0 : getHeight(me->l);int hr = me->r==NULL? 0 : getHeight(me->r);return h + max(hl,hr); }//獲得子樹的顯示寬度 int getWidth(struct BiTree* me) {char buf[100];sprintf(buf,"%d",me->v); int w = strlen(buf);if(me->l) w += getWidth(me->l);if(me->r) w += getWidth(me->r);return w; }int getRootPos(struct BiTree* me, int x){return me->l==NULL? x : x + getWidth(me->l); }//把緩沖區當二維畫布用 void printInBuf(struct BiTree* me, char buf[][WIDTH], int x, int y) {int p1,p2,p3;char sv[100];sprintf(sv, "%d", me->v);p1 = me->l==NULL? x : getRootPos(me->l, x);//p1為左子樹的左子樹的高度p2 = getRootPos(me, x);//p2為左子樹的寬度 p3 = me->r==NULL? p2 : getRootPos(me->r, p2+strlen(sv));//p3為右子樹的 buf[y][p2] = '|';for(int i=p1; i<=p3; i++) buf[y+1][i]='-';for(int i=0; i<strlen(sv); i++) buf[y+1][p2+i]=sv[i];if(p1<p2) buf[y+1][p1] = '/';if(p3>p2) buf[y+1][p3] = '\\';if(me->l) printInBuf(me->l,buf,x,y+2); // if(me->r) ____________________________________; //填空位置if(me->r) printInBuf(me->r,buf,(p2+p3)/2+(me->v<509?0:2),y+2); // if(me->r) printInBuf(me->r,buf,p2+strlen(sv),y+2); //填空位置 }void showBuf(char x[][WIDTH]) {int i,j;for(i=0; i<HEIGHT; i++){for(j=WIDTH-1; j>=0; j--){if(x[i][j]==' ') x[i][j] = '\0';else break;}if(x[i][0]) printf("%s\n",x[i]);else break;} }void show(struct BiTree* me) {char buf[HEIGHT][WIDTH];for(int i=0; i<HEIGHT; i++)for(int j=0; j<WIDTH; j++) buf[i][j] = ' ';printInBuf(me, buf, 0, 0);showBuf(buf); }int main() { struct BiTree buf[N]; //存儲節點數據 int n = 0; //節點個數 init(&buf[0], 500); n++; //初始化第一個節點 add(&buf[0], init(&buf[n++],200)); //新的節點加入樹中 add(&buf[0], init(&buf[n++],509));add(&buf[0], init(&buf[n++],100));add(&buf[0], init(&buf[n++],250));add(&buf[0], init(&buf[n++],507));add(&buf[0], init(&buf[n++],600));add(&buf[0], init(&buf[n++],650));add(&buf[0], init(&buf[n++],450));add(&buf[0], init(&buf[n++],440));add(&buf[0], init(&buf[n++],220));show(&buf[0]); return 0; } 與50位技術專家面對面20年技術見證,附贈技術全景圖總結
以上是生活随笔為你收集整理的2015年第六届蓝桥杯C/C++ A组国赛 —— 第三题:显示二叉树的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 2015年第六届蓝桥杯C/C++ B组国
- 下一篇: CSDN修改博客皮肤模板