数据结构实验之查找二:平衡二叉树

Time Limit: 400MS Memory Limit: 65536KB

Problem Description

根据给定的输入序列建立一棵平衡二叉树,求出建立的平衡二叉树的树根。

Input

输入一组测试数据。数据的第1行给出一个正整数N(n <= 20),N表示输入序列的元素个数;第2行给出N个正整数,按数据给定顺序建立平衡二叉树。

Output

输出平衡二叉树的树根。

Example Input

5
88 70 61 96 120

Example Output

70

DQE:

结点平衡因子改为该结点树深度,旋转操作记得更新结点深度。
 
 #include <iostream>
#include <cstdio>
using namespace std;
/*
全测数据
in
9
16 3 7 11 9 26 18 14 15
out
11 7 3 9 18 15 14 16 26
3 7 9 11 14 15 16 18 26
*/
struct BT
{
int d;
int x;
BT *lt,*rt;
}; int dp(BT *r)
{
return r?r->d:;
}
void rfd(BT *r)
{
int ld=dp(r->lt)+,rd=dp(r->rt)+;
r->d=ld>rd?ld:rd;
} void LL(BT *&r)
{
// printf("LL#%d#%d*%d ",r->x,dp(r->lt),dp(r->rt));
BT *a=r,*b=a->lt,*br=b->rt;
r=b; //利用引用修改上级指向
b->rt=a;
a->lt=br;
//深度重定
rfd(a);
}
void RR(BT *&r)
{
// printf("RR#%d#%d*%d ",r->x,dp(r->rt),dp(r->lt));
BT *a=r,*b=a->rt,*bl=b->lt;
r=b;
b->lt=a;
a->rt=bl; rfd(a);
}
void LR(BT *&r)
{
RR(r->lt);
rfd(r->lt);
LL(r);
}
void RL(BT *&r)
{
LL(r->rt);
rfd(r->rt);
RR(r);
} void insert(BT *&root,int e)
{
if(!root)
{
BT *r=new BT;
r->x=e;
r->d=;
r->lt=r->rt=NULL;
root=r;
}
else
{
if(e<root->x)
{
insert(root->lt,e);
if(dp(root->lt)-dp(root->rt)>)
{
if(e<root->lt->x)
LL(root);
else
LR(root);
}
}
else if(e>root->x)
{
insert(root->rt,e);
if(dp(root->rt)-dp(root->lt)>)
{
if(e>root->rt->x)
RR(root);
else
RL(root);
}
}
//更新根深度
rfd(root);
}
} void xout(BT *r)
{if(r){printf("%d ",r->x);xout(r->lt);xout(r->rt);}}
void mout(BT *r)
{if(r){mout(r->lt);printf("%d ",r->x);mout(r->rt);}}
int main()
{
BT *root=NULL;
int n,i;
scanf("%d",&n);
while(n--)
{
scanf("%d",&i);
insert(root,i);
}
/*
putchar('\n');
xout(root);
putchar('\n');
mout(root);
*/
printf("%d\n",root->x);
return ;
} /***************************************************
User name: ***
Result: Accepted
Take time: 0ms
Take Memory: 156KB
Submit time: 2016-11-29 19:09:25
****************************************************/

SDUT 3374 数据结构实验之查找二:平衡二叉树的更多相关文章

  1. SDUTOJ 3374 数据结构实验之查找二:平衡二叉树

    题目链接:http://acm.sdut.edu.cn/onlinejudge2/index.php/Home/Index/problemdetail/pid/3374.html 题目大意 略. 分析 ...

  2. SDUT 3376 数据结构实验之查找四:二分查找

    数据结构实验之查找四:二分查找 Time Limit: 20MS Memory Limit: 65536KB Submit Statistic Problem Description 在一个给定的无重 ...

  3. SDUT OJ 数据结构实验之二叉树二:遍历二叉树

    数据结构实验之二叉树二:遍历二叉树 Time Limit: 1000 ms Memory Limit: 65536 KiB Submit Statistic Discuss Problem Descr ...

  4. SDUT OJ 数据结构实验之串二:字符串匹配

    数据结构实验之串二:字符串匹配 Time Limit: 1000 ms Memory Limit: 65536 KiB Submit Statistic Discuss Problem Descrip ...

  5. SDUT OJ 数据结构实验之排序二:交换排序

    数据结构实验之排序二:交换排序 Time Limit: 1000 ms Memory Limit: 65536 KiB Submit Statistic Discuss Problem Descrip ...

  6. SDUT OJ 数据结构实验之链表二:逆序建立链表

    数据结构实验之链表二:逆序建立链表 Time Limit: 1000 ms Memory Limit: 65536 KiB Submit Statistic Discuss Problem Descr ...

  7. SDUT 3399 数据结构实验之排序二:交换排序

    数据结构实验之排序二:交换排序 Time Limit: 1000MS Memory Limit: 65536KB Submit Statistic Problem Description 冒泡排序和快 ...

  8. SDUT 3379 数据结构实验之查找七:线性之哈希表

    数据结构实验之查找七:线性之哈希表 Time Limit: 1000MS Memory Limit: 65536KB Submit Statistic Problem Description 根据给定 ...

  9. SDUT 3377 数据结构实验之查找五:平方之哈希表

    数据结构实验之查找五:平方之哈希表 Time Limit: 400MS Memory Limit: 65536KB Submit Statistic Problem Description 给定的一组 ...

随机推荐

  1. pthread_cond_wait() 前使用 while 讲解

    pthread_cond_wait() 前使用 while 讲解 -- : LINUX环境下多线程编程肯定会遇到需要条件变量的情况,此时必然要使用pthread_cond_wait()函数.但这个函数 ...

  2. 463. Island Perimeter Add to List

    You are given a map in form of a two-dimensional integer grid where 1 represents land and 0 represen ...

  3. Long Jumps(二分查找lower_bound()函数的运用)

    Valery is a PE teacher at a school in Berland. Soon the students are going to take a test in long ju ...

  4. LeetCode Beautiful Arrangement

    原题链接在这里:https://leetcode.com/problems/beautiful-arrangement/description/ 题目: Suppose you have N inte ...

  5. Spring Boot基本配置

    本文参考javaEE开发的颠覆者SpringBoot实战第一版 基本配置 入口类和@SpringBootApplication Spring Boot通常有一个名为*Application的入口类,且 ...

  6. 为什么 Eclipse 里的 Classpath Variables M2_REPO 无法修改(non modifiable)

    本文转载自:http://uule.iteye.com/blog/2034097 解决方法: 在C:\Documents and Settings\Administrator\.m2中放入settin ...

  7. Spring MVC配置详解(2)---bai

    web.xml <?xml version="1.0" encoding="UTF-8"?> <web-app version="2 ...

  8. 原生的ado.net(访问sql server数据库)

    本文介绍原生的ado.net(访问sql server数据库) 写在前面 数据库连接字符串 过时的写法 string str = "server=localhost;database=my_ ...

  9. 11-04 SQLserver基础--连接查询、联合查询、索引

    一.子查询补充: Exists的用法: select*from haha where exists(select*from bumen where bumen.code=haha.bumen,and  ...

  10. 10-28SQLserver基础--数据库管理器(基础操作)

    C#基础--数据库(用来存储大量的数据) 操作数据库文件唯一途径 SQL server,结构化查询语言简称SQL. Analysis services:分析挖掘数据 Reporting service ...