/*
平衡二叉树(Balanced Binary Tree)(AVL Tree)
平衡因子(Balance Factor):BF(T) = LHeight - RHeight
|BF| <= 1
调整:
1. 左子树的左边插入,LL插入:向右旋转(顺时针)。-> 为何叫做左单旋?唉,真是让人难以理解。
2. 右子树的右边插入,RR插入:向左旋转(逆时针)。
3. 左子树的右边插入,LR插入:对左子树进行逆时针旋转,再顺时针旋转。
4. 右子树的左边插入,RL插入:对右子树进行顺时针旋转,在逆时针旋转。
*/
#ifndef AVLTREE_H
#define AVLTREE_H
#include <algorithm> //std::max
using namespace std;
using T = int; //Element Type
class AVLTree {
//节点类
struct Node {
T data;
Node *left, *right;
int height;
Node(const T &data, Node* const &left=nullptr, Node* const &right=nullptr, const int &height=0):
data(data), left(left), right(right), height(height) {}
};
public:
AVLTree(); //生成一颗空树。
// ~AVLTre
04-树5 Root of AVL Tree (25分)
最新推荐文章于 2024-05-01 14:31:27 发布