在实现34重构后,我们只需要实现插入和删除数据的函数就可以完成AVL树的实现了,AVL的重构核心已经给出了,不再赘述。直接放上完成的代码。
#include <bits/stdc++.h>
typedef struct node {
int data;
struct node *father;
struct node *lch;
struct node *rch;
} Node, *ptrNode;
int height(ptrNode ptr) {
int lch, rch;
if (ptr == NULL) {
return -1;
}
if (ptr->lch == NULL && ptr->rch == NULL) {
return 0;
}
lch = height(ptr->lch);
rch = height(ptr->rch);
return (lch > rch ? lch : rch) + 1;
}
bool isbalanced(ptrNode ptr) {
int hleft, hright;
if (ptr == NULL) {
return true;
}
hleft = height(ptr->lch);
hright = height(ptr->rch);
if (abs(hleft - hright) < 2) {
return true;
}
return false;
}
void preorder(ptrNode root) {
if (root == NULL)
return;
printf("%d ", root->data);
preorder(root->lch);
preorder(root->rch);
}
ptrNode create(int data) {
ptrNode temp = new Node;
temp->data = data;
temp->lch = NULL;
temp->rch = NULL;
temp->father = NULL;
return temp;
}
ptrNode rerotate(ptrNode a, ptrNode b, ptrNode c, ptrNode T1, ptrNode T2,
ptrNode T3, ptrNode T4) {
//调整内部关系,完成34重构
if (T1)
T1->father = a;
a->lch = T1;
if (T2)
T2->father = a;
a->rch = T2;
if (T3)
T3->father = c;
c->lch = T3;
if (T4)
T4->father = c;
c->rch = T4;
a->father = b, b->lch = a;
c->father = b, b->rch = c;
return b;
}
ptrNode operate_to_balance(ptrNode &a, ptrNode a_father) {
// a_father为这一个子树的父结点,方便连接
ptrNode b = (height(a->lch) >= height(a->rch)) ? (a->lch) : (a->rch);
ptrNode c = (height(b->lch) >= height(b->rch)) ? (b->lch) : (b->rch);
ptrNode father_control;
if (a->lch == b && b->lch == c) { // left left
father_control = rerotate(c, b, a, c->lch, c->rch, b->rch, a->rch);
}
if (a->lch == b && b->rch == c) { // left right
father_control = rerotate(b, c, a, b->lch, c->lch, c->rch, a->rch);
}
if (a->rch == b && b->lch == c) { // right left
father_control = rerotate(a, c, b, a->lch, c->lch, c->rch, b->rch);
}
if (a->rch == b && b->rch == c) { // right right
father_control = rerotate(a, b, c, a->lch, b->lch, c->lch, c->rch);
}
father_control->father = a_father;
return father_control;
}
ptrNode find(ptrNode ptr, int data) {
ptrNode temp = NULL;
if (ptr->data == data) {
return ptr;
}
if (ptr->data > data) {
if (ptr->lch != NULL) {
temp = find(ptr->lch, data);
}
} else {
if (ptr->rch != NULL) {
temp = find(ptr->rch, data);
}
}
return temp;
}
ptrNode addnode(ptrNode ptr, int data, ptrNode root) {
if (data < ptr->data) {
if (ptr->lch == NULL) {
ptr->lch = create(data);
ptr->lch->father = ptr;
for (ptrNode _rebal = ptr; _rebal; _rebal = _rebal->father) {
if (!isbalanced(_rebal)) {
if (_rebal->father == NULL) {
root = operate_to_balance(_rebal, NULL);
root->father = NULL;
return root;
}
if (_rebal->father->lch == _rebal) {
_rebal->father->lch =
operate_to_balance(_rebal, _rebal->father);
} else {
_rebal->father->rch =
operate_to_balance(_rebal, _rebal->father);
}
while (_rebal->father != NULL) {
_rebal = _rebal->father;
}
root = _rebal;
break;
}
}
} else {
root = addnode(ptr->lch, data, root);
}
} else {
if (ptr->rch == NULL) {
ptr->rch = create(data);
ptr->rch->father = ptr;
for (ptrNode _rebal = ptr; _rebal; _rebal = _rebal->father) {
if (!isbalanced(_rebal)) {
if (_rebal->father == NULL) {
root = operate_to_balance(_rebal, NULL);
root->father = NULL;
return root;
}
if (_rebal->father->lch == _rebal) {
_rebal->father->lch =
operate_to_balance(_rebal, _rebal->father);
} else {
_rebal->father->rch =
operate_to_balance(_rebal, _rebal->father);
}
while (_rebal->father != NULL) {
_rebal = _rebal->father;
}
root = _rebal;
break;
}
}
} else {
root = addnode(ptr->rch, data, root);
}
}
return root;
}
ptrNode deletedata(ptrNode ptr, int data) {
ptrNode _delete = find(ptr, data);
ptrNode _rebal;
if (_delete->lch == NULL && _delete->rch == NULL) { //_delete是叶节点
if (_delete->father == NULL) {
delete _delete;
_delete = NULL;
return NULL;
}
if (_delete->father->lch == _delete) {
_delete->father->lch = NULL;
} else {
_delete->father->rch = NULL;
}
} else if (_delete->lch != NULL && _delete->rch == NULL) {
if (_delete->father == NULL) {
_delete->lch->father = NULL;
ptrNode root = _delete->lch;
delete _delete;
_delete = NULL;
return root;
} else {
if (_delete->father->lch == _delete) {
_delete->father->lch = _delete->lch;
_delete->lch->father = _delete->father;
} else {
_delete->father->rch = _delete->lch;
_delete->lch->father = _delete->father;
}
}
} else if (_delete->lch == NULL && _delete->rch != NULL) {
if (_delete->father == NULL) {
_delete->rch->father = NULL;
ptrNode root = _delete->rch;
delete _delete;
_delete = NULL;
return root;
} else {
if (_delete->father->lch == _delete) {
_delete->father->lch = _delete->rch;
_delete->rch->father = _delete->father;
} else {
_delete->father->rch = _delete->rch;
_delete->rch->father = _delete->father;
}
}
} else if (_delete->lch != NULL && _delete->rch != NULL) {
ptrNode replace = _delete->rch;
ptrNode root;
while (replace->lch) {
replace = replace->lch;
}
_delete->data = replace->data;
root = deletedata(_delete->rch, replace->data);
return root;
}
_rebal = _delete->father;
delete _delete;
_delete = NULL;
ptrNode root;
while (_rebal) {
if (!isbalanced(_rebal)) {
if (_rebal->father == NULL) {
root = operate_to_balance(_rebal, NULL);
root->father = NULL;
return root;
}
if (_rebal->father->lch == _rebal) {
_rebal->father->lch =
operate_to_balance(_rebal, _rebal->father);
} else {
_rebal->father->rch =
operate_to_balance(_rebal, _rebal->father);
}
}
if (_rebal->father == NULL) {
root = _rebal;
}
_rebal = _rebal->father;
}
return root;
}
int main() {
ptrNode tree, temp;
int data;
//以下是creat和addnode模块
scanf("%d", &data);
tree = create(data);
while (1) {
scanf("%d", &data);
if (data == 0)
break;
tree = addnode(tree, data, tree);
}
tree = deletedata(tree, 4);
printf("the height of this tree : %d\n", height(tree) + 1);
return 0;
}