[数据结构]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树的插入删除操作

//.......................avl.h #pragma once#include #include using namespace std; //...................

AVL树的插入删除操作

// Demo2.cpp : 定义控制台应用程序的入口点。 // #include "stdafx.h" #include using namespace std; #define max(a,...

AVL树的插入与删除操作

AVL树是一种较老的数据结构，它的出现是为了解决了二叉查找树在最坏情况下的插入结果。 二叉查找树（以下称为二叉树）如果能以较好的插入序列来创建，使得树的结构趋于平衡，则其大部分操作都可以O(logN)...

AVL树的插入和删除

avl tree的插入删除操作代码

#include using namespace std; struct node//avl tree结点 { int entry; struct node *left=NULL...

avl树的插入操作和删除操作

avl树相比于搜索二叉树每个结点是多了个平衡因子bf，avl树时时刻刻要维持树中的每个结点的平衡因子的绝对值小于等于１． avl树的插入操作： avl树因为要保证每个结点的平衡因子要时时刻刻都符合要求...

AVL树的插入_删除操作实现~

AVL的插入操作：找到所插位置之后，往上寻找可能发生不平衡之处x，若找到x则调整平衡。顶多调整平衡1次。 AVL的删除操作：找到删除结点x，类似于二叉查找树一样找到其后继或前驱y，若为x->righ...

举报原因： 您举报文章：深度学习：神经网络中的前向传播和反向传播算法推导 色情 政治 抄袭 广告 招聘 骂人 其他 (最多只允许输入30个字)