[数据结构]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;
}

阅读更多
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/yuchenchenyi/article/details/51558325
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭