国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 學(xué)院 > 開發(fā)設(shè)計(jì) > 正文

數(shù)據(jù)結(jié)構(gòu)之 樹的儲(chǔ)存和遍歷總結(jié)

2019-11-09 21:08:15
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

知道前序(包括空結(jié)點(diǎn) 下面代碼用’,’代替)建立一個(gè)二叉樹,前序 中序 后序 層序輸出 如何求葉子結(jié)點(diǎn)數(shù), 如何求二叉樹深度。

#include<stdio.h>#include<stdlib.h>#include<string.h>typedef struct node{ char data; struct node *lt, *rt;}ST;char str[52];int flag, num;ST *creat(){ ST *root; if(str[++flag] == ',') { root = NULL; } else { root = (ST *)malloc(sizeof(ST)); root->data = str[flag]; root->lt = creat(); root->rt = creat(); } return root;}void midsort(ST *root){ if(root) { midsort(root->lt); 知道前序和中序建立二叉樹過(guò)程

#include<stdio.h>#include<string.h>#include<stdlib.h>typedef struct node{ char data; struct node *lt, *rt;}ST;ST *creat(int n, char a[], char b[]){ ST *root; int i; if(n == 0) return NULL; root = (ST *)malloc(sizeof(ST)); root->data = a[0]; for(i = 0; b[i]; i++) if(b[i] == a[0]) break; root->lt = creat(i, a + 1, b); root->rt = creat(n - i - 1, a + i + 1, b + i + 1); return root;}void lastsort(ST *root){ if(root) { lastsort(root->lt); lastsort(root->rt); printf("%c", root->data); }}int main(){ ST *root; int len; char a[100], b[100]; while(~scanf("%s %s", a, b)) { len = strlen(a); root = creat(len, a, b); lastsort(root); printf("/n"); } return 0;}

知道中序和后序建二叉樹過(guò)程

#include<stdio.h>#include<string.h>#include<stdlib.h>typedef struct node{ char data; struct node *lt, *rt;}ST;ST *creat(int n, char b[], char a[]){ ST *root; int i; if(n == 0) return NULL; root = (ST *)malloc(sizeof(ST)); root->data = b[0]; for(i = 0; a[i]; i++) if(a[i] == b[0]) break; root->rt = creat(n - i - 1, b + 1, a + i + 1); root->lt = creat(i, b + n - i, a); return root;}void headsort(ST *root){ if(root){ printf("%c", root->data); headsort(root->lt); headsort(root->rt); }}int main(){ ST *root; int t, len, i, j; char a[55], b[55], t1; while(~scanf("%d", &t)) { while(t--) { memset(a, 0, sizeof(a)); memset(b, 0, sizeof(b)); scanf("%s %s", a, b); len = strlen(b); for(i = 0, j = len - 1; i <= j; i++, j--) { t1 = b[i]; b[i] = b[j]; b[j] = t1; } root = creat(len, b, a); headsort(root); printf("/n"); } } return 0;}
發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 桑日县| 乃东县| 漳州市| 来凤县| 区。| 类乌齐县| 吐鲁番市| 神木县| 华安县| 保康县| 博客| 广西| 舟曲县| 桃园县| 呼玛县| 湘乡市| 南平市| 伊金霍洛旗| 重庆市| 志丹县| 桓台县| 上高县| 兴业县| 深圳市| 阿瓦提县| 德惠市| 江陵县| 吉木萨尔县| 新河县| 蓬莱市| 万年县| 沾化县| 东乡县| 调兵山市| 安福县| 西乌| 天镇县| 新津县| 银川市| 万全县| 嘉兴市|