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

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

1、条件准备

定义结构体,高度,值,左结点,右结点
cpp 复制代码
#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函数插入,最后输出根节点值
cpp 复制代码
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上有讲述,如果对代码不太理解我建议举例子推一遍比较好
cpp 复制代码
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函数

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

4、insert函数

跟MOOC上代码逻辑基本一样。
cpp 复制代码
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上也写出了不少,所以难度不算大。
完整代码如下:
cpp 复制代码
#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;
}
相关推荐
MATLAB代码顾问8 分钟前
如何用MATLAB计算多边形的几何中心
算法·机器学习·matlab
栩日月10 分钟前
Linux学习记录十四----------线程的创建和回收
linux·数据结构·学习
啊QQQQQ12 分钟前
C++11(3)
java·开发语言·c++
戊子仲秋12 分钟前
【LeetCode】每日一题 2024_9_13 预算内的最多机器人数目(滑动窗口、单调队列)
算法·leetcode
LN-ZMOI14 分钟前
CSP-J初赛每日题目2
c++·csp初赛
CV金科20 分钟前
蓝桥杯-STM32G431RBT6(UART解析字符串sscanf和解决串口BUG)
c语言·stm32·单片机·嵌入式硬件·mcu·算法·bug
机器学习之心27 分钟前
顶刊算法 | 鹈鹕算法POA-Transformer-LSTM多变量回归预测
算法·lstm·transformer·多变量回归预测·poa
WenGyyyL1 小时前
面试经典150题——多数元素
算法·哈希表·摩尔算法
Wendy_robot1 小时前
远程连接MySQL并操作
linux·c++·mysql
i嗑盐の小F1 小时前
【 ACM独立出版,见刊后1个月检索!!!】第二届通信网络与机器学习国际学术会议(CNML 2024,10月25-27)
网络·图像处理·人工智能·深度学习·算法·机器学习·计算机视觉