#include <iostream>
using namespace std;
//二叉平衡搜索树
struct TreeNode {
int val;
int height;
TreeNode *left;
TreeNode *right;
TreeNode(int x) : val(x), height(1), left(NULL), right(NULL) {
}
};
int getHeight(TreeNode *AVL) {
int height = 0;
if (AVL) {
height = AVL->height;
}
return height;
}
int getMax(int a, int b) {
return a > b ? a : b;
}
//左单旋
TreeNode *adjustLL(TreeNode *AVL) {
TreeNode *child = AVL->left;
AVL->left = child->right;
child->right = AVL;
AVL->height = getMax(getHeight(AVL->left), getHeight(AVL->right)) + 1;
child->height = getMax(getHeight(child->left), AVL->height) + 1;
return child;
}
//右单旋
TreeNode *adjustRR(TreeNode *AVL) {
TreeNode *child = AVL->right;
AVL->right = child->left;
child->left = AVL;
AVL->height = getMax(getHeight(AVL-&g
平衡二叉树的插入方法
最新推荐文章于 2024-05-14 14:15:01 发布