#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. OI数学汇总

    最前面:\(\LaTeX\)可能需要加载一会,请耐心等待o~ 前言 数学在\(\text{OI}\)中十分重要.其中大多都是数论. 什么是数论? \[ 研究整数的理论 --zzq \] 本文包含所有侧 ...

  2. shell-基础2-字符串文本处理${}

    一.为什么使用${}引用变量 1.$a和${a}的效果与区别 因为个别特殊字符会影响正常引用,所以需要使用${}引用变量,加花括号是为了帮助解释器识别变量的边界 $a和${a}效果一样,当变量后面连接 ...

  3. Maven 的依赖范围

    Maven 在编译项目主代码的时候需要使用一套 classpath,在编译和执行测试的时候会使用另外一套 classpath.最后,实际运行 Maven 项目的时候,又会使用一套 classpath. ...

  4. java之hibernate之crud

    这篇文章主要讲解: 1>.对Hibernate使用的一些简单封装: · 2>.在单元测试中,使用Hibernate的封装的工具进行增删改查的测试 1.目录结构展示 2.代码展示 2.0 配 ...

  5. Arduino 计算机视觉系统概述

    计算机视觉系统概述 计算机视觉系统是最近比较热门的研究领域,今天开始给大家介绍下计算机视觉相关的知识. 视觉是人的所有感官中最敏感的一种,人的视觉可以感知环境,而机器的视觉却很难感知环境 为了解决计算 ...

  6. Window 服务器安装MongoDB 设置外网可访问

    1.下载MongoDB www.mongodb.com/download-center#community 2.下一步下一步安装. 安装完成后配置环境变量 我的的默认安装,环境变量地址  C:\Pro ...

  7. 【BZOJ 2351】Matrix(Hash)

    题目链接 二维\(Hash\)类似二维前缀和,每一行看成一个\(h\)进制数,每一个以(1,1)为左上角的矩阵看成一个由每一行的\(Hash\)值组成的\(l\)进制数. 然后自己推推柿子就行. #i ...

  8. tensorflow 单机多GPU训练时间比单卡更慢/没有很大时间上提升

    使用tensorflow model库里的cifar10 多gpu训练时,最后测试发现时间并没有减少,反而更慢 参考以下两个链接 https://github.com/keras-team/keras ...

  9. ActiveMQ Queue vs Topic vs VirtualTopic

    之前写过一篇文章讨论VirtualTopic,但觉得不够透彻,这里再根据实验结果进行一次横向对比破除模糊和选择困难症. 文章中核心对比要素是:消息副本和负载均衡 Queue的特点和优势 ActiveM ...

  10. SSM框架之MyBatis入门介绍

    一.什么是MyBatis? MyBatis源自Apache的iBatis开源项目, 从iBatis3.x开始正式更名为MyBatis.它是一个优秀的持久层框架. 二.为什么使用MyBatis? 为了和 ...