麻豆小视频在线观看_中文黄色一级片_久久久成人精品_成片免费观看视频大全_午夜精品久久久久久久99热浪潮_成人一区二区三区四区

首頁 > 學院 > 開發設計 > 正文

二叉樹的遍歷,葉子數目以及深度

2019-11-10 19:47:30
字體:
來源:轉載
供稿:網友

PRoblem Description 已知一個按先序序列輸入的字符序列,如abc,,de,g,,f,,,(其中逗號表示空節點)。請建立二叉樹并按中序和后序方式遍歷二叉樹,最后求出葉子節點個數和二叉樹深度。

Input 輸入一個長度小于50個字符的字符串。 Output 輸出共有7行: 第1行輸出前序遍歷序列; 第2行輸出中序遍歷序列; 第3行輸出后序遍歷序列; 第4行輸出層序遍歷序列; 第5行輸出葉子節點個數; 第6行輸出葉子節點(從上到下,從左到右); 第7行輸出二叉樹深度。

Example Input

abc,,de,g,,f,,,

Example Output

abcdegf cbegdfa cgefdba abcdefg 3 cfg 5

建樹

struct node *creat(struct node *t) { char c; c = str[i ++]; if (c == ',') t = NULL; else { t = (struct node *)malloc(sizeof(struct node)); t -> data = c; t -> l = creat(t -> l); t -> r = creat(t -> r); } return t; }

前序遍歷

void qianxu(struct node *t) { if (t != NULL) { printf("%c", t -> data); qianxu(t -> l); qianxu(t -> r); }}

中序遍歷

void zhonxu(struct node *t) { if (t != NULL) { zhonxu(t -> l); printf("%c",t -> data); zhonxu(t -> r); } }

后序遍歷

void houxu(struct node *t) { if (t != NULL) { houxu(t -> l); houxu(t -> r); printf("%c",t -> data); }}

層序遍歷

void cengxu(struct node *t) { int in = 0, out = 0; struct node *a[1050]; a[in ++] = t; while(in > out) { if (a[out] != NULL) { printf("%c",a[out] -> data); a[in ++] = a[out] -> l; a[in ++] = a[out] -> r; } out ++; } }

葉子個數

void num(struct node *t) { if (t != NULL) { if (t -> l == NULL && t -> r == NULL) count ++; else { num(t -> l); num(t -> r); } } }

葉子節點(從上到下,從左到右) 由層序遍歷修改而得

void cengxunum(struct node *t) { int in = 0; int out = 0; struct node *p[1050]; p[in ++] = t; while(in > out) { if (p[out] != NULL) { if (p[out] -> l == NULL &&p[out] -> r == NULL) { printf("%c",p[out] -> data); } else { p[in ++] = p[out] -> l; p[in ++] = p[out] -> r; } } out ++; } }

二叉樹深度

int depth(struct node *t) { int d1, d2; if (t != NULL) { d1 = depth(t -> l); d2 = depth(t -> r); if (d1 > d2) return d1 + 1; else return d2 + 1; } return 0; }

最終代碼:

#include<stdio.h> #include<string.h> #include<stdlib.h> #include<math.h> struct node { char data; struct node *l, *r; }tree; struct node *creat(struct node *t); void qianxu(struct node *t); void zhonxu(struct node *t); void houxu(struct node *t); void cengxu(struct node *t); void num(struct node *t); void cengxunum(struct node *t); int depth(struct node *t); char str[1050]; int i; int count = 0; int main() { while(scanf("%s",str)!=EOF) { i = 0; struct node *tree = NULL; tree = creat(tree); qianxu(tree); printf("/n"); zhonxu(tree); printf("/n"); houxu(tree); printf("/n"); cengxu(tree); printf("/n"); num(tree); printf("%d/n",count); cengxunum(tree); printf("/n"); int de; de = depth(tree); printf("%d/n",de); } return 0; } struct node *creat(struct node *t) { char c; c = str[i ++]; if (c == ',') t = NULL; else { t = (struct node *)malloc(sizeof(struct node)); t -> data = c; t -> l = creat(t -> l); t -> r = creat(t -> r); } return t; } void zhonxu(struct node *t) { if (t != NULL) { zhonxu(t -> l); printf("%c",t -> data); zhonxu(t -> r); } } void houxu(struct node *t) { if (t != NULL) { houxu(t -> l); houxu(t -> r); printf("%c",t -> data); }}void qianxu(struct node *t) { if (t != NULL) { printf("%c", t -> data); qianxu(t -> l); qianxu(t -> r); }}void cengxu(struct node *t) { int in = 0, out = 0; struct node *a[1050]; a[in ++] = t; while(in > out) { if (a[out] != NULL) { printf("%c",a[out] -> data); a[in ++] = a[out] -> l; a[in ++] = a[out] -> r; } out ++; } } void num(struct node *t) { if (t != NULL) { if (t -> l == NULL && t -> r == NULL) count ++; else { num(t -> l); num(t -> r); } } } void cengxunum(struct node *t) { int in = 0; int out = 0; struct node *p[1050]; p[in ++] = t; while(in > out) { if (p[out] != NULL) { if (p[out] -> l == NULL &&p[out] -> r == NULL) { printf("%c",p[out] -> data); } else { p[in ++] = p[out] -> l; p[in ++] = p[out] -> r; } } out ++; } } int depth(struct node *t) { int d1, d2; if (t != NULL) { d1 = depth(t -> l); d2 = depth(t -> r); if (d1 > d2) return d1 + 1; else return d2 + 1; } return 0; }
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 天天碰天天操 | 国产a级片电影 | 国色天香综合网 | 成人免费福利视频 | 亚洲骚妻 | 丁香天堂网 | 成人国产精品久久 | 蜜桃视频观看麻豆 | 国产精品久久av | av在线一区二区三区 | 亚洲一区二区三区日本久久九 | 免费毛片电影 | 久久国产成人午夜av浪潮 | 欧美综合在线观看视频 | 久久精品亚洲欧美日韩精品中文字幕 | 男人天堂新地址 | 全黄裸片武则天艳史 | 成人三级黄色片 | 一区视频 | 91情侣在线偷精品国产 | 国产成人免费精品 | 中文字幕一区久久 | 国产 一区 | 久久久一区二区三区精品 | 国产在线观看一区二区三区 | 色淫影院 | 日韩2区 | 免费久久精品 | 毛片毛片 | 亚洲国产精品久久久久 | 日韩精品中文字幕在线播放 | 宅男噜噜噜66国产在线观看 | 在线观看中文字幕国产 | 国产精品久久久久久久久久大牛 | 亚洲精品wwww | 国产成年人小视频 | 久在线观看福利视频69 | 99激情 | 视频在线色 | 国产免费传媒av片在线 | 特级黄aaaaaaaaa毛片 |