k1->h = max( height(k1->left),height(k1->right) ) + 1;
k2->h = max( height(k2->left),height(k2->right) ) + 1;
//k1=k2,因为在insert函数中传入的是p->left或者p->right的引用,所以这里能把根结点赋给其父结点的子节点
k2 = k1;
}
template<typename Type> void AVLTree<Type>::rotateLeftDouble(Node*& k3)
{
rotateRight(k3->left);
rotateLeft(k3);
}
template<typename Type> void AVLTree<Type>::rotateRightDouble(Node*& k3)
{
rotateLeft(k3->right);
rotateRight(k3);
}
template<typename Type> void AVLTree<Type>::traverse(Node* p)const
{
if( p == NULL)
return;
else
{
traverse(p->left);
cout<<"element:"<<p->e<<endl; //traverse by mid
traverse(p->right);
}
}
template<typename Type> void AVLTree<Type>::traverseByLevel(Node* root)const
{
if(root == NULL)
{
cout<<"The tree is empty"<<endl;
return;
}
queue<Node*> que;
que.push(root);
while( !que.empty() )
{
Node* ptr = que.front();
que.pop();
cout<<"element:"<<ptr->e<<" th:"<<height(ptr->left) - height(ptr->right)<<endl;
if(ptr->left != NULL)
que.push(ptr->left);
if(ptr->right != NULL)
que.push(ptr->right);
}
}
template<typename Type> typename AVLTree<Type>::Node* & AVLTree<Type>::getParent(Node* p)
{
if( p == m_root)
return NULL;
Node* ptr = m_root;
Node* ptf = ptr;
while( ptr != NULL )
{
if ( ptr->e == p->e )
return ptf;
if ( ptr->e > p->e )
{
ptf = ptr;
ptr = ptr->leftChild;
}
else
{
ptf = ptr;
ptr = ptr->rightChild;
}
}
}
template<typename Type> typename AVLTree<Type>::Node*& AVLTree<Type>::find(Type e)const
{
Node* ptr = m_root;
while(ptr != NULL)
{
if ( ptr->e == e )
return ptr;
if ( ptr->e > e )
ptr = ptr->leftChild;
else
ptr = ptr->rightChild;
}
//if ( ptr == NULL )
return NULL;
}
template<typename Type> void AVLTree<Type>::clears(Node*& p)
{
if( p == NULL )
return;
else
{
clears(p->left);
clears(p->right);
delete p;
p = NULL;
mLength--;
}
}
template<typename Type> void AVLTree<Type>::clear()
{
clears(mRoot);
}
template<typename Type> void AVLTree<Type>::erase(Type e,Node* &p)
{
if( p == NULL)
return;
if( e > p->e)
{
erase(e,p->right);
if( height(p->left) - height(p->right) == 2)
{
if( height(p->left->left) > height(p->left->right) )
rotateLeft(p);
else
rotateLeftDouble(p);
}
}
else if( e < p->e)
{
erase(e,p->left);
if( height(p->left) - height(p->right) == -2)
{
if( height(p->right->right) > height(p->right->left) )
rotateRight(p);
else
rotateRightDouble(p);
}
}
else if ( e == p->e && p->left!= NULL && p->right!= NULL)
{
Node* pmax = p->left;
while( pmax->right != NULL)
{
pmax = pmax->right;
}
p->e = pmax->e;
erase(p->e,p->left);
}
else //最终的删除会在这里执行
{
Node* pNew = p->left==NULL ? p->right : p->left;
delete p;
p = pNew;
mLength--;
}
if ( p!=NULL)
p->h = max( height(p->left),height(p->right)) + 1;
}
#endif
//main.cpp
#include <iostream>
#include "AVLTree.h"
using namespace std;
void main()
{
int Arr[9] = {6,2,8,4,10,0,12,16,14};
AVLTree<int> Tr(Arr,9);
Tr.traverse(Tr.getRoot());
Tr.traverseByLevel(Tr.getRoot());