#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. Linux中shell字符串分隔、字符串替换、字符串拼接

    1.从properties文件中读取变量 SERVER_NAME=`sed '/project.config/!d;s/.*=//' conf/dubbo.properties | tr -d '\r ...

  2. nginx配置http静态站点服务器

    1.  系统环境Windows 10 2.  设置静态站点目录,注意不要出现中文(这里踩了很多坑,可以查看错误日志error.log, “No mapping for the Unicode char ...

  3. Logback+ELK+SpringMVC搭建日志收集服务器

    (转) 1.ELK是什么? ELK是由Elasticsearch.Logstash.Kibana这3个软件的缩写. Elasticsearch是一个分布式搜索分析引擎,稳定.可水平扩展.易于管理是它的 ...

  4. 敏感词检测、屏蔽设计(iOS & Android)

    敏感词检测 服务器端最常使用的算法是DFA算法.如果服务器端使用java实现常规的DFA算法,假若... 源码:https://github.com/qiyer/DFA_Cplusplus

  5. UI5-技术篇-JS函数eval动态执行

    <html lang="en"> <head> <meta charset="utf-8" /> <title> ...

  6. mongodump

    mongodump工具是MongoDB提供的用来导出数据的工具,具体的用法参考官方文档:https://docs.mongodb.com/manual/reference/program/mongod ...

  7. iOS应用状态保存和恢复

    当应用被后台Kill掉的时候希望从后台返回的时候显示进入后台之前的内容 在Appdelegate中设置 - (BOOL)application:(UIApplication *)application ...

  8. mysql 模糊查询like小结

    以不完整的条件进行查询 因为条件是模糊的 所以叫模糊查询,可以对有相同信息的数据快速归类 . like  运算符:可以很好的通过%和-两种通配符对数据进行筛选查询 %(所有)放在条件前中后.可查询包含 ...

  9. c# DirectoryInfo类

  10. 【DRF框架】REST风格

    REST风格 表述性状态转移——web交互方案 目的 解决前后端交互的问题,开发效率高,简介,性能好 定义 资源:网上的所有信息或者很抽象的概念,在web中只要又被引用的必要都是资源 URI:统一资源 ...