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

简述:

实现AVL 树,主要是两个功能 : 插入某节点和删除某节点

AVL Tree的定义,

1. 是一棵二叉搜索树(故而每个节点是惟一的, 如果出现重复数字会破坏平衡树的算法)

2. 每个节点左右子树的高度之差(平衡因子)相差最多为1

实现:

为了使所得的二叉树为平衡二叉树,

首先在BSTNode中加了一个计算节点高度的方法getHeight(), 当两个节点高度相差2的时候,视为平衡破坏

[cpp]
view plain
copy
print
?

  1. int getHeight(){  
  2.     if(this == NULL)  
  3.         return 0;  
  4.     if(left == NULL && right == NULL)  
  5.         return 1;  
  6.     else{  
  7.         return 1 + max(left->getHeight(), right->getHeight());  
  8.     }  
  9. }  
int getHeight(){
	if(this == NULL)
		return 0;
	if(left == NULL && right == NULL)
		return 1;
	else{
		return 1 + max(left->getHeight(), right->getHeight());
	}
}

之后讨论一下,不平衡出现的四种情况, 新增节点(红色)

1) LL,  新建在左子树的左节点上

LL代码实现:

[cpp]
view plain
copy
print
?

  1. template<class Type>  
  2. BSTNode<Type>* AVLTree<Type>::LL(BSTNode<Type>* &topNode){  
  3.     BSTNode<Type> * leftSonNode = topNode->left;  
  4.     topNode->left = leftSonNode->right;  
  5.     leftSonNode->right = topNode;  
  6.     return leftSonNode;  
  7. }  
template<class Type>
BSTNode<Type>* AVLTree<Type>::LL(BSTNode<Type>* &topNode){
	BSTNode<Type> * leftSonNode = topNode->left;
	topNode->left = leftSonNode->right;
	leftSonNode->right = topNode;
	return leftSonNode;
}

2) RR,  新建在右子树的右节点上

RR代码实现:

[cpp]
view plain
copy
print
?

  1. template<class Type>  
  2. BSTNode<Type>* AVLTree<Type>::RR(BSTNode<Type>* &topNode){  
  3.     BSTNode<Type> *rightSonNode = topNode->right;  
  4.     topNode->right = rightSonNode->left;  
  5.     rightSonNode->left = topNode;  
  6.     return rightSonNode;  
  7. }  
template<class Type>
BSTNode<Type>* AVLTree<Type>::RR(BSTNode<Type>* &topNode){
	BSTNode<Type> *rightSonNode = topNode->right;
	topNode->right = rightSonNode->left;
	rightSonNode->left = topNode;
	return rightSonNode;
}

3) LR,  新建在左子树的右节点上

LR代码实现:

[cpp]
view plain
copy
print
?

  1. template<class Type>  
  2. BSTNode<Type>* AVLTree<Type>::LR(BSTNode<Type>* &topNode){  
  3.     topNode->left = RR(topNode->left);  
  4.     return LL(topNode);  
  5. }  
template<class Type>
BSTNode<Type>* AVLTree<Type>::LR(BSTNode<Type>* &topNode){
	topNode->left = RR(topNode->left);
	return LL(topNode);
}

4) RL,  新建在右子树的左节点上

RL代码实现:

[cpp]
view plain
copy
print
?

  1. template<class Type>  
  2. BSTNode<Type>* AVLTree<Type>::RL(BSTNode<Type>* &topNode){  
  3.     topNode->right = LL(topNode->right);  
  4.     return RR(topNode);  
  5. }  
template<class Type>
BSTNode<Type>* AVLTree<Type>::RL(BSTNode<Type>* &topNode){
	topNode->right = LL(topNode->right);
	return RR(topNode);
}

对于删除操作,每一次删除一个节点之后,优先考虑其子节点的最左节点的值替换删除节点,但是需要注意的是,

在替换之后需要自修改节点向下每个节点做Rotate操作,用来处理因为删除某个节点之后平衡树的破坏

下面是Delete函数的实现:

[cpp]
view plain
copy
print
?

  1. template<class Type>  
  2. BSTNode<Type>* AVLTree<Type>::Delete(const Type& key){  
  3.     return root = Delete(root, key);  
  4. }  
  5.   
  6.   
  7. template<class Type>  
  8. BSTNode<Type>* AVLTree<Type>::Delete(BSTNode<Type>* &node, const Type &key){  
  9.     if(node == NULL){  
  10.         return NULL;  
  11.     }  
  12.     /** 
  13.      * if we find the matched key, 
  14.      * delete the matched node and replace it by the most left node 
  15.      * of its right child 
  16.      */  
  17.     else if(key == node->key){  
  18.         if(!node->right){  
  19.             BSTNode<Type> *newNode = node->left;  
  20.             delete node;  
  21.             return newNode;  
  22.         }else{  
  23.             BSTNode<Type> *secondMostLeftNode = node->right;  
  24.             if(secondMostLeftNode->left == NULL){  
  25.                 return secondMostLeftNode;  
  26.             }  
  27.             while(secondMostLeftNode->left->left)  
  28.                 secondMostLeftNode = secondMostLeftNode->left;  
  29.             BSTNode<Type> *mostLeftNode = secondMostLeftNode->left;  
  30.             secondMostLeftNode->left->left = node->left;  
  31.             secondMostLeftNode->left->right = node->right;  
  32.             secondMostLeftNode->left = NULL;  
  33.             return mostLeftNode;  
  34.         }  
  35.     }  
  36.     //from bottom to the top   
  37.     else if(key < node->key){  
  38.         node->left = Delete(node->left, key);  
  39.     }  
  40.     else{  
  41.         node->right = Delete(node->right, key);  
  42.     }  
  43.     if(node->left)  
  44.         node->left = Rotate(node->left);  
  45.     if(node->right)  
  46.         node->right = Rotate(node->right);  
  47.     node = Rotate(node);  
  48.     return node;  
  49. }  
  50.   
  51.   
  52. /** 
  53.  * Rotate one node and its sub tree 
  54.  */  
  55. template<class Type>  
  56. BSTNode<Type>* AVLTree<Type>::Rotate(BSTNode<Type>* node){  
  57.     if(node->left->getHeight() – node->right->getHeight() == 2){  
  58.         if(node->left->left->getHeight() >= node->left->right->getHeight())  
  59.             node = LL(node);  
  60.         else  
  61.             node = LR(node);  
  62.     }  
  63.     if(node->right->getHeight() – node->left->getHeight() == 2){  
  64.         if(node->right->right->getHeight() >= node->right->left->getHeight())  
  65.             node = RR(node);  
  66.         else  
  67.             node = RL(node);  
  68.     }  
  69.     return node;  
  70. }  
template<class Type>
BSTNode<Type>* AVLTree<Type>::Delete(const Type& key){
	return root = Delete(root, key);
}


template<class Type>
BSTNode<Type>* AVLTree<Type>::Delete(BSTNode<Type>* &node, const Type &key){
	if(node == NULL){
		return NULL;
	}
	/**
	 * if we find the matched key,
	 * delete the matched node and replace it by the most left node
	 * of its right child
	 */
	else if(key == node->key){
		if(!node->right){
			BSTNode<Type> *newNode = node->left;
			delete node;
			return newNode;
		}else{
			BSTNode<Type> *secondMostLeftNode = node->right;
			if(secondMostLeftNode->left == NULL){
				return secondMostLeftNode;
			}
			while(secondMostLeftNode->left->left)
				secondMostLeftNode = secondMostLeftNode->left;
			BSTNode<Type> *mostLeftNode = secondMostLeftNode->left;
			secondMostLeftNode->left->left = node->left;
			secondMostLeftNode->left->right = node->right;
			secondMostLeftNode->left = NULL;
			return mostLeftNode;
		}
	}
	//from bottom to the top
	else if(key < node->key){
		node->left = Delete(node->left, key);
	}
	else{
		node->right = Delete(node->right, key);
	}
	if(node->left)
		node->left = Rotate(node->left);
	if(node->right)
		node->right = Rotate(node->right);
	node = Rotate(node);
	return node;
}


/**
 * Rotate one node and its sub tree
 */
template<class Type>
BSTNode<Type>* AVLTree<Type>::Rotate(BSTNode<Type>* node){
	if(node->left->getHeight() - node->right->getHeight() == 2){
		if(node->left->left->getHeight() >= node->left->right->getHeight())
			node = LL(node);
		else
			node = LR(node);
	}
	if(node->right->getHeight() - node->left->getHeight() == 2){
		if(node->right->right->getHeight() >= node->right->left->getHeight())
			node = RR(node);
		else
			node = RL(node);
	}
	return node;
}

之后是整个平衡树插入删除节点实现以及在main函数中测试的代码:

[cpp]
view plain
copy
print
?

  1. #include <iostream>   
  2. #include <ctime>   
  3. #include <cstdlib>   
  4. using namespace std;  
  5.   
  6. template<class Type>  
  7. class AVLTree;  
  8.   
  9.   
  10. /** 
  11.  * Binary Search Tree Node:  BSTNode class 
  12.  */  
  13. template<class Type>  
  14. class BSTNode{  
  15. friend class AVLTree<Type>;  
  16. private:  
  17.     Type key;  
  18.     BSTNode *left;  
  19.     BSTNode *right;  
  20. public:  
  21.     BSTNode(): left(NULL), right(NULL){}  
  22.     BSTNode(const Type& key): key(key), left(NULL), right(NULL){}  
  23.     Type getkey(){return key;}  
  24.     int getHeight(){  
  25.         if(this == NULL)  
  26.             return 0;  
  27.         if(left == NULL && right == NULL)  
  28.             return 1;  
  29.         else{  
  30.             return 1 + max(left->getHeight(), right->getHeight());  
  31.         }  
  32.     }  
  33.     void clear(){  
  34.         if(this == NULL)  
  35.             return;  
  36.         left->clear();  
  37.         right->clear();  
  38.         delete this;  
  39.     }  
  40.     void Output_DLR(){ //Node -> left -> Right order   
  41.         if(this != NULL){  
  42.             cout << key << “, “;  
  43.             left->Output_DLR();  
  44.             right->Output_DLR();  
  45.         }  
  46.     }  
  47. };  
  48.   
  49.   
  50. /** 
  51.  * AVLTree class 
  52.  */  
  53. template<class Type>  
  54. class AVLTree{  
  55. private:  
  56.     BSTNode<Type> *root;  
  57. public:  
  58.     AVLTree(): root(NULL){}  
  59.     BSTNode<Type>* Insert(BSTNode<Type>* &, const Type&);  
  60.     BSTNode<Type>* Insert(const Type& );  
  61.     BSTNode<Type>* Delete(BSTNode<Type>* &, const Type&);  
  62.     BSTNode<Type>* Delete(const Type& );  
  63.     BSTNode<Type>* Rotate(BSTNode<Type>* );  
  64.     BSTNode<Type>* GetRoot();  
  65.     BSTNode<Type>* LL(BSTNode<Type>* &);  
  66.     BSTNode<Type>* LR(BSTNode<Type>* &);  
  67.     BSTNode<Type>* RL(BSTNode<Type>* &);  
  68.     BSTNode<Type>* RR(BSTNode<Type>* &);  
  69.     void Clear();  
  70.     void Output_DLR();  
  71.     void Output_LRN();  
  72. };  
  73.   
  74.   
  75. template<class Type>  
  76. BSTNode<Type>* AVLTree<Type>::LL(BSTNode<Type>* &topNode){  
  77.     BSTNode<Type> * leftSonNode = topNode->left;  
  78.     topNode->left = leftSonNode->right;  
  79.     leftSonNode->right = topNode;  
  80.     return leftSonNode;  
  81. }  
  82.   
  83.   
  84. template<class Type>  
  85. BSTNode<Type>* AVLTree<Type>::RR(BSTNode<Type>* &topNode){  
  86.     BSTNode<Type> *rightSonNode = topNode->right;  
  87.     topNode->right = rightSonNode->left;  
  88.     rightSonNode->left = topNode;  
  89.     return rightSonNode;  
  90. }  
  91.   
  92.   
  93. template<class Type>  
  94. BSTNode<Type>* AVLTree<Type>::LR(BSTNode<Type>* &topNode){  
  95.     topNode->left = RR(topNode->left);  
  96.     return LL(topNode);  
  97. }  
  98.   
  99.   
  100. template<class Type>  
  101. BSTNode<Type>* AVLTree<Type>::RL(BSTNode<Type>* &topNode){  
  102.     topNode->right = LL(topNode->right);  
  103.     return RR(topNode);  
  104. }  
  105.   
  106.   
  107. template<class Type>  
  108. BSTNode<Type>* AVLTree<Type>::GetRoot(){  
  109.     return root;  
  110. }  
  111.   
  112.   
  113. template<class Type>  
  114. BSTNode<Type>* AVLTree<Type>::Insert(const Type& key){  
  115.     return Insert(root, key);  
  116. }  
  117.   
  118.   
  119. template<class Type>  
  120. BSTNode<Type>* AVLTree<Type>::Insert(BSTNode<Type>* &node, const Type &key){  
  121.     if(node == NULL){  
  122.         return node = new BSTNode<Type>(key);  
  123.     }  
  124.     //from bottom to the top   
  125.     else if(key < node->key){  
  126.         Insert(node->left, key);  
  127.         if(node->left->getHeight() – node->right->getHeight() == 2){  
  128.             if(key < node->left->key)  
  129.                 node = LL(node);  
  130.             else  
  131.                 node = LR(node);  
  132.         }  
  133.     }  
  134.     else{  
  135.         Insert(node->right, key);  
  136.         if(node->right->getHeight() – node->left->getHeight() == 2){  
  137.             if(key > node->right->key)  
  138.                 node = RR(node);  
  139.             else  
  140.                 node = RL(node);  
  141.         }  
  142.     }  
  143.     return node;  
  144. }  
  145.   
  146.   
  147. template<class Type>  
  148. BSTNode<Type>* AVLTree<Type>::Delete(const Type& key){  
  149.     return root = Delete(root, key);  
  150. }  
  151.   
  152.   
  153. template<class Type>  
  154. BSTNode<Type>* AVLTree<Type>::Delete(BSTNode<Type>* &node, const Type &key){  
  155.     if(node == NULL){  
  156.         return NULL;  
  157.     }  
  158.     /** 
  159.      * if we find the matched key, 
  160.      * delete the matched node and replace it by the most left node 
  161.      * of its right child 
  162.      */  
  163.     else if(key == node->key){  
  164.         if(!node->right){  
  165.             BSTNode<Type> *newNode = node->left;  
  166.             delete node;  
  167.             return newNode;  
  168.         }else{  
  169.             BSTNode<Type> *secondMostLeftNode = node->right;  
  170.             if(secondMostLeftNode->left == NULL){  
  171.                 return secondMostLeftNode;  
  172.             }  
  173.             while(secondMostLeftNode->left->left)  
  174.                 secondMostLeftNode = secondMostLeftNode->left;  
  175.             BSTNode<Type> *mostLeftNode = secondMostLeftNode->left;  
  176.             secondMostLeftNode->left->left = node->left;  
  177.             secondMostLeftNode->left->right = node->right;  
  178.             secondMostLeftNode->left = NULL;  
  179.             return mostLeftNode;  
  180.         }  
  181.     }  
  182.     //from bottom to the top   
  183.     else if(key < node->key){  
  184.         node->left = Delete(node->left, key);  
  185.     }  
  186.     else{  
  187.         node->right = Delete(node->right, key);  
  188.     }  
  189.     if(node->left)  
  190.         node->left = Rotate(node->left);  
  191.     if(node->right)  
  192.         node->right = Rotate(node->right);  
  193.     node = Rotate(node);  
  194.     return node;  
  195. }  
  196.   
  197.   
  198. /** 
  199.  * Rotate one node and its sub tree 
  200.  */  
  201. template<class Type>  
  202. BSTNode<Type>* AVLTree<Type>::Rotate(BSTNode<Type>* node){  
  203.     if(node->left->getHeight() – node->right->getHeight() == 2){  
  204.         if(node->left->left->getHeight() >= node->left->right->getHeight())  
  205.             node = LL(node);  
  206.         else  
  207.             node = LR(node);  
  208.     }  
  209.     if(node->right->getHeight() – node->left->getHeight() == 2){  
  210.         if(node->right->right->getHeight() >= node->right->left->getHeight())  
  211.             node = RR(node);  
  212.         else  
  213.             node = RL(node);  
  214.     }  
  215.     return node;  
  216. }  
  217.   
  218.   
  219.   
  220. template<class Type>  
  221. void AVLTree<Type>::Clear(){  
  222.     root->clear();  
  223.     root = NULL;  
  224. }  
  225.   
  226.   
  227. template<class Type>  
  228. void AVLTree<Type>::Output_DLR(){  
  229.     if(!root)  
  230.         cout << “EMPTY TREE! “ << endl;  
  231.     else  
  232.         root->Output_DLR();  
  233. }  
  234.   
  235.   
  236. template<class Type>  
  237. void AVLTree<Type>::Output_LRN(){  
  238.     if(!root)  
  239.         cout << “EMPTY TREE! “ << endl;  
  240.     else  
  241.         root->Output_LRN();  
  242. }  
  243.   
  244. //Test Main   
  245. int main() {  
  246.     AVLTree<int> *tree = new AVLTree<int>();  
  247.     cout << “First, Test Insert(key) funciton: “ << endl;  
  248.     cout << “Test LL : “ << endl;  
  249.     //test LL   
  250.     tree->Insert(8);  
  251.     tree->Insert(6);  
  252.     tree->Insert(11);  
  253.     tree->Insert(4);  
  254.     tree->Insert(7);  
  255.     tree->Insert(2);  
  256.     cout << “DLR Output LL: “ << endl;  
  257.     tree->GetRoot()->Output_DLR();  
  258.     tree->Clear();  
  259.   
  260.     //test RR   
  261.     cout << endl << endl << “Test RR : “ << endl;  
  262.     tree->Insert(8);  
  263.     tree->Insert(6);  
  264.     tree->Insert(10);  
  265.     tree->Insert(9);  
  266.     tree->Insert(12);  
  267.     tree->Insert(14);  
  268.     cout << “DLR Output RR: “ << endl;  
  269.     tree->GetRoot()->Output_DLR();  
  270.     tree->Clear();  
  271.   
  272.     //test LR   
  273.     cout << endl << endl << “Test LR : “ << endl;  
  274.     tree->Insert(9);  
  275.     tree->Insert(6);  
  276.     tree->Insert(11);  
  277.     tree->Insert(4);  
  278.     tree->Insert(7);  
  279.     tree->Insert(8);  
  280.     cout << “DLR Output LR: “ << endl;  
  281.     tree->GetRoot()->Output_DLR();  
  282.     tree->Clear();  
  283.   
  284.   
  285.     //test RL   
  286.     cout << endl << endl << “Test RL : “ << endl;  
  287.     tree->Insert(6);  
  288.     tree->Insert(4);  
  289.     tree->Insert(12);  
  290.     tree->Insert(10);  
  291.     tree->Insert(14);  
  292.     tree->Insert(8);  
  293.     cout << “DLR Output RL: “ << endl;  
  294.     tree->GetRoot()->Output_DLR();  
  295.     tree->Clear();  
  296.   
  297.     //test Delete(const Type& )   
  298.     cout << endl << endl << “Test Delete : “ << endl;  
  299.     tree->Insert(6);  
  300.     tree->Insert(7);  
  301.     tree->Insert(9);  
  302.     tree->Insert(13);  
  303.     tree->Insert(15);  
  304.     tree->Insert(4);  
  305.     tree->Insert(5);  
  306.     tree->Insert(17);  
  307.     tree->Insert(19);  
  308.     tree->Insert(12);  
  309.     tree->Insert(10);  
  310.     tree->Insert(14);  
  311.     tree->Insert(8);  
  312.     cout << “DLR Output Before Delete: “ << endl;  
  313.     tree->Output_DLR();  
  314.     tree->Delete(7);  
  315.     cout << endl << “DLR Output After Delete: “ << endl;  
  316.     tree->Output_DLR();  
  317.     tree->Clear();  
  318.   
  319.     return 0;  
  320. }  
#include <iostream>
#include <ctime>
#include <cstdlib>
using namespace std;

template<class Type>
class AVLTree;


/**
 * Binary Search Tree Node:  BSTNode class
 */
template<class Type>
class BSTNode{
friend class AVLTree<Type>;
private:
	Type key;
	BSTNode *left;
	BSTNode *right;
public:
	BSTNode(): left(NULL), right(NULL){}
	BSTNode(const Type& key): key(key), left(NULL), right(NULL){}
	Type getkey(){return key;}
	int getHeight(){
		if(this == NULL)
			return 0;
		if(left == NULL && right == NULL)
			return 1;
		else{
			return 1 + max(left->getHeight(), right->getHeight());
		}
	}
	void clear(){
		if(this == NULL)
			return;
		left->clear();
		right->clear();
		delete this;
	}
	void Output_DLR(){ //Node -> left -> Right order
		if(this != NULL){
			cout << key << ", ";
			left->Output_DLR();
			right->Output_DLR();
		}
	}
};


/**
 * AVLTree class
 */
template<class Type>
class AVLTree{
private:
	BSTNode<Type> *root;
public:
	AVLTree(): root(NULL){}
	BSTNode<Type>* Insert(BSTNode<Type>* &, const Type&);
	BSTNode<Type>* Insert(const Type& );
	BSTNode<Type>* Delete(BSTNode<Type>* &, const Type&);
	BSTNode<Type>* Delete(const Type& );
	BSTNode<Type>* Rotate(BSTNode<Type>* );
	BSTNode<Type>* GetRoot();
	BSTNode<Type>* LL(BSTNode<Type>* &);
	BSTNode<Type>* LR(BSTNode<Type>* &);
	BSTNode<Type>* RL(BSTNode<Type>* &);
	BSTNode<Type>* RR(BSTNode<Type>* &);
	void Clear();
	void Output_DLR();
	void Output_LRN();
};


template<class Type>
BSTNode<Type>* AVLTree<Type>::LL(BSTNode<Type>* &topNode){
	BSTNode<Type> * leftSonNode = topNode->left;
	topNode->left = leftSonNode->right;
	leftSonNode->right = topNode;
	return leftSonNode;
}


template<class Type>
BSTNode<Type>* AVLTree<Type>::RR(BSTNode<Type>* &topNode){
	BSTNode<Type> *rightSonNode = topNode->right;
	topNode->right = rightSonNode->left;
	rightSonNode->left = topNode;
	return rightSonNode;
}


template<class Type>
BSTNode<Type>* AVLTree<Type>::LR(BSTNode<Type>* &topNode){
	topNode->left = RR(topNode->left);
	return LL(topNode);
}


template<class Type>
BSTNode<Type>* AVLTree<Type>::RL(BSTNode<Type>* &topNode){
	topNode->right = LL(topNode->right);
	return RR(topNode);
}


template<class Type>
BSTNode<Type>* AVLTree<Type>::GetRoot(){
	return root;
}


template<class Type>
BSTNode<Type>* AVLTree<Type>::Insert(const Type& key){
	return Insert(root, key);
}


template<class Type>
BSTNode<Type>* AVLTree<Type>::Insert(BSTNode<Type>* &node, const Type &key){
	if(node == NULL){
		return node = new BSTNode<Type>(key);
	}
	//from bottom to the top
	else if(key < node->key){
		Insert(node->left, key);
		if(node->left->getHeight() - node->right->getHeight() == 2){
			if(key < node->left->key)
				node = LL(node);
			else
				node = LR(node);
		}
	}
	else{
		Insert(node->right, key);
		if(node->right->getHeight() - node->left->getHeight() == 2){
			if(key > node->right->key)
				node = RR(node);
			else
				node = RL(node);
		}
	}
	return node;
}


template<class Type>
BSTNode<Type>* AVLTree<Type>::Delete(const Type& key){
	return root = Delete(root, key);
}


template<class Type>
BSTNode<Type>* AVLTree<Type>::Delete(BSTNode<Type>* &node, const Type &key){
	if(node == NULL){
		return NULL;
	}
	/**
	 * if we find the matched key,
	 * delete the matched node and replace it by the most left node
	 * of its right child
	 */
	else if(key == node->key){
		if(!node->right){
			BSTNode<Type> *newNode = node->left;
			delete node;
			return newNode;
		}else{
			BSTNode<Type> *secondMostLeftNode = node->right;
			if(secondMostLeftNode->left == NULL){
				return secondMostLeftNode;
			}
			while(secondMostLeftNode->left->left)
				secondMostLeftNode = secondMostLeftNode->left;
			BSTNode<Type> *mostLeftNode = secondMostLeftNode->left;
			secondMostLeftNode->left->left = node->left;
			secondMostLeftNode->left->right = node->right;
			secondMostLeftNode->left = NULL;
			return mostLeftNode;
		}
	}
	//from bottom to the top
	else if(key < node->key){
		node->left = Delete(node->left, key);
	}
	else{
		node->right = Delete(node->right, key);
	}
	if(node->left)
		node->left = Rotate(node->left);
	if(node->right)
		node->right = Rotate(node->right);
	node = Rotate(node);
	return node;
}


/**
 * Rotate one node and its sub tree
 */
template<class Type>
BSTNode<Type>* AVLTree<Type>::Rotate(BSTNode<Type>* node){
	if(node->left->getHeight() - node->right->getHeight() == 2){
		if(node->left->left->getHeight() >= node->left->right->getHeight())
			node = LL(node);
		else
			node = LR(node);
	}
	if(node->right->getHeight() - node->left->getHeight() == 2){
		if(node->right->right->getHeight() >= node->right->left->getHeight())
			node = RR(node);
		else
			node = RL(node);
	}
	return node;
}



template<class Type>
void AVLTree<Type>::Clear(){
	root->clear();
	root = NULL;
}


template<class Type>
void AVLTree<Type>::Output_DLR(){
	if(!root)
		cout << "EMPTY TREE! " << endl;
	else
		root->Output_DLR();
}


template<class Type>
void AVLTree<Type>::Output_LRN(){
	if(!root)
		cout << "EMPTY TREE! " << endl;
	else
		root->Output_LRN();
}

//Test Main
int main() {
	AVLTree<int> *tree = new AVLTree<int>();
	cout << "First, Test Insert(key) funciton: " << endl;
    cout << "Test LL : " << endl;
    //test LL
    tree->Insert(8);
    tree->Insert(6);
    tree->Insert(11);
    tree->Insert(4);
    tree->Insert(7);
    tree->Insert(2);
    cout << "DLR Output LL: " << endl;
    tree->GetRoot()->Output_DLR();
    tree->Clear();

    //test RR
    cout << endl << endl << "Test RR : " << endl;
    tree->Insert(8);
    tree->Insert(6);
    tree->Insert(10);
    tree->Insert(9);
    tree->Insert(12);
    tree->Insert(14);
    cout << "DLR Output RR: " << endl;
    tree->GetRoot()->Output_DLR();
    tree->Clear();

    //test LR
    cout << endl << endl << "Test LR : " << endl;
    tree->Insert(9);
    tree->Insert(6);
    tree->Insert(11);
    tree->Insert(4);
    tree->Insert(7);
    tree->Insert(8);
    cout << "DLR Output LR: " << endl;
    tree->GetRoot()->Output_DLR();
    tree->Clear();


    //test RL
    cout << endl << endl << "Test RL : " << endl;
    tree->Insert(6);
    tree->Insert(4);
    tree->Insert(12);
    tree->Insert(10);
    tree->Insert(14);
    tree->Insert(8);
    cout << "DLR Output RL: " << endl;
    tree->GetRoot()->Output_DLR();
    tree->Clear();

    //test Delete(const Type& )
    cout << endl << endl << "Test Delete : " << endl;
    tree->Insert(6);
    tree->Insert(7);
    tree->Insert(9);
    tree->Insert(13);
    tree->Insert(15);
    tree->Insert(4);
    tree->Insert(5);
    tree->Insert(17);
    tree->Insert(19);
    tree->Insert(12);
    tree->Insert(10);
    tree->Insert(14);
    tree->Insert(8);
    cout << "DLR Output Before Delete: " << endl;
    tree->Output_DLR();
    tree->Delete(7);
    cout << endl << "DLR Output After Delete: " << endl;
    tree->Output_DLR();
    tree->Clear();

	return 0;
}


测试输出:

对于最后的测试Delete的结果,平衡树的改变可以观察下面这副,数据是相同的

http://blog.csdn.net/anialy/article/details/7985165

    原文作者:平衡二叉树
    原文地址: https://blog.csdn.net/lanyanjiayu/article/details/17561629
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞