#include<iostream>
#include<algorithm>
#include<cctype>
#include<fstream>
#include<cstring>
using namespace std;
struct AVLTree {
int data;
struct AVLTree* left;
struct AVLTree* right;
int height = 0;
};//构建AVL树的基本结构
typedef struct AVLTree* node;
int getheight(node T)
{
if (T == NULL) return -1;
else return T->height;
}
node FindMin(node T) {
node h = new struct AVLTree;
h = T;
while (h->left) {
if (h->left) h = h->left;
}
return h;
}
node SingleLeftRotation(node A)//将平衡二叉树做左单旋
{
node B = new struct 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;
}
node SingleRightRotation(node A)//右单旋
{
node B = new struct 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;
}
node DoubleLeftRightRotation(node A)//左-右单旋
{
A->left = SingleRightRotation(A->left);
return SingleLeftRotation(A);
}
node DoubleRightLeftRotation(node A)//右-左单旋
{
A->right = SingleLeftRotation(A->right);
return SingleRightRotation(A);
}
node Insert(node root, int x)//AVL树的插入操作
{
if (root == NULL) {
root = new struct AVLTree;
root->data = x;
root->height = 0;
root->left = root->right = NULL;
}
else if (x < root->data) {
root->left = Insert(root->left, x);
if (abs(getheight(root->left) - getheight(root->right)) == 2) {
if (x < root->left->data) {
root = SingleLeftRotation(root);
}
else {
root = DoubleLeftRightRotation(root);
}
}
}
else if (x > root->data) {
root->right = Insert(root->right, x);
if (abs(getheight(root->left) - getheight(root->right)) == 2) {
if (x < root->right->data) {
root = DoubleRightLeftRotation(root);
}
else {
root = SingleRightRotation(root);
}
}
}
root->height = max(getheight(root->left), getheight(root->right)) + 1;
return root;
}
node delete_tree(int T, node root)//AVL树的删除操作
{
if (root == NULL) return NULL;
else {
if (T < root->data) {
root->left = delete_tree(T, root->left);
if (getheight(root->right) - getheight(root->left) == 2) {
if (getheight(root->right->left) > getheight(root->right->right)) {
root = DoubleRightLeftRotation(root);
}
else root = SingleRightRotation(root);
}
}
else if (T > root->data) {
root->right = delete_tree(T, root->right);
if (getheight(root->left) - getheight(root->right) == 2) {
if (getheight(root->left->right) > getheight(root->left->left)) {
root = DoubleLeftRightRotation(root);
}
else {
root = SingleLeftRotation(root);
}
}
}
else if (T == root->data) {
if (root->left != NULL && root->right != NULL) {
node temp = FindMin(root->right);
root->data = temp->data;
root->right = delete_tree(temp->data, root->right);
}
else {
if (root->right == NULL && root->left != NULL) {
root = root->left;
}
else if (root->right != NULL && root->left == NULL) {
root = root->right;
}
else {
root = NULL;
}
}
}
if (root != NULL) root->height = max(getheight(root->left), getheight(root->right)) + 1;
return root;
}
}
node create_AVLTree(int n)//AVL树的创建
{
node head = new struct AVLTree;
head = NULL;
for (int i = 0; i < n; i++) {
int a; cin >> a;
head = Insert(head, a);
}
return head;
}
void printt_middle(node head)//中序遍历
{
if (head) {
printt_middle(head->left);
cout << head->data << " ";
printt_middle(head->right);
}
}
void printt_behind(node head)
{
if (head) {
printt_behind(head->left);
printt_behind(head->right);
cout << head->data << " ";
}
}
void printt_before(node head)
{
if (head) {
cout << head->data << " ";
printt_before(head->left);
printt_before(head->right);
}
}
int main()
{
int n; cin >> n;
node root = create_AVLTree(n);
printt_middle(root);
cout << endl;
printf("请输入想要删除的值:\n");
while (root) {
int a; cin >> a;
root = delete_tree(a, root);
printt_middle(root); cout << "\n";
}
}
AVL树(平衡二叉树)的构建及其基本的插入操作
最新推荐文章于 2022-05-26 22:49:39 发布