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

Posted yccy1230

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[数据结构]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;


以上是关于[数据结构]10.4实现avl Tree的插入和删除操作。的主要内容,如果未能解决你的问题,请参考以下文章

Root of AVL Tree

红黑树(RB-tree)比AVL树的优势在哪?

数据结构树 —— 编程作业 06 :Root of AVL Tree

数据结构 ---[实现平衡树(AVL Tree)]

04-树5 Root of AVL Tree

Root of AVL Tree(二叉平衡树的建立)