AVL树之c++实现 发表于 2018-08-18 | 更新于 2021-04-26 | 分类于 算法篇 | 评论数: | 阅读次数: 本文字数: 16k | 阅读时长 ≈ 15 分钟 个人笔记,如有描述不当,欢迎留言指出~ 头文件AVLTree.h123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103#ifndef _AVL_TREE_HPP_#define _AVL_TREE_HPP_template <class T>class AVLTreeNode{ public: T key; // 关键字(键值) int height; // 高度 AVLTreeNode *left; // 左孩子 AVLTreeNode *right; // 右孩子 AVLTreeNode(T value, AVLTreeNode *l, AVLTreeNode *r): key(value), height(0),left(l),right(r){}};template <class T>class AVLTree { private: AVLTreeNode<T> *mRoot; // 根结点 public: AVLTree(); ~AVLTree(); // 获取树的高度 int height(); // 获取树的高度 int max(int a, int b); // 前序遍历"AVL树" void preOrder(); // 中序遍历"AVL树" void inOrder(); // 后序遍历"AVL树" void postOrder(); // (递归实现)查找"AVL树"中键值为key的节点 AVLTreeNode<T>* search(T key); // (非递归实现)查找"AVL树"中键值为key的节点 AVLTreeNode<T>* iterativeSearch(T key); // 查找最小结点:返回最小结点的键值。 T minimum(); // 查找最大结点:返回最大结点的键值。 T maximum(); // 将结点(key为节点键值)插入到AVL树中 void insert(T key); // 删除结点(key为节点键值) void remove(T key); // 销毁AVL树 void destroy(); // 打印AVL树 void print(); private: // 获取树的高度 int height(AVLTreeNode<T>* tree) ; // 前序遍历"AVL树" void preOrder(AVLTreeNode<T>* tree) const; // 中序遍历"AVL树" void inOrder(AVLTreeNode<T>* tree) const; // 后序遍历"AVL树" void postOrder(AVLTreeNode<T>* tree) const; // (递归实现)查找"AVL树x"中键值为key的节点 AVLTreeNode<T>* search(AVLTreeNode<T>* x, T key) const; // (非递归实现)查找"AVL树x"中键值为key的节点 AVLTreeNode<T>* iterativeSearch(AVLTreeNode<T>* x, T key) const; // 查找最小结点:返回tree为根结点的AVL树的最小结点。 AVLTreeNode<T>* minimum(AVLTreeNode<T>* tree); // 查找最大结点:返回tree为根结点的AVL树的最大结点。 AVLTreeNode<T>* maximum(AVLTreeNode<T>* tree); // LL:左左对应的情况(左单旋转)。 AVLTreeNode<T>* leftLeftRotation(AVLTreeNode<T>* k2); // RR:右右对应的情况(右单旋转)。 AVLTreeNode<T>* rightRightRotation(AVLTreeNode<T>* k1); // LR:左右对应的情况(左双旋转)。 AVLTreeNode<T>* leftRightRotation(AVLTreeNode<T>* k3); // RL:右左对应的情况(右双旋转)。 AVLTreeNode<T>* rightLeftRotation(AVLTreeNode<T>* k1); // 将结点(z)插入到AVL树(tree)中 AVLTreeNode<T>* insert(AVLTreeNode<T>* &tree, T key); // 删除AVL树(tree)中的结点(z),并返回被删除的结点 AVLTreeNode<T>* remove(AVLTreeNode<T>* &tree, AVLTreeNode<T>* z); // 销毁AVL树 void destroy(AVLTreeNode<T>* &tree); // 打印AVL树 void print(AVLTreeNode<T>* tree, T key, int direction);};#endif 源文件 AVLTree.cpp123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464#include "AVLTree.h"#include <iomanip>#include <iostream>using namespace std;/* * 构造函数 */template <class T>AVLTree<T>::AVLTree():mRoot(NULL){}/* * 析构函数 */template <class T>AVLTree<T>::~AVLTree() { destroy(mRoot);}/* * 获取树的高度 */template <class T>int AVLTree<T>::height(AVLTreeNode<T>* tree) { if (tree != NULL) return tree->height; return 0;}template <class T>int AVLTree<T>::height() { return height(mRoot);}/* * 比较两个值的大小 */template <class T>int AVLTree<T>::max(int a, int b) { return a>b ? a : b;}/* * 前序遍历"AVL树" */template <class T>void AVLTree<T>::preOrder(AVLTreeNode<T>* tree) const{ if(tree != NULL) { cout<< tree->key << " " ; preOrder(tree->left); preOrder(tree->right); }}template <class T>void AVLTree<T>::preOrder() { preOrder(mRoot);}/* * 中序遍历"AVL树" */template <class T>void AVLTree<T>::inOrder(AVLTreeNode<T>* tree) const{ if(tree != NULL) { inOrder(tree->left); cout<< tree->key << " " ; inOrder(tree->right); }}template <class T>void AVLTree<T>::inOrder() { inOrder(mRoot);}/* * 后序遍历"AVL树" */template <class T>void AVLTree<T>::postOrder(AVLTreeNode<T>* tree) const{ if(tree != NULL) { postOrder(tree->left); postOrder(tree->right); cout<< tree->key << " " ; }}template <class T>void AVLTree<T>::postOrder() { postOrder(mRoot);}/* * (递归实现)查找"AVL树x"中键值为key的节点 */template <class T>AVLTreeNode<T>* AVLTree<T>::search(AVLTreeNode<T>* x, T key) const{ if (x==NULL || x->key==key) return x; if (key < x->key) return search(x->left, key); else return search(x->right, key);}template <class T>AVLTreeNode<T>* AVLTree<T>::search(T key) { return search(mRoot, key);}/* * (非递归实现)查找"AVL树x"中键值为key的节点 */template <class T>AVLTreeNode<T>* AVLTree<T>::iterativeSearch(AVLTreeNode<T>* x, T key) const{ while ((x!=NULL) && (x->key!=key)) { if (key < x->key) x = x->left; else x = x->right; } return x;}template <class T>AVLTreeNode<T>* AVLTree<T>::iterativeSearch(T key){ return iterativeSearch(mRoot, key);}/* * 查找最小结点:返回tree为根结点的AVL树的最小结点。 */template <class T>AVLTreeNode<T>* AVLTree<T>::minimum(AVLTreeNode<T>* tree){ if (tree == NULL) return NULL; while(tree->left != NULL) tree = tree->left; return tree;}template <class T>T AVLTree<T>::minimum(){ AVLTreeNode<T> *p = minimum(mRoot); if (p != NULL) return p->key; return (T)NULL;} /* * 查找最大结点:返回tree为根结点的AVL树的最大结点。 */template <class T>AVLTreeNode<T>* AVLTree<T>::maximum(AVLTreeNode<T>* tree){ if (tree == NULL) return NULL; while(tree->right != NULL) tree = tree->right; return tree;}template <class T>T AVLTree<T>::maximum(){ AVLTreeNode<T> *p = maximum(mRoot); if (p != NULL) return p->key; return (T)NULL;}/* * LL:左左对应的情况(左单旋转)。 * * 返回值:旋转后的根节点 */template <class T>AVLTreeNode<T>* AVLTree<T>::leftLeftRotation(AVLTreeNode<T>* k2){ AVLTreeNode<T>* k1=k2->left; k2->left=k1->right; k1->right=k2; k2->height=max(height(k2->left),height(k2->right))+1; k1->height=max(height(k1->left),height(k1->right))+1; return k1;}/* * RR:右右对应的情况(右单旋转)。 * * 返回值:旋转后的根节点 */template <class T>AVLTreeNode<T>* AVLTree<T>::rightRightRotation(AVLTreeNode<T>* k1){ AVLTreeNode<T>* k2=k1->right; k1->right=k2->left; k2->left=k1; k1->height=max(height(k1->left),height(k1->right))+1; k2->height=max(height(k2->left),height(k2->right))+1; return k2;}/* * LR:左右对应的情况(左双旋转)。 * * 返回值:旋转后的根节点 */template <class T>AVLTreeNode<T>* AVLTree<T>::leftRightRotation(AVLTreeNode<T>* k3){ k3->left=rightRightRotation(k3->left); return leftLeftRotation(k3);}/* * RL:右左对应的情况(右双旋转)。 * * 返回值:旋转后的根节点 */template <class T>AVLTreeNode<T>* AVLTree<T>::rightLeftRotation(AVLTreeNode<T>* k1){ k1->right = leftLeftRotation(k1->right); return rightRightRotation(k1);}/* * 将结点插入到AVL树中,并返回根节点 * * 参数说明: * tree AVL树的根结点 * key 插入的结点的键值 * 返回值: * 根节点 */template <class T>AVLTreeNode<T>* AVLTree<T>::insert(AVLTreeNode<T>* &tree, T key){ if (tree == NULL) { // 新建节点 tree = new AVLTreeNode<T>(key, NULL, NULL); if (tree==NULL) { cout << "ERROR: create avltree node failed!" << endl; return NULL; } } else if (key < tree->key) // 应该将key插入到"tree的左子树"的情况 { tree->left = insert(tree->left, key); // 插入节点后,若AVL树失去平衡,则进行相应的调节。 if (height(tree->left) - height(tree->right) == 2) { if (key < tree->left->key) tree = leftLeftRotation(tree); else tree = leftRightRotation(tree); } } else if (key > tree->key) // 应该将key插入到"tree的右子树"的情况 { tree->right = insert(tree->right, key); // 插入节点后,若AVL树失去平衡,则进行相应的调节。 if (height(tree->right) - height(tree->left) == 2) { if (key > tree->right->key) tree = rightRightRotation(tree); else tree = rightLeftRotation(tree); } } else //key == tree->key) { cout << "添加失败:不允许添加相同的节点!" << endl; } tree->height = max( height(tree->left), height(tree->right)) + 1; return tree;}template <class T>void AVLTree<T>::insert(T key){ insert(mRoot, key);}/* * 删除结点(z),返回根节点 * * 参数说明: * tree AVL树的根结点 * z 待删除的结点 * 返回值: * 根节点 */template <class T>AVLTreeNode<T>* AVLTree<T>::remove(AVLTreeNode<T>* &tree, AVLTreeNode<T>* z){ // 根为空 或者 没有要删除的节点,直接返回NULL。 if (tree==NULL || z==NULL) return NULL; if (z->key < tree->key) // 待删除的节点在"tree的左子树"中 { tree->left = remove(tree->left, z); // 删除节点后,若AVL树失去平衡,则进行相应的调节。 if (height(tree->right) - height(tree->left) == 2) { AVLTreeNode<T> *r = tree->right; if (height(r->left) > height(r->right)) tree = rightLeftRotation(tree); else tree = rightRightRotation(tree); } } else if (z->key > tree->key)// 待删除的节点在"tree的右子树"中 { tree->right = remove(tree->right, z); // 删除节点后,若AVL树失去平衡,则进行相应的调节。 if (height(tree->left) - height(tree->right) == 2) { AVLTreeNode<T> *l = tree->left; if (height(l->right) > height(l->left)) tree = leftRightRotation(tree); else tree = leftLeftRotation(tree); } } else // tree是对应要删除的节点。 { // tree的左右孩子都非空 if ((tree->left!=NULL) && (tree->right!=NULL)) { if (height(tree->left) > height(tree->right)) { // 如果tree的左子树比右子树高; // 则(01)找出tree的左子树中的最大节点 // (02)将该最大节点的值赋值给tree。 // (03)删除该最大节点。 // 这类似于用"tree的左子树中最大节点"做"tree"的替身; // 采用这种方式的好处是:删除"tree的左子树中最大节点"之后,AVL树仍然是平衡的。 AVLTreeNode<T>* max = maximum(tree->left); tree->key = max->key; tree->left = remove(tree->left, max); } else { // 如果tree的左子树不比右子树高(即它们相等,或右子树比左子树高1) // 则(01)找出tree的右子树中的最小节点 // (02)将该最小节点的值赋值给tree。 // (03)删除该最小节点。 // 这类似于用"tree的右子树中最小节点"做"tree"的替身; // 采用这种方式的好处是:删除"tree的右子树中最小节点"之后,AVL树仍然是平衡的。 AVLTreeNode<T>* min = maximum(tree->right); tree->key = min->key; tree->right = remove(tree->right, min); } } else { AVLTreeNode<T>* tmp = tree; tree = (tree->left!=NULL) ? tree->left : tree->right; delete tmp; } } return tree;}template <class T>void AVLTree<T>::remove(T key){ AVLTreeNode<T>* z; if ((z = search(mRoot, key)) != NULL) mRoot = remove(mRoot, z);}/* * 销毁AVL树 */template <class T>void AVLTree<T>::destroy(AVLTreeNode<T>* &tree){ if (tree==NULL) return ; if (tree->left != NULL) destroy(tree->left); if (tree->right != NULL) destroy(tree->right); delete tree;}template <class T>void AVLTree<T>::destroy(){ destroy(mRoot);}/* * 打印"二叉查找树" * *key -- 节点的键值 *direction -- 0,表示该节点是根节点; * -1,表示该节点是它的父结点的左孩子; * 1,表示该节点是它的父结点的右孩子。 */template <class T>void AVLTree<T>::print(AVLTreeNode<T>* tree, T key, int direction){ if(tree != NULL) { if(direction==0) // tree是根节点 cout << setw(2) << tree->key << " is root" <<", height is "<<tree->height<< endl; else // tree是分支节点 cout << setw(2) << tree->key << " is " << setw(2) << key << "'s " << setw(12) << (direction==1?"right child" : "left child") <<", height is "<<tree->height<< endl; print(tree->left, tree->key, -1); print(tree->right,tree->key, 1); }}template <class T>void AVLTree<T>::print(){ if (mRoot != NULL) print(mRoot, mRoot->key, 0);} 测试文件 test.cpp1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950#include <iostream>#include "AVLTree.cpp"using namespace std;static int arr[]= {3,2,1,4,5,6,7,16,15,14,13,12,11,10,8,9};#define TBL_SIZE(a) ( (sizeof(a)) / (sizeof(a[0])) )int main(){ int i,ilen; AVLTree<int>* tree=new AVLTree<int>(); cout << "== 依次添加: "; ilen = TBL_SIZE(arr); for(i=0; i<ilen; i++) { cout << arr[i] <<" "; tree->insert(arr[i]); } cout << "\n== 前序遍历: "; tree->preOrder(); cout << "\n== 中序遍历: "; tree->inOrder(); cout << "\n== 后序遍历: "; tree->postOrder(); cout << endl; cout << "== 高度: " << tree->height() << endl; cout << "== 最小值: " << tree->minimum() << endl; cout << "== 最大值: " << tree->maximum() << endl; cout << "== 树的详细信息: " << endl; tree->print(); i = 8; cout << "\n== 删除根节点: " << i; tree->remove(i); cout << "\n== 高度: " << tree->height() ; cout << "\n== 中序遍历: " ; tree->inOrder(); cout << "\n== 树的详细信息: " << endl; tree->print(); // 销毁二叉树 tree->destroy(); return 0;} 请博主喝咖啡 打赏 微信支付 支付宝 本文作者: cloudintheking 本文链接: https://cloudintheking.github.io/posts/942112fa.html 版权声明: 本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明出处!