C++对生成的二叉平衡树,能实现数据的查找,插入,删除
时间: 2024-03-25 13:40:03 浏览: 43
是的,C++可以通过实现二叉平衡树来实现数据的查找、插入和删除。下面是一个简单的实现:
```c++
#include <iostream>
using namespace std;
// 定义二叉平衡树的节点结构体
struct AVLNode {
int val;
int height;
AVLNode* left;
AVLNode* right;
AVLNode(int v) : val(v), height(1), left(nullptr), right(nullptr) {}
};
// 计算节点高度的辅助函数
int getHeight(AVLNode* root) {
return root == nullptr ? 0 : root->height;
}
// 计算节点平衡因子的辅助函数
int getBalanceFactor(AVLNode* root) {
return root == nullptr ? 0 : getHeight(root->left) - getHeight(root->right);
}
// 更新节点高度的辅助函数
void updateHeight(AVLNode* root) {
root->height = max(getHeight(root->left), getHeight(root->right)) + 1;
}
// 左旋转的辅助函数
AVLNode* leftRotate(AVLNode* root) {
AVLNode* newRoot = root->right;
root->right = newRoot->left;
newRoot->left = root;
updateHeight(root);
updateHeight(newRoot);
return newRoot;
}
// 右旋转的辅助函数
AVLNode* rightRotate(AVLNode* root) {
AVLNode* newRoot = root->left;
root->left = newRoot->right;
newRoot->right = root;
updateHeight(root);
updateHeight(newRoot);
return newRoot;
}
// 插入节点的辅助函数
AVLNode* insertNode(AVLNode* root, int val) {
if (root == nullptr) {
return new AVLNode(val);
}
if (val < root->val) {
root->left = insertNode(root->left, val);
} else if (val > root->val) {
root->right = insertNode(root->right, val);
} else {
return root; // 已存在相同的节点,不需要插入
}
updateHeight(root);
int balanceFactor = getBalanceFactor(root);
if (balanceFactor > 1 && val < root->left->val) { // LL型
return rightRotate(root);
} else if (balanceFactor > 1 && val > root->left->val) { // LR型
root->left = leftRotate(root->left);
return rightRotate(root);
} else if (balanceFactor < -1 && val > root->right->val) { // RR型
return leftRotate(root);
} else if (balanceFactor < -1 && val < root->right->val) { // RL型
root->right = rightRotate(root->right);
return leftRotate(root);
}
return root;
}
// 查找节点的辅助函数
AVLNode* searchNode(AVLNode* root, int val) {
if (root == nullptr || root->val == val) {
return root;
}
if (val < root->val) {
return searchNode(root->left, val);
} else {
return searchNode(root->right, val);
}
}
// 查找最小节点的辅助函数
AVLNode* findMinNode(AVLNode* root) {
while (root->left != nullptr) {
root = root->left;
}
return root;
}
// 删除节点的辅助函数
AVLNode* deleteNode(AVLNode* root, int val) {
if (root == nullptr) {
return nullptr;
}
if (val < root->val) {
root->left = deleteNode(root->left, val);
} else if (val > root->val) {
root->right = deleteNode(root->right, val);
} else {
if (root->left == nullptr || root->right == nullptr) {
AVLNode* tmp = root->left == nullptr ? root->right : root->left;
if (tmp == nullptr) {
tmp = root;
root = nullptr;
} else {
*root = *tmp;
}
delete tmp;
} else {
AVLNode* tmp = findMinNode(root->right);
root->val = tmp->val;
root->right = deleteNode(root->right, tmp->val);
}
}
if (root == nullptr) {
return nullptr;
}
updateHeight(root);
int balanceFactor = getBalanceFactor(root);
if (balanceFactor > 1 && getBalanceFactor(root->left) >= 0) { // LL型
return rightRotate(root);
} else if (balanceFactor > 1 && getBalanceFactor(root->left) < 0) { // LR型
root->left = leftRotate(root->left);
return rightRotate(root);
} else if (balanceFactor < -1 && getBalanceFactor(root->right) <= 0) { // RR型
return leftRotate(root);
} else if (balanceFactor < -1 && getBalanceFactor(root->right) > 0) { // RL型
root->right = rightRotate(root->right);
return leftRotate(root);
}
return root;
}
int main() {
AVLNode* root = nullptr;
root = insertNode(root, 4);
root = insertNode(root, 2);
root = insertNode(root, 1);
root = insertNode(root, 3);
root = insertNode(root, 6);
root = insertNode(root, 5);
root = insertNode(root, 7);
root = deleteNode(root, 3);
AVLNode* target = searchNode(root, 5);
cout << target->val << endl; // 输出 5
return 0;
}
```
在上述代码中,我们实现了一个二叉平衡树,包括节点的插入、查找和删除操作。通过AVL树的自平衡特性,可以保证树的高度始终在 $O(\log n)$ 的范围内,从而实现了高效的数据操作。