当前位置: 首页 > article >正文

浙大数据结构:04-树5 Root of AVL Tree

这道题核心平衡树的代码在MOOC上有,需要完善修改即可。
机翻

 1、条件准备

定义结构体,高度,值,左结点,右结点
#include <iostream>
#include <stdlib.h>
using namespace std;
typedef struct AVLNode *position;
typedef position AVLTree;
typedef int ElementType;
struct AVLNode
{
  ElementType data;
  AVLTree Left;
  AVLTree Right;
  int height;
};
主函数读入结点,调用insert函数插入,最后输出根节点值
int main()
{
  int n;
  cin >> n;
  AVLTree T = NULL;
  for (int i = 0; i < n; i++)
  {
    int a;
    cin >> a;
    T = insert(T, a);
  }
  cout << T->data;

  return 0;
}

2、平衡树四种旋转

具体逻辑在MOOC的ppt上有讲述,如果对代码不太理解我建议举例子推一遍比较好

AVLTree singleleftrotation(AVLTree A)
{
 //左左
  AVLTree B = A->Left;
  A->Left = B->Right;
  B->Right = A;
  A->height = max(getheight(A->Left), getheight(A->Right)) + 1;
  B->height = max(getheight(B->Left), A->height) + 1;
  return B;
}

AVLTree singlerightrotation(AVLTree A)
{
//右右
  AVLTree B = A->Right;
  A->Right = B->Left;
  B->Left = A;
  A->height = max(getheight(A->Left), getheight(A->Right)) + 1;
  B->height = max(getheight(B->Right), A->height) + 1;

  return B;
}

AVLTree doubleleftrotation(AVLTree A)
{
//左右
  A->Left = singlerightrotation(A->Left);
  return singleleftrotation(A);
}

AVLTree doublerightrotation(AVLTree A)
{
//右左
  A->Right = singleleftrotation(A->Right);
  return singlerightrotation(A);
}

3、getheight函数

获取树高度,递归实现
int getheight(AVLTree A)
{
  if (!A)
    return 0;
  return max(getheight(A->Left), getheight(A->Right)) + 1;
}

4、insert函数

跟MOOC上代码逻辑基本一样。
AVLTree insert(AVLTree T, ElementType x)
{

  if (!T)
  {
    AVLTree T = (AVLTree)malloc(sizeof(struct AVLNode));
    T->data = x;
    T->Right = T->Left = NULL;
    return T;
  }
  else if (x < T->data)
  {
    T->Left = insert(T->Left, x);
    if (getheight(T->Left) - getheight(T->Right) == 2)
    {
      if (x < T->Left->data)
        T = singleleftrotation(T);
      else
        T = doubleleftrotation(T);
    }
  }
  else if (x > T->data)
  {
    T->Right = insert(T->Right, x);
    if (getheight(T->Right) - getheight(T->Left) == 2)
    {
      if (x > T->Right->data)
        T = singlerightrotation(T);
      else
        T = doublerightrotation(T);
    }
  }
  T->height = max(getheight(T->Left), getheight(T->Right)) + 1;

  return T;
}

5、总结

这个题最难的在于平衡树代码和插入代码的实现,不过MOOC上也写出了不少,所以难度不算大。
完整代码如下:
#include <iostream>
#include <stdlib.h>
using namespace std;
typedef struct AVLNode *position;
typedef position AVLTree;
typedef int ElementType;
struct AVLNode
{
  ElementType data;
  AVLTree Left;
  AVLTree Right;
  int height;
};

int getheight(AVLTree A)
{
  if (!A)
    return 0;
  return max(getheight(A->Left), getheight(A->Right)) + 1;
}

AVLTree singleleftrotation(AVLTree A)
{
  AVLTree B = A->Left;
  A->Left = B->Right;
  B->Right = A;
  A->height = max(getheight(A->Left), getheight(A->Right)) + 1;
  B->height = max(getheight(B->Left), A->height) + 1;
  return B;
}

AVLTree singlerightrotation(AVLTree A)
{
  AVLTree B = A->Right;
  A->Right = B->Left;
  B->Left = A;
  A->height = max(getheight(A->Left), getheight(A->Right)) + 1;
  B->height = max(getheight(B->Right), A->height) + 1;

  return B;
}

AVLTree doubleleftrotation(AVLTree A)
{
  A->Left = singlerightrotation(A->Left);
  return singleleftrotation(A);
}

AVLTree doublerightrotation(AVLTree A)
{
  A->Right = singleleftrotation(A->Right);
  return singlerightrotation(A);
}

AVLTree insert(AVLTree T, ElementType x)
{

  if (!T)
  {
    AVLTree T = (AVLTree)malloc(sizeof(struct AVLNode));
    T->data = x;
    T->Right = T->Left = NULL;
    return T;
  }
  else if (x < T->data)
  {
    T->Left = insert(T->Left, x);
    if (getheight(T->Left) - getheight(T->Right) == 2)
    {
      if (x < T->Left->data)
        T = singleleftrotation(T);
      else
        T = doubleleftrotation(T);
    }
  }
  else if (x > T->data)
  {
    T->Right = insert(T->Right, x);
    if (getheight(T->Right) - getheight(T->Left) == 2)
    {
      if (x > T->Right->data)
        T = singlerightrotation(T);
      else
        T = doublerightrotation(T);
    }
  }
  T->height = max(getheight(T->Left), getheight(T->Right)) + 1;

  return T;
}

int main()
{
  int n;
  cin >> n;
  AVLTree T = NULL;
  for (int i = 0; i < n; i++)
  {
    int a;
    cin >> a;
    T = insert(T, a);
  }
  cout << T->data;

  return 0;
}


http://www.kler.cn/news/302687.html

相关文章:

  • 【C++\Qt项目实战】俄罗斯方块
  • iOS 15推出后利用邮件打开率的7种方法
  • 向量空间与函数空间的类比分析
  • Linux入门攻坚——32、Mini Linux制作
  • oracle 条件取反
  • Elasticsearch介绍以及solr对比
  • 高级java每日一道面试题-2024年9月03日-JVM篇-怎么判断对象是否可以被回收?
  • MySql-表的内外连接
  • QLable提升类
  • python画图|3D垂线标记
  • 九、Redis 的实际使用与Redis的设计
  • Android Auto助力电动汽车智能驾驶
  • Java面试篇基础部分-Java各种垃圾收集器
  • 电脑提示丢失mfc140u.dll的详细解决方案,mfc140u.dll文件是什么
  • DAY99 APP 攻防-小程序篇反编译外在抓包主包分包配置泄漏算法逆向未授权
  • VS code 写下 print 时让编译器自动添加括号
  • 第二百二十五节 JPA教程 - JPA列长度示例、JPA列精度范围示例
  • 安卓framework美化手势导航侧滑返回UI
  • OpenAI O1:人工智能推理能力的新里程碑
  • 快讯丨深蓝L07将于9月20日正式上市
  • 【视频教程】遥感云大数据在灾害、水体与湿地领域典型案例实践及GPT模型应用
  • 【从问题中去学习k8s】k8s中的常见面试题(夯实理论基础)(二十七)
  • 自测的重要性
  • 如何分辨IP地址是否能够正常使用
  • DFS算法专题(二)——穷举vs暴搜vs深搜vs回溯vs剪枝【OF决策树】
  • 2024网络安全人才实战能力白皮书安全测试评估篇
  • Python中列表、元组、字典和集合的详细解释
  • 用Java实现人工智能
  • Linux 安装神州通用数据库 ShenTong7.0.8_342.92_linux64
  • 大数据集群搭建以及使用过程中几个实用的shell脚本