AVL tree Node Declaration:
1 struct AvlNode2 {3 comparable element;4 avlnode *left;5 avlnode *right;6 int height;7 8 Avlnode (const comparable & Theelement,avlnode *lt,avlnode *rt,int h=0): Element (Theelement), left (LT), right (RT), Height (t) 9};
COMPUTE node Height:
1 int height (Avlnode * t) const2 {3 return t = = NULL? -1:t->height;4}
Insert an action into the AVL:
void 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->element) Rotatewithleftchild (t); else doublewithleftchild (t); } else if (T->element < x) { insert (x,t->right); if (height (t->right)-height (t->left) = = 2) if (T->right->element < x) Rotatewithleftchild (t ); else doublewithleftchild (t); } else ; T->height = max (height (t->left), height (t->right)) +1;}
To perform a single rotation process:
1 void Rotatewithleftchild (Avlnode * & K2) 2 {3 avlnode *k1 = k2->left;4 k2->left = k1->right;5 k 1->right = k2;6 k2->height = max (height (k2->left), height (k2->right)) +1;7 k1->height = max ( Height (k1->left), height (k1->right)) +1;8 k2=k1;9}
To perform a double-rotation process:
void Doublewithleftchild (Avlnode * & K3) { rotatewithleftchild (k3->left); Rotatewithleftchild (K3);}
20120920-avl tree Definition "Data structure and algorithm analysis"