AVL树

AVL树的各种变换图
在这里插入图片描述
//AVL 树
#include
#include
#include
using namespace std;
template
struct TreeNode
{
T _val;
TreeNode* _left;
TreeNode* _right;
int bf;
TreeNode* _parent;
TreeNode(const T& val = T())
:_val(val), _left(nullptr), _right(nullptr),bf(0),_parent(nullptr)
{}
};
template
class AVLTree
{
public:
typedef TreeNode Node;
AVLTree()
:_root(nullptr)
{}
bool insert(const T& val)
{
Node* cur = _root;
Node* parent = nullptr;
if (cur == nullptr)
{
cur = new Node(val);
_root = cur;
return true;
}
while (cur)
{
parent = cur;
if (cur->_val == val)
return false;
else if (cur->_val > val)
{
cur = cur->_left;
}
else if (cur->_val < val)
{
cur = cur->_right;
}
}
cur = new Node(val);
if (cur->_val > parent->_val)
{
parent->_right = cur;
}
else
{
parent->_left = cur;
}
cur->_parent = parent;
while (parent)
{
if (parent->_left == cur)
{
parent->bf–;
}
else
{
parent->bf++;
}
if (parent->bf == 0)
{
break;
}
else if (parent->bf == 1 || parent->bf == -1)
{
cur = parent;
parent = cur->_parent;
}
else if (parent->bf == 2||parent->bf==-2)
{
if (parent->bf == 2 && cur->bf == 1)//右边的右边高
{
rorateLeft(parent);
}
else if (parent->bf == -2 && cur->bf == -1)//左边的左边高
{
rorateRight(parent);
}
else if(parent->bf2&&cur->bf-1)//右边的左边高
{
rorateRL(parent);
}
else //左边的右边高
{
rorateLR(parent);
}
break;
}
}
return true;
}
void rorateRight(Node* parent)
{
Node* SubL = parent->_left;
Node* SubLR = SubL->_right;
parent->_left = SubLR;
if (SubLR)
{
SubLR->_parent = parent;
}
SubL->_right = parent;
if (parent == _root)
{
SubL->_parent = nullptr;
_root = SubL;
}
else
{
SubL->_parent = parent->_parent;
if (parent->_parent->_left == parent)
{
parent->_parent->_left = SubL;
}
else
{
parent->_parent->_right = SubL;
}
}
parent->_parent = SubL;
parent->bf = SubL->bf = 0;
}
void rorateLeft(Node* parent)
{
Node* SubR = parent->_right;
Node* SubRL = SubR->_left;
parent->_right = SubRL;
if (SubRL)
{
SubRL->_parent = parent;
}
SubR->_left = parent;
if (parent == _root)
{
SubR->_parent = nullptr;
_root = SubR;
}
else
{
SubR->_parent = parent->_parent;
if (parent->_parent->_left == parent)
{
parent->_parent->_left = SubR;
}
else
parent->_parent->_right = SubR;
}
parent->_parent = SubR;
parent->bf = SubR->bf = 0;
}
void rorateRL(Node* parent)
{
Node* SubR = parent->_right;
Node* SubRL = SubR->_left;
int bf = SubRL->bf;
rorateRight(SubR);
rorateLeft(parent);
if (bf == -1)
{
SubR->bf = 1;
parent->bf = 0;
}
else if (bf == 1)
{
SubR->bf = 0;
parent->bf = -1;
}
}
void rorateLR(Node* parent)
{
Node* SubL = parent->_left;
Node* SubLR = SubL->_right;
int bf = SubLR->bf;
rorateLeft(parent->_left);
rorateRight(parent);
if (bf == 1)
{
SubL->bf = -1;
parent->bf = 0;
}
else
{
SubL->bf = 0;
parent->bf = 1;
}
}
void inorder()
{
inorder(_root);
}
void inorder(Node* root)
{
if (root == nullptr)
return;
inorder(root->_left);
cout << root->_val << " ";
inorder(root->_right);
}
bool isBlance()
{
return isBlance(_root);
}
bool isBlance(Node* cur)
{
if (cur == nullptr)
return true;
//判断左右节点高度是否和平衡因子相同
int distance = High(cur->_right) - High(cur->_left);
if (cur->bf != distance)
{
cout << “异常” << endl;
return false;
}
return abs(cur->bf)<2&&isBlance(cur->_left) && isBlance(cur->_right);
}
int High(Node* parent)
{
if (parent == nullptr)
return 0;
int left = High(parent->_left);
int right = High(parent->_right);
return left > right ? left+1 : right+1;
}
private:
Node* _root;
};
void test()
{
AVLTreeavl;
int array[] = { 4, 2, 6, 1, 3, 5, 15, 7, 16,14 };
for (const auto& e : array)
avl.insert(e);
avl.inorder();
cout << endl;
cout << avl.isBlance() << endl;
}
int main()
{
test();
return 0;
}

【直流微电网】径向直流微电网的状态空间建模与线性化:一种耦合DC-DC变换器状态空间平均模型的方法 (Matlab代码实现)内容概要:本文介绍了径向直流微电网的状态空间建模与线性化方法,重点提出了一种基于耦合DC-DC变换器状态空间平均模型的建模策略。该方法通过对系统中多个相互耦合的DC-DC变换器进行统一建模,构建出整个微电网的集中状态空间模型,并在此基础上实施线性化处理,便于后续的小信号分析与稳定性研究。文中详细阐述了建模过程中的关键步骤,包括电路拓扑分析、状态变量选取、平均化处理以及雅可比矩阵的推导,最终通过Matlab代码实现模型仿真验证,展示了该方法在动态响应分析和控制器设计中的有效性。; 适合人群:具备电力电子、自动控制理论基础,熟悉Matlab/Simulink仿真工具,从事微电网、新能源系统建模与控制研究的研究生、科研人员及工程技术人员。; 使用场景及目标:①掌握直流微电网中多变换器系统的统一建模方法;②理解状态空间平均法在非线性电力电子系统中的应用;③实现系统线性化并用于稳定性分析与控制器设计;④通过Matlab代码复现和扩展模型,服务于科研仿真与教学实践。; 阅读建议:建议读者结合Matlab代码逐步理解建模流程,重点关注状态变量的选择与平均化处理的数学推导,同时可尝试修改系统参数或拓扑结构以加深对模型通用性和适应性的理解。
07-07
AVL是一种自平衡的二叉查找,它确保了的高度始终保持在对数级别,从而保证了查找、插入和删除操作的时间复杂度为O(log n)。这种数据结构以它的发明者G.M. Adelson-Velsky和E.M. Landis的名字命名[^1]。 ### AVL的定义 - **平衡因子**:每个节点都有一个平衡因子,它是该节点左子的高度减去右子的高度。对于AVL来说,所有节点的平衡因子只能是-1, 0或1。 - **空**:如果T是空,则它自然是一个AVL。 - **非空**:若T不是空,则其左右子TL和TR都必须是AVL,并且对于任意节点,|HL - HR| ≤ 1(其中HL和HR分别表示左子和右子的高度)。 ### AVL的操作 当进行插入或删除操作时,可能会破坏AVL的平衡性,这时需要通过旋转来重新恢复平衡: - **单旋转**: - 左单旋(Single Rotate with Left)用于处理左孩子的左子过高。 - 右单旋(Single Rotate with Right)用于处理右孩子的右子过高。 - **双旋转**: - 左右双旋(Double Rotate with Left)用于处理左孩子的右子过高的情况。 - 右左双旋(Double Rotate with Right)用于处理右孩子的左子过高的情况。 这些旋转操作可以保持AVL的性质不变,并且能够快速地调整的结构以维持平衡。 ### AVL的实现 下面是一个简单的C语言代码示例,展示了如何定义AVL的节点以及实现基本的插入操作。这个例子中包含了必要的函数声明和一些核心逻辑。 ```c #include <stdio.h> #include <stdlib.h> // 定义AVL节点结构体 typedef struct AvlNode { int element; struct AvlNode *left; struct AvlNode *right; int height; // 节点的高度 } *AvlTree, *Position; // 获取两个整数中的较大者 int max(int a, int b) { return (a > b) ? a : b; } // 计算给定节点的高度 static int Height(AvlTree T) { if (T == NULL) return -1; // 空节点高度为-1 else return T->height; } // 单旋转 - 左左情况 AvlTree singlerotatewithLeft(AvlTree K2) { Position K1 = K2->left; K2->left = K1->right; K1->right = K2; // 更新节点高度 K2->height = max(Height(K2->left), Height(K2->right)) + 1; K1->height = max(Height(K1->left), K2->height) + 1; return K1; // 新根 } // 单旋转 - 右右情况 AvlTree singlerotatewithRight(AvlTree K2) { Position K1 = K2->right; K2->right = K1->left; K1->left = K2; // 更新节点高度 K2->height = max(Height(K2->left), Height(K2->right)) + 1; K1->height = max(Height(K1->right), K2->height) + 1; return K1; // 新根 } // 双旋转 - 左右情况 AvlTree doublerotatewithLeft(AvlTree K3) { K3->left = singlerotatewithRight(K3->left); return singlerotatewithLeft(K3); } // 双旋转 - 右左情况 AvlTree doublerotatewithRight(AvlTree K3) { K3->right = singlerotatewithLeft(K3->right); return singlerotatewithRight(K3); } // 插入新元素到AVLAvlTree Insert(int x, AvlTree T) { if (T == NULL) { // 如果为空,创建新节点 T = (AvlTree)malloc(sizeof(struct AvlNode)); if (T == NULL) printf("Out of space!!!"); else { T->element = x; T->left = T->right = NULL; T->height = 0; // 新叶节点高度为0 } } else if (x < T->element) { // 向左子插入 T->left = Insert(x, T->left); // 检查并修复平衡 if (Height(T->left) - Height(T->right) == 2) { if (x < T->left->element) T = singlerotatewithLeft(T); // 左左旋转 else T = doublerotatewithLeft(T); // 左右旋转 } } else if (x > T->element) { // 向右子插入 T->right = Insert(x, T->right); // 检查并修复平衡 if (Height(T->right) - Height(T->left) == 2) { if (x > T->right->element) T = singlerotatewithRight(T); // 右右旋转 else T = doublerotatewithRight(T); // 右左旋转 } } // 更新高度 T->height = max(Height(T->left), Height(T->right)) + 1; return T; } ``` 上述代码提供了AVL的基本框架,包括节点定义、插入操作及必要的旋转方法。实际应用中可能还需要添加更多的功能,如删除节点、查找特定值等。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值