#include <stdio.h>
#include <string.h>
#include <stdlib.h> struct node
{
int data;
int h;
struct node *lc,*rc; //平衡二叉树 需要一个 h 来记录平衡因子
}; int max(int x ,int y)
{
if(x > y) return x;
else return y;
} int fin(struct node *root) // 返回这个结点的平衡因子,也就是左右子树的差值
{
if(root == NULL) return -1;
else return root -> h;
} struct node *LL(struct node *root) // 如果是左左型,也就是呈现 根 - 左子树 p - 左子树2 , 我们要把 根 变成 p 的右子树
{
struct node *p = root -> lc;
root -> lc = p -> rc;
p -> rc = root;
p -> h = max(fin(p->lc),fin(p->rc)) + 1; // 更新这两个点的平衡因子
root -> h = max(fin(root->lc),fin(root->rc)) + 1;
return p; //别忘记返回 p
} struct node *RR(struct node *root) // 同上相反
{
struct node *p = root -> rc;
root -> rc = p -> lc;
p -> lc = root;
p -> h = max(fin(p->lc),fin(p->rc)) + 1;
root -> h = max(fin(root->lc),fin(root->rc)) + 1;
return p;
} struct node *LR(struct node *root) //LR型, 形如 根 - 左子树 - 右子树
{
root -> lc = RR(root -> lc);
return LL(root);
} struct node *RL(struct node *root)
{
root -> rc = LL(root -> rc);
return RR(root);
} struct node *creat(struct node *root, int x) // 建树的过程
{
if(root == NULL) //如何到底层或者到可以放这个数的地方
{
root = (struct node *)malloc(sizeof(struct node));
root -> data = x;
root -> lc = root -> rc = NULL; // 左右孩子、h 初始化
root -> h = 0;
}
else if(root -> data > x) // 如果小的话,找左子树,
{
root -> lc = creat(root -> lc, x);
if(fin(root->lc) - fin(root->rc) > 1) // 如果找完之后,放进去之后,判断时候不平衡了,如果不平衡,判断是什么样子的类型,再旋转
{
if(root -> lc -> data > x) root = LL(root); // LL型,因为如果 root -> lc -> data > x,那么 x 是放在了这个的左子树
else root = LR(root); //相反,这样子会放在右子树
}
}
else if(root -> data < x)
{
root -> rc = creat(root -> rc, x);
if(fin(root->rc) - fin(root->lc) >1)
{
if(root -> rc -> data < x) root = RR(root);
else root = RL(root);
}
}
root -> h = max(fin(root->lc),fin(root->rc)) + 1; // 没插入一次新值,更新 root 的平衡因子
return root;
}
int main()
{
int n,m;
scanf("%d",&n);
struct node *root = NULL;
for(int i = 0; i < n; i ++)
{
scanf("%d",&m);
root = creat(root,m);
}
printf("%d\n",root->data);
return 0;
}

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

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

    数据结构实验之查找二:平衡二叉树 Time Limit: 400MS Memory Limit: 65536KB Submit Statistic Problem Description 根据给定的输 ...

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

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

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

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

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

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

  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 Descrip ...

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

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

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

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

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

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

随机推荐

  1. [jsp学习笔记]jstl标签的使用

    jstl常用在html嵌套 <body> <% session.setAttribute("q", "yushi"); request.set ...

  2. SQL递归获取树型路径中文名称

    项目中遇到一个树型结构表要根据任意传入节点获取它从根节点一直到自身节点的全部路径的中文名称,并且用'>'与分隔. 我使用在sqlServer中写了一个解析函数方便开发调用. USE [RP_ER ...

  3. Description Resource Path Location Type Unknown Unknown Unknown org.eclipse.core.internal.resources.Marker is not of a displayable type

    是访问限制报错. 方法一: 全局属性Project>preferences>java>Compiler>Errors/Warnings>把右侧的[Deprecated a ...

  4. Ubuntu linux安装完成后隐藏linux磁盘挂载点

    方案1 打开注册表 , 找到这个位置: 计算机\HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Policies\Explore ...

  5. Http 请求头包含哪些信息?

    协议头 说明 示例 状态 Accept 可接受的响应内容类型(Content-Types). Accept: text/plain 固定 Accept-Charset 可接受的字符集 Accept-C ...

  6. python中的debug

    python中有很多的debug方法,大部分新人忽略了Python debugger(pdb)的重要性. 1.命令行运行 在终端中输入命令行   python -m pdb helloword.py ...

  7. javascript_17-基本类型和引用类型

    基本类型 直接存储值 Number . String .Boolean undefined.null 引用类型 存储引用 -Object.Array.Date.函数 包装基本类型--引用类型 func ...

  8. There is no getter for property named 'PRODUCT_ID' in 'class java.lang.String'

    背景:心血来潮之际,准备搭建以下多月未曾碰触过的Mybatis框架,体验一番原生之美.殊不知能力有限,错误百出.特抒此文以纪念此坑.问题:想在sql管理中来统一处理一些简单的判断,但是添加判断之后参数 ...

  9. Python_文件相关操作

    1.open(filePath,type)方法:打开文件 filePath:文件路径 type:操作文件的方式(r:读取,w:覆盖写入,a:追加写入) 2.strip()方法:去除读取到的每行内容后的 ...

  10. MVC模式:action、dao、model、service、util

    这就是一个典型的MVC: action:主要是Struts2,用来做跳转,比如jsp页面提交的表单就是进入到action里面,然后action再调用service里面的逻辑,最后返回到jsp响应请求. ...