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

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

數(shù)據(jù)結(jié)構(gòu)之 平衡二叉樹的建立

2019-11-09 20:53:23
字體:
供稿:網(wǎng)友

數(shù)據(jù)結(jié)構(gòu)實(shí)驗(yàn)之查找二:平衡二叉樹 Time Limit: 400MS Memory Limit: 65536KB PRoblem Description

根據(jù)給定的輸入序列建立一棵平衡二叉樹,求出建立的平衡二叉樹的樹根。 Input

輸入一組測(cè)試數(shù)據(jù)。數(shù)據(jù)的第1行給出一個(gè)正整數(shù)N(n <= 20),N表示輸入序列的元素個(gè)數(shù);第2行給出N個(gè)正整數(shù),按數(shù)據(jù)給定順序建立平衡二叉樹。 Output

輸出平衡二叉樹的樹根。 Example Input

5 88 70 61 96 120

Example Output

70

#include<stdio.h>#include<string.h>#include<stdlib.h>typedef struct node{ int data, d; struct node *lt, *rt;}ST;int max(int a, int b){ if(a < b) return b; else return a;}int deep(ST *root){ if(!root) return -1; else return root->d;}ST *LL(ST *root){ ST *p; p = root->lt; root->lt = p->rt; p->rt = root; root->d = max(deep(root->lt), deep(root->rt)) + 1; return p;}ST *RR(ST *root){ ST *p; p = root->rt; root->rt = p->lt; p->lt = root; root->d = max(deep(root->lt), deep(root->rt)) + 1; return p;}ST *LR(ST *root){ root->lt = RR(root->lt); root = LL(root); return root;}ST *RL(ST *root){ root->rt = LL(root->rt); root = RR(root); return root;}ST *insert(int x, ST *root){ if(!root) { root = (ST *)malloc(sizeof(ST)); root->data = x; root->d = 0; root->lt = NULL; root->rt = NULL; } else { if(x < root->data) { root->lt = insert(x, root->lt); if(deep(root->lt) - deep(root->rt) > 1) { if(root->lt->data > x) root = LL(root); else root = LR(root); } } else if(x > root->data) { root->rt = insert(x, root->rt); if(deep(root->rt) - deep(root->lt) > 1) { if(root->rt->data < x) root = RR(root); else root = RL(root); } } } root->d = max(deep(root->rt),deep(root->lt)) + 1; return root;}int main(){ ST *root; int n, x; while(~scanf("%d", &n)) { root = NULL; while(n--) { scanf("%d", &x); root = insert(x, root); } printf("%d/n", root->data); } return 0;}
上一篇:this指針的用法解釋

下一篇:a[255]的值

發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 融水| 渝中区| 巢湖市| 通道| 五华县| 临湘市| 武平县| 越西县| 盘锦市| 彝良县| 偏关县| 延寿县| 伊金霍洛旗| 达拉特旗| 兖州市| 淮南市| 平山县| 马公市| 义马市| 雷州市| 花莲县| 揭西县| 安国市| 电白县| 阳东县| 河源市| 咸宁市| 六盘水市| 密云县| 平遥县| 清苑县| 祥云县| 余干县| 泉州市| 扎鲁特旗| 岚皋县| 宝应县| 家居| 建始县| 青川县| 凤翔县|