#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;
}
AVL树
最新推荐文章于 2024-11-29 12:45:03 发布