AVL树

#include <stdlib.h>
#include <stdio.h>
typedef struct Node {
    int height;
    int val;
    struct Node* lchild, *rchild;
} Node;

//定义虚拟叶子节点 方便失衡调整
Node __NIL;
#define NIL (&__NIL)
int static_init() {
    NIL->lchild = NIL->rchild = NIL;
    NIL->height = 0;
}
//在程序运行之前执行static_init()函数初始化虚拟叶子节点
int _______temp = static_init();

Node* getNewNode(int val) {
    Node *node = (Node*)malloc(sizeof(Node));
    node->lchild = node->rchild = NIL;
    node->val = val;
    node->height = 1;
    return node;
}
void UP(Node *root) {
    root->height = (root->lchild->height > root->rchild->height ? root->lchild->height : root->rchild->height) + 1;
}
Node* rotate_left(Node *root) {
    Node* temp = root->rchild;
    root->rchild = temp->lchild;
    temp->lchild = root;
    UP(root);
    UP(temp);
    return temp;
}
Node* rotate_right(Node *root) {
    Node* temp = root->lchild;
    root->lchild = temp->rchild;
    temp->rchild = root;
    UP(root);
    UP(temp);
    return temp;
}
Node* __maintain(Node* root) {
    if (root->lchild->height > root->rchild->height) {
        if (root->lchild->rchild->height > root->lchild->lchild->height) {
        //LR型失衡
            root->lchild = rotate_left(root->lchild);
        }
        //LL型失衡
        root = rotate_right(root);
    } else {
        if (root->rchild->lchild->height > root->rchild->rchild->height) {
        //RL型失衡
            root->rchild = rotate_right(root->rchild);
        }
        //RR型失衡
        root = rotate_left(root);
    }
    return root;
}
Node *maintain(Node* root) {
    if (abs(root->lchild->height - root->rchild->height) < 2) return root;
    root = __maintain(root);
    return root;
}
Node* processor(Node* node) {
    Node* temp = node->lchild;
    while (temp->rchild != NIL) {
        temp = temp->rchild;
    }
    return temp;
}
Node* delete_node(Node* root, int val) {
    if (root == NIL) return root;
    if (root->val > val) {
        root->lchild = delete_node(root->lchild, val);
    } else if (root->val < val) {
        root->rchild = delete_node(root->rchild, val);
    } else {
        if (root->lchild == NIL && root->rchild == NIL) {
            free(root);
            return NIL;
        } else if (root->lchild == NIL || root->rchild == NIL) {
            Node* temp = root->lchild == NIL ? root->rchild : root->lchild;
            free(root);
            return temp;
        } else {
            Node *temp = processor(root);
            root->val = temp->val;
            root->lchild = delete_node(root->lchild, temp->val);
        }
    }
    UP(root);
    root = maintain(root);
    return root;
}
Node* insert(Node *root, int val) {
    if (root == NIL) return getNewNode(val);
    if (root->val == val) return root;
    else if (root->val > val) {
        root->lchild = insert(root->lchild, val);
    } else {
        root->rchild = insert(root->rchild, val);
    }
    UP(root);
    root = maintain(root);
    return root;
}
void pre_order(Node *root) {
    if (root == NIL) return;
    printf("%d(%d %d)\n", root->val, root->lchild->val, root->rchild->val);
    pre_order(root->lchild);
    pre_order(root->rchild);
}
void clear(Node* root) {
    if (root == NIL) return;
    clear(root->lchild);
    clear(root->rchild);
    free(root);
}
int main() {
    int m, val;
    Node* root = NIL;
    while (scanf("%d %d", &m, &val) != EOF) {
        switch (m) {
            case 1:
                root = insert(root, val);
                break;
            case 2:
                root = delete_node(root, val);
                break;
            case 3:
                pre_order(root);
        }
    }
    clear(root);
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值