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



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章