AVL树(平衡二叉树)的构建及其基本的插入操作

#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";
    }
}



  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值