1 Definition of AVL tree
The AVL tree is a self-balancing binary sort tree, which is characterized by the Zuozi height of any one node and the height difference of the right subtree between -1,0,1 and three. Any sub-tree of the AVL tree is an AVL tree.
2 Implementation of AVL tree
The AVL tree essence is a binary sort tree, so the binary sort tree of any nature of the AVL tree has, but the AVL tree is slightly more complex is the AVL tree must meet the balance conditions, specific to the BST different places mainly embodied in the insertion, deletion operations.
Insert operation: You may have an imbalance after inserting, so it is time to rotate the tree to achieve balance. There are four types of rotation, left, left, right, left, right, right, and so on. The left and right rotation are mirrored, and left and right rotation are mirrored, so essentially two types of rotation. For left-to-left rotation, you only need to rotate once, for the left and right rotation, you need to perform two rotations. See:
In this case, the recursive method is used to implement the insert and delete operations. The convenient point of using recursion is that if the parameter of the function is a reference type, when we pass in a p->left, when we recursively return to the lower level of the current function, the assignment to P is actually the operation of the p->left in the upper recursion, so we do not need to pass the parent pointer.
3 Implementation Code
AVLTree.h
#ifndef ddxx_avltree_h#define ddxx_avltree_h#include <iostream> #include <queue>using namespace std; Template<typename Type>class avltree{struct node{type e; Node* left; node* Right;int H; Node (Type _e): E (_e), left (null), Right (NULL), h (0) {}node (type _e,node* _left,node* _right,int _h): E (E), left (_left), Right (_right), H (_h) {}};p Ublic:avltree (); Avltree (Type arr[],int nlength);/*avltree (const avltree& right); avltree& operator= (const avltree& right), */~avltree ();p ublic:boolinsert (type e,node* &p); Voiderase (type e,node* &p); Node*&find (Type e) const;voidtraverse (node* p) const;voidtraversebylevel (node* P) const;intgetlength () {return Mlength;} Node*&getparent (node* p); Node*&getroot () {return mroot;}//notice the Return Typeboolempty () {return mroot==null;}; Voidclear (); Voidclears (node* &p);p rivate:voidrotateleft (node* &k2); Voidrotateright (Node* &k2); Voidrotateleftdouble (node* &p); voidrotaterightdouble (node* &p); Intheight (node* p) const{retUrn p = = NULL? -1:p->h;} Intmax (int x,int y) {return x>y?x:y;} private:node* mroot;int mlength;}; Template<typename type> Avltree<type>::avltree (): Mroot (NULL), Mlength (0) {}template<typename Type > Avltree<type>::avltree (Type arr[],int nlength): Mroot (NULL), Mlength (0) {for (int i=0;i<nlength;i++) { Insert (Arr[i],mroot);}} Template<typename type> Avltree<type>::~avltree () {clears (mroot);} Template<typename type> bool Avltree<type>::insert (Type e,node* &p) {if (p== NULL) {p = new Node (e); mlength++;} else if (E < p->e) {Insert (E,p->left), if (height (p->left)-height (p->right) = = 2) {if (E < P->left-> ; e) Rotateleft (p); elserotateleftdouble (P);}} else if (E > P->e) {Insert (e,p->right), if (height (p->left)-height (p->right) = =-2) {if (E > p->right- >e) Rotateright (p); elserotaterightdouble (P);}} else//E ia already exist{//return false;} P->h = max (height (p->left), height (p->right)) +1;return true;} Re+Plate<typename type> void Avltree<type>::rotateleft (node*& K2) {node* K1 = K2->left;k2->left = k1- >right;k1->right = K2;k1->h = max (height (k1->left), height (k1->right)) + 1;k2->h = max (height (k2-> left), height (k2->right)) + 1;K2 = k1;//Join the original node}template<typename type> void Avltree<type>: : Rotateright (node* &k2) {node* K1 = K2->right;k2->right = K1->left;k1->left = K2;k1->h = max (height (k1 ->left), height (k1->right)) + 1;k2->h = max (height (k2->left), height (k2->right)) + 1;//K1=K2, Because a reference to P->left or p->right is passed in the Insert function, the root node can be assigned to its parent node's child nodes 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 Midtraverse (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:" <
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 ()); Tr.erase (14,tr.getroot ()); Tr.traverse (Tr.getroot ()); Tr.traversebylevel (Tr.getroot ());cout<< "Tree ' s length is:" <<tr.getlength () <<endl; Tr.clear ();cout<< "Tree ' s length is:" <<tr.getlength () <<endl;}
4 test Results
AVL tree implemented by C + + templates