#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. STL之 stack的基础应用

    头文件 #include<stack> stack<int>  s; stack<char> s;//定义一个名字为s 的存int char的stack 基本指令 ...

  2. Springboot 之 静态资源路径配置

    1.静态资源路径是指系统可以直接访问的路径,且路径下的所有文件均可被用户通过浏览器直接读取. 2.在Springboot中默认的静态资源路径有:classpath:/META-INF/resource ...

  3. 14-2 SQL语言简介

    1.结构化查询语言(Structured Query Language,SQL),常被读作sequel,最初是由Microsoft.Sybase和Ashton-Tate这3家公司共同开发的. 2.Wi ...

  4. C#中关于常用日期的计算

    void Main() { DateTime dt = DateTime.Now; //当前时间 DateTime startWeek = dt.AddDays( - Convert.ToInt32( ...

  5. 翻译 API

    Request http://fy.iciba.com/ajax.php?a=fy&f=auto&t=auto&w=love Pre 英译汉 Request http://fy ...

  6. Nginx的proxy buffer参数总结

    1. proxy_buffering 语法:proxy_buffering on|off 默认值:proxy_buffering on 上下文:http,server,location 作用:该指令开 ...

  7. UTF-8 中文编码范围

    主流的匹配字符有两种 [\u4e00-\u9fa5]和[\u2E80-\u9FFF],后者范围更广,包括了日韩地区的汉字 import re pattern = re.compile("[\ ...

  8. 15.centos7基础学习与积累-001

    1.从头开始积累centos7系统运用 大牛博客:https://blog.51cto.com/yangrong/p5 做到精通这个操作系统, 互联网常用服务器品牌: dell:       4u   ...

  9. mysql5.7切换导致gtid不一致

    今天在公司的工程环境中做了个案例,手动切换关闭主库的mysql服务,从库上升为主库之后,发现主库处于read_only状态,通过高可用的组件观察了剩余主从库的alive以及delay的状态发现均正常. ...

  10. CF: Long Number

                                                    题目链接 #include<iostream> #include<string> ...