浙大数据结构: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;
}
相关推荐
计算机小白一个36 分钟前
蓝桥杯 Java B 组之设计 LRU 缓存
java·算法·蓝桥杯
万事可爱^1 小时前
HDBSCAN:密度自适应的层次聚类算法解析与实践
算法·机器学习·数据挖掘·聚类·hdbscan
黑不溜秋的1 小时前
C++ 设计模式 - 策略模式
c++·设计模式·策略模式
大数据追光猿3 小时前
Python应用算法之贪心算法理解和实践
大数据·开发语言·人工智能·python·深度学习·算法·贪心算法
Dream it possible!3 小时前
LeetCode 热题 100_在排序数组中查找元素的第一个和最后一个位置(65_34_中等_C++)(二分查找)(一次二分查找+挨个搜索;两次二分查找)
c++·算法·leetcode
夏末秋也凉3 小时前
力扣-回溯-46 全排列
数据结构·算法·leetcode
南宫生3 小时前
力扣每日一题【算法学习day.132】
java·学习·算法·leetcode
柠石榴3 小时前
【练习】【回溯No.1】力扣 77. 组合
c++·算法·leetcode·回溯
Leuanghing3 小时前
【Leetcode】11. 盛最多水的容器
python·算法·leetcode
qy发大财3 小时前
加油站(力扣134)
算法·leetcode·职场和发展