AVL树的c++实现

参照网上别人的代码再加上自己对AVL树的理解,用C++对其实现。

#include <iostream>

#include <queue>
#ifndef AVLTREE_H
#define AVLTREE_H
using namespace std;


template<typename elementtype>
class avlTree_node{
public:
elementtype data;
int height;
avlTree_node* left;
avlTree_node* right;
avlTree_node(const elementtype &dat) :data(dat), height(0), left(nullptr), right(nullptr){}
avlTree_node(const elementtype &dat, avlTree_node *lptr, avlTree_node *rptr) :data(dat), height(0), left(lptr), right(rptr){}
};


template<typename elementtype>
class avlTree{
public:
avlTree() :tree_root(nullptr){}
~avlTree();
int tree_height();
int max_num(int a, int b);
void inOder(ostream &out)const;
void level_by_level(ostream &out)const;
void insert(const elementtype &item);
void remove(const elementtype &item);
bool seartch(const elementtype &item)const;
avlTree_node<elementtype>* findmaxnode(avlTree_node<elementtype> *ptr);
avlTree_node<elementtype>* findminnode(avlTree_node<elementtype> *ptr);
void destroy_tree();

private:
int tree_height_aux(avlTree_node<elementtype>* subtree);
void inOder_aux(ostream &out,avlTree_node<elementtype>* subtree)const;
avlTree_node<elementtype>* leftRotation(avlTree_node<elementtype>* subtree_root);
avlTree_node<elementtype>* rightRotation(avlTree_node<elementtype>* subtree_root);
avlTree_node<elementtype>* left_rightRotation(avlTree_node<elementtype>* subtree_root);
avlTree_node<elementtype>* right_leftRotation(avlTree_node<elementtype>* subtree_root);
avlTree_node<elementtype>* remove_aux(avlTree_node<elementtype>* &subtree_root, avlTree_node<elementtype>* dptr);
avlTree_node<elementtype>* insert_aux(const elementtype &item, avlTree_node<elementtype>* &subtree_root);
avlTree_node<elementtype>* seartch_aux(avlTree_node<elementtype>* x, const elementtype &item)const;
avlTree_node<elementtype>* tree_root;
void destroy_tree_aux(avlTree_node<elementtype> *subtree_root);
};


template<typename elementtype>
avlTree<elementtype>::~avlTree()
{
destroy_tree();
}
template<typename elementtype>
void avlTree<elementtype>::destroy_tree()
{
destroy_tree_aux(tree_root);
}
template<typename elementtype>
void avlTree<elementtype>::destroy_tree_aux(avlTree_node<elementtype> *subtree_root)
{
if (subtree_root == nullptr)
return;
if (subtree_root->left != nullptr)
destroy_tree_aux(subtree_root->left);
else if (subtree_root->right != nullptr)
destroy_tree_aux(subtree_root->right);
delete subtree_root;
}


template<typename elementtype>
int avlTree<elementtype>::tree_height_aux(avlTree_node<elementtype> *subtree)
{
if (subtree != nullptr)
{
return subtree->height;
}
return 0;
}


template<typename elementtype>
avlTree_node<elementtype>* avlTree<elementtype>::leftRotation(avlTree_node<elementtype>* subtree_root)
{
avlTree_node<elementtype>* new_root=subtree_root->right;
subtree_root->right = new_root->left;
new_root->left = subtree_root;
subtree_root->height = max_num(tree_height_aux(subtree_root->left), tree_height_aux(subtree_root->right)) + 1;
new_root->height = max_num(tree_height_aux(subtree_root),tree_height_aux(new_root->right)) + 1;
return new_root;
}


template<typename elementtype>
avlTree_node<elementtype>* avlTree<elementtype>::rightRotation(avlTree_node<elementtype>* subtree_root)
{
avlTree_node<elementtype>* new_root = subtree_root->left;
subtree_root->left = new_root->right;
new_root->right = subtree_root;
//cout << "交换指针完毕\n";
subtree_root->height = max_num(tree_height_aux(subtree_root->left), tree_height_aux(subtree_root->right)) + 1;
new_root->height = max_num(tree_height_aux(subtree_root), tree_height_aux(new_root->left)) + 1;
//cout << "更新高度完毕\n";
return new_root;
}


template<typename elementtype>
avlTree_node<elementtype>*  avlTree<elementtype>::left_rightRotation(avlTree_node<elementtype>* subtree_root)
{
subtree_root->left = leftRotation(subtree_root->left);
return rightRotation(subtree_root);
}


template<typename elementtype>
avlTree_node<elementtype>* avlTree<elementtype>::right_leftRotation(avlTree_node<elementtype>* subtree_root)
{
subtree_root->right = rightRotation(subtree_root->right);
return leftRotation(subtree_root);
}


template<typename elementtype>
int avlTree<elementtype>::max_num(int a, int b)
{
return (a > b) ? a : b;
}


template<typename elementtype>
int avlTree<elementtype>::tree_height()
{
return tree_height_aux(tree_root);
}


template<typename elementtype>
avlTree_node<elementtype>* avlTree<elementtype>::insert_aux(const elementtype &item, avlTree_node<elementtype>* &subtree_root)
{
if (subtree_root == nullptr)
{
subtree_root = new avlTree_node<elementtype>(item);
if (subtree_root == nullptr)
{
cerr << "创建AVL树失败!\n";
return nullptr;
}
}
else if (item < subtree_root->data)
{
//cout << "tree root data:" << tree_root->data;
subtree_root->left = insert_aux(item, subtree_root->left);
//cout << "subtree root data:" << subtree_root->data << endl;
if (tree_height_aux(subtree_root->left)-tree_height_aux(subtree_root->right)== 2)//往左边插入使得左子树的高度增加,有可能出现不平衡
{
if (item < subtree_root->left->data)//当插入的项位于+2不平衡节点的左孩子的左子树时,执行简单右旋操作
{
cout << "rightRotation start\n";
subtree_root = rightRotation(subtree_root);
cout << "rightRoatation finish\n";
}
else//当插入项位于+2不平衡节点的右孩子的右子树时,执行左右旋操作
{
subtree_root = left_rightRotation(subtree_root);
cout << "left_rightRotation\n";
}
}
}
else if (item>subtree_root->data)
{
subtree_root->right = insert_aux(item, subtree_root->right);
if (tree_height_aux(subtree_root->right) - tree_height_aux(subtree_root->left) == 2)
{
if (item > subtree_root->right->data)
{
subtree_root = leftRotation(subtree_root);
cout << "leftRotation\n";
}
else
{
subtree_root = right_leftRotation(subtree_root);
cout << "right_leftRotation";
}
}
}
else
cerr << "您要添加的项已经在AVL树中了!\n";
subtree_root->height = max_num(tree_height_aux(subtree_root->left), tree_height_aux(subtree_root->right)) + 1;
return subtree_root;
}


template<typename elementtype>
void avlTree<elementtype>::insert(const elementtype &item)
{
insert_aux(item,tree_root);
}


/*
输入:开始查找的树的根以及查找内容
输出:若找到,则返回该项的指针;否则返回空指针。
*/
template<typename elementtype>
avlTree_node<elementtype>* avlTree<elementtype>::seartch_aux(avlTree_node<elementtype>* x, const elementtype &item)const
{
if (x == nullptr)
return x;
if (item < x->data)
return seartch_aux(x->left, item);
else if (item>x->data)
return seartch_aux(x->right, item);
else
return x;
}


template<typename elementtype>
bool avlTree<elementtype>::seartch(const elementtype &item)const
{
if (seartch_aux(tree_root, item) == nullptr)
return false;
return true;
}


template<typename elementtype>
void avlTree<elementtype>::inOder_aux(ostream &out, avlTree_node<elementtype>* subtree_root)const
{
if (subtree_root == nullptr)
return;
inOder_aux(out, subtree_root->left);
out << subtree_root->data << " ";
inOder_aux(out, subtree_root->right);
}


template<typename elementtype>
void avlTree<elementtype>::inOder(ostream &out)const
{
inOder_aux(out, tree_root);
}


template<typename elementtype>
void avlTree<elementtype>::level_by_level(ostream &out)const
{
if (tree_root == nullptr)
{
cout << "空树\n";
return;
}
queue<avlTree_node<elementtype>*> tmpptr_store;
avlTree_node<elementtype> *ptr;
tmpptr_store.push(tree_root);
while (!tmpptr_store.empty())
{
ptr = tmpptr_store.front();
tmpptr_store.pop();
out << ptr->data<<"高:"<<ptr->height<<" ";
if (ptr->left != nullptr)
tmpptr_store.push(ptr->left);
if (ptr->right != nullptr)
tmpptr_store.push(ptr->right);

}
}


/*
input:a pointer whih is poiter to left subtree
output:return a poiter to the largest node
*/
template<typename elementtype>
avlTree_node<elementtype>* avlTree<elementtype>::findmaxnode(avlTree_node<elementtype> *ptr)
{
while (ptr->right != nullptr)
ptr = ptr->right;
return ptr;
}


/*
input:a poiter to the right subtree
output:return a poiter to the smalllest node 
*/
template<typename elementtype>
avlTree_node<elementtype>* avlTree<elementtype>::findminnode(avlTree_node<elementtype> *ptr)
{
while (ptr ->left!= nullptr)
ptr = ptr->left;
return ptr;
}


template<typename elementtype>
avlTree_node<elementtype>* avlTree<elementtype>::remove_aux(avlTree_node<elementtype>* &subtree_root, avlTree_node<elementtype>* dptr)
{
if (subtree_root == nullptr || dptr == nullptr)
return nullptr;
else if (dptr->data< subtree_root->data)
{
cout << "turn left \n";
subtree_root->left = remove_aux(subtree_root->left, dptr);
if (tree_height_aux(subtree_root->right) - tree_height_aux(subtree_root->left) == 2)
{
avlTree_node<elementtype> *r = subtree_root->right;
if (tree_height_aux(r->left)>tree_height_aux(r->right))
{
cout << "right_leftRotation start\n";
subtree_root = right_leftRotation(subtree_root);
cout << "right_leftRotation finish\n";
}
else
{
cout << "leftRotation start\n";
subtree_root = leftRotation(subtree_root);
cout << "leftRotation finish\n";
}
}
//不需要旋转的时候要更新该节点的高度,因为删除节点!
else
subtree_root->height = max_num(tree_height_aux(subtree_root->left), tree_height_aux(subtree_root->right)) + 1;


}
else if (dptr->data > subtree_root->data)
{
cout << "turn right\n";
subtree_root->right = remove_aux(subtree_root->right, dptr);
if (tree_height_aux(subtree_root->left) - tree_height_aux(subtree_root->right) == 2)
{
avlTree_node<elementtype> *l = subtree_root->left;
if (tree_height_aux(l->right) > tree_height_aux(l->left))
{
cout << "left_rightRotation start\n";
subtree_root = left_rightRotation(subtree_root);
cout << "left_rightRotation finish\n";
}
else
{
cout << "rightRotation start \n";
subtree_root = rightRotation(subtree_root);
cout << "rightRotation finish \n";
}
}
else
subtree_root->height = max_num(tree_height_aux(subtree_root->left), tree_height_aux(subtree_root->right)) + 1;


}
else
{
cout << "delete it\n";
if (subtree_root->left&&subtree_root->right)
{
cout << "删除非叶子节点\n";
if (tree_height_aux(subtree_root->left) > tree_height_aux(subtree_root->right))
{
cout << "开始寻找左子树最大节点\n";
avlTree_node<elementtype> *max_ptr = findmaxnode(subtree_root->left);
cout << "寻找完毕\n";
subtree_root->data = max_ptr->data;
subtree_root->left=remove_aux(subtree_root->left, max_ptr);
}
else
{
cout << "开始寻找"<<subtree_root->data<<"右子树最小节点\n";
avlTree_node<elementtype> *min_ptr; 
min_ptr=findminnode(subtree_root->right);
//cout << subtree_root->right->data << endl;
cout << "寻找完毕,最小值为:";
cout<<min_ptr->data << endl;
subtree_root->data = min_ptr->data;
subtree_root->right = remove_aux(subtree_root->right, min_ptr);
}
}
else
{
avlTree_node<elementtype> *tmp = subtree_root;
subtree_root = (subtree_root->left!= nullptr) ? subtree_root->left : subtree_root->right;
delete tmp;


}


}
return subtree_root;
}


template<typename elementtype>
void avlTree<elementtype>::remove(const elementtype &item)
{
avlTree_node<elementtype> *x;
x = seartch_aux(tree_root, item);
if (x != nullptr)
remove_aux(tree_root,x);
else
cout << "该项不再AVL树中\n";
}




#endif
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值