?????????AVL?????C++???
???????????? ???????[ 2016/9/29 14:17:03 ] ??????????????????? C++ ??
????????AVL?????????ο??? ?????????——AVL?????
??????????????????????????????????????ο????????????——????????????C++???
????AVL-tree?????"????????????????"???????????????????????????????????????????????O(logN)??????κν??????????????????1??
??????AVL???????????:
????struct AvlNode{
????Comparable element;
????AvlNode * left;
????AvlNode * right;
????int height;
????AvlNode(const Comparable & e?? AvlNode * lt?? AvlNode * rt?? int h = 0):element(e)?? left(lt)?? right(rt)?? height(h){}
????};
?????y????????????????????
??????AVL????????????????
????void makeEmpty();//??????
????bool isEmpty() const;//?ж??????????
????void lessOrderPrintTree();//??С?????????AVL?????
????void biggerOrderPrintTree();//???С?????AVL?????
????void insert(const Comparable & x);//??????x????
????Comparable findMin() const;//???С?
????Comparable findMax() const;//??????
???????????????????
????/****************************************************************
????* ?????????void insert(const Comparable & x?? AvlNode * t)
????* ????????: ????t??????????x????
????* ?????б?: x??????????
????* t????????
????* ????????void
????*****************************************************************/
????template<typename Comparable>
????void AvlTree<Comparable>::insert(const Comparable & x?? AvlNode * & t)
????{
????if(t == NULL)//?????????
????t = new AvlNode(x?? NULL?? NULL);
????else if(x < t->element){
????insert(x?? t->left);
????if(height(t->left) - height(t->right) == 2){
????if(x < t->left->element)//??????????????
????rotateWithLeftChild(t);
????else
????doubleWithLeftChild(t);//?????????????
????}
????}
????else if(x > t->element){
????insert(x?? t->right);
????if(height(t->right) - height(t->left) == 2){
????if(x > t->right->element)//??????????????
????rotateWithRightChild(t);
????else
????doubleWithRightChild(t);//?????????????
????}
????}
????//???x??????????????????????????????????????????????????????????num?????????
????t->height = max(height(t->left)?? height(t->right)) + 1;//??????t????
????}
????/****************************************************************
????* ?????????rotateWithLeftChild(AvlNode *t)
????* ????????: ??????????е?????????????????????
????* ?????б?: t?????????????
????* ??????????
????*****************************************************************/
????template<typename Comparable>
????void AvlTree<Comparable>::rotateWithLeftChild(AvlNode * & k2)
????{
????cout << "?????" << endl;
????AvlNode * 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)?? k2->height) + 1;
????k2 = k1;
????}
????/****************************************************************
????* ?????????rotateWithRightChild(AvlNode *t)
????* ????????: ??????????е?????????????????????
????* ?????б?: t?????????????
????* ??????????
????*****************************************************************/
????template<typename Comparable>
????void AvlTree<Comparable>::rotateWithRightChild(AvlNode * & k1)
????{
????cout << "??????" << endl;
????AvlNode * 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->right)?? k1->height) + 1;
????k1 = k2;
????}
????/****************************************************************
????* ?????????doubleWithLeftChild(AvlNode *t)
????* ????????: ????????????????????????????????
????* ?????б?: t?????????????
????* ??????????
????*****************************************************************/
????template<typename Comparable>
????void AvlTree<Comparable>::doubleWithLeftChild(AvlNode * & k3)
????{
????cout << "**********************" << endl;
????cout << "??????: " << endl;
????rotateWithRightChild(k3->left);
????rotateWithLeftChild(k3);
????cout << "**********************" << endl;
????}
????/****************************************************************
????* ?????????doubleWithRightChild(AvlNode *t)
????* ????????: ????????????????????????????????
????* ?????б?: t?????????????
????* ??????????
????*****************************************************************/
????template<typename Comparable>
????void AvlTree<Comparable>::doubleWithRightChild(AvlNode * & k1)
????{
????cout << "**********************" << endl;
????cout << "??????: " << endl;
????rotateWithLeftChild(k1->right);
????rotateWithRightChild(k1);
????cout << "**********************" << endl;
????}
????????????????????????
?????????????AVL?????C++???template<typename Comparable> void AvlTree<Comparable>::rotateWithRightChild(AvlNode * & k1) { cout << "??????" << endl; AvlNode * 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->right)?? k1->height) + 1; k1 = k2; }????????????????
????????????????????????
????template<typename Comparable> void AvlTree<Comparable>::doubleWithRightChild(AvlNode * & k1) { cout << "**********************" << endl; cout << "??????: " << endl; rotateWithLeftChild(k1->right); rotateWithRightChild(k1); cout << "**********************" << endl; }
?????ú????е????????????ú????????????
???????????????????????
?????????????в?????: 1?? 2?? 3?? 4?? 5?? 6?? 7?? 16?? 15??
??????????????????????????????????ó??????????????main??????tree2????????????????????AVL????????????????????????????????
????AvlTree<int> tree2; cout << "????AVL??trre2: " << endl; for(int i = 1; i < 8; ++i) tree2.insert(i); tree2.insert(16); tree2.insert(15); tree2.lessOrderPrintTree(); tree2.biggerOrderPrintTree();??????
????????AVL??trre2:
??????????
??????????
??????????
??????????
????**********************
??????????:
?????????
??????????
????**********************
??????С?????????1 2 3 4 5 6 7 15 16
???????С?????16 15 7 6 5 4 3 2 1
???????????AVL??????????
????/*************************************************************************
????> File Name: AvlTree.cpp
????> Author:
????> Mail:
????> Created Time: 2016??04??08?? ?????? 10?14??48??
????************************************************************************/
????#include <iostream>
????#include <algorithm>
????#include <vector>
????using namespace std;
????template<typename Comparable>
????class AvlTree{
????public:
????AvlTree(){ root = NULL; }
????~AvlTree();
????void makeEmpty();//??????
????bool isEmpty() const;//?ж??????????
????void lessOrderPrintTree();//??С?????????AVL?????
????void biggerOrderPrintTree();//???С?????AVL?????
????void insert(const Comparable & x);//??????x????
????Comparable findMin() const;//???С?
????Comparable findMax() const;//??????
????private:
????struct AvlNode{
????Comparable element;
????AvlNode * left;
????AvlNode * right;
????int height;
????AvlNode(const Comparable & e?? AvlNode * lt?? AvlNode * rt?? int h = 0):element(e)?? left(lt)?? right(rt)?? height(h){}
????};
????AvlNode * root;
????private:
????void makeEmpty(AvlNode * t);
????void lessOrderPrintTree(AvlNode * t);
????void biggerOrderPrintTree(AvlNode * t);
????int height(AvlNode * t) const;//????????t????
????void insert(const Comparable & x?? AvlNode * & t);//??t??????????x????
????void rotateWithLeftChild(AvlNode * & k2);//??????????????????
????void rotateWithRightChild(AvlNode * & k1);//??????????????????
????void doubleWithLeftChild(AvlNode * & k3);//?????????????????
????void doubleWithRightChild(AvlNode * & k1);//?????????????????
????Comparable findMin(AvlNode * t) const;//???С?
????Comparable findMax(AvlNode * t) const;//??????
????};
??????
???·???
??????????????????
2023/3/23 14:23:39???д?ò??????????
2023/3/22 16:17:39????????????????????Щ??
2022/6/14 16:14:27??????????????????????????
2021/10/18 15:37:44???????????????
2021/9/17 15:19:29???·???????·
2021/9/14 15:42:25?????????????
2021/5/28 17:25:47??????APP??????????
2021/5/8 17:01:11