• 数据结构实验之查找二:平衡二叉树 (SDUT 3374)


    #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
    ",root->data);
        return 0;
    }
    
    
  • 相关阅读:
    springboot + swagger的实体类属性注解
    Idea破解办法+idea免费生成注册码+jsp属性选择器+注解什么的都报错
    springboot笔记(一)
    docker-compose 安装
    Spring cloud 分布式锁
    Mybait 快速生成Java POJO文件 及数据库Mapping文件。
    Git 提交所有文件
    Docker dockerfile-maven-plugin 使用
    Docker remote api 开启
    Linux git 关联 github仓库
  • 原文地址:https://www.cnblogs.com/lcchy/p/10139404.html
Copyright © 2020-2023  润新知