# [数据结构]10.4实现avl Tree的插入和删除操作。

//**********************************************************插入函数****************************************************

template<class Record>
Error_code AVL_tree<Record>::insert(const Record & new_data)
{
bool taller;
return avl_insert(root, new_data, taller);
}

template<class Record>
Error_code AVL_tree<Record>::avl_insert(Binary_node<Record>*& sub_root, const Record & new_data, bool & taller)
{
if (sub_root == NULL) {
sub_root = new AVL_node<Record>(new_data);
taller = true;
return success;
}
else if (sub_root->data == new_data) {
taller = false;
return duplicate_error;
}
else if (sub_root->data > new_data) {
Error_code result = avl_insert(sub_root->left_child, new_data, taller);
if (taller == true) {
switch (sub_root->get_balance())
{
case left_higher:
left_balance(sub_root);
taller = false;
break;
case equal_height:
sub_root->set_balance(left_higher);
break;
case right_higher:
sub_root->set_balance(equal_height);
taller = false;
break;
}
}
return result;
}
else {
Error_code result = avl_insert(sub_root->right_child, new_data, taller);
if (taller == true) {
switch (sub_root->get_balance())
{
case left_higher:
sub_root->set_balance(equal_height);
taller = false;
break;
case equal_height:
sub_root->set_balance(right_higher);
break;
case right_higher:
right_balance(sub_root);
taller = false;
break;
}
}
return result;
}
}

//*******************************************************************删除函数********************************************************
template<class Record>
Error_code AVL_tree<Record>::remove(Record & old_data)
{
bool shorter = true;
return avl_remove(root, old_data, shorter);
}

template<class Record>
Error_code AVL_tree<Record>::avl_remove(Binary_node<Record>*& sub_root, Record & new_data, bool & shorter)
{
Error_code result;
if (sub_root == NULL) {
shorter = false;
return not_present;
}
else if (new_data == sub_root->data) {
Binary_node<Record>*to_delete = sub_root;
if (sub_root->right_child == NULL) {
sub_root = sub_root->left_child;
shorter = true;
delete to_delete;
return success;
}
else if (sub_root->left_child == NULL) {
sub_root = sub_root->right_child;
shorter = true;
delete to_delete;
return success;
}
else {
to_delete = sub_root->left_child;
Binary_node<Record> *parent = sub_root;
while (!to_delete->right_child) {
parent = to_delete;
to_delete = to_delete->left_child;
}
sub_root->data = to_delete->data;
new_data = to_delete->data;
delete to_delete;
}
}
if (new_data < sub_root->data) {
result = avl_remove(sub_root->left_child, new_data, shorter);
if (shorter == true) {
switch (sub_root->get_balance())
{
case left_higher:
sub_root->set_balance(equal_height);
break;
case equal_height:
sub_root->set_balance(right_higher);
break;
case right_higher:
shorter = right_balance2(sub_root);
break;
}
}
}
if (new_data > sub_root->data) {
result = avl_remove(sub_root->right_child, new_data, shorter);
if (shorter == true) {
switch (sub_root->get_balance())
{
case left_higher:
shorter=left_balance2(sub_root);
break;
case equal_height:
break;
sub_root->set_balance(left_higher);
case right_higher:
sub_root->set_balance(equal_height);
break;
}
}
}
return result;
}


#### AVL树的插入与删除操作

2016-07-22 20:30:19

#### AVL树插入删除算法详解（有图） -- C++语言实现

2016-08-14 21:42:44

#### AVL树插入、删除的分析与实现

2014-07-10 10:19:23

#### AVL树的插入与删除

2016-05-25 11:00:23

#### 【数据结构】AVL树详解

2016-11-02 21:41:34

#### Avl树的基本操作（c语言实现）

2016-03-17 16:42:19

#### 数据结构（Java语言）——AVLTree简单实现

2015-08-18 21:44:33

#### java数据结构与算法之平衡二叉树(AVL树)的设计与实现

2016-12-31 23:40:43

#### AVL Tree 平衡二叉树基本插入删除节点功能的实现

2012-09-18 20:40:08

#### 数据结构&&AVL树原理、插入操作详解及实现

2014-12-08 23:59:07