
the upper bound of log2(N+1)
An AVL tree is a binary search tree in which the heights of the left and right subtrees of the root differ by at most 1, and in which the left and right subtrees are again AVL trees. With each node of an AVL tree is associated a balance factor which is left high, equal, or right high according, respectively, as to whether the left subtree has height greater than, equal to, or less than that of the right subtree.
An empty tree is height balanced.
Note that the definition does not require that all leaves be on the same or adjacent levels.


CASE 1: Right High - needs left rotation
-x is right high
-rotate node x upward to the root, dropping the root r down into the
left subtree of x
-the subtree T2 of nodes with keys between those of r and x now
becomes the right subtree of r rather than left subtree of x.
-actually rotate values in 3 pointer variables
-total height is increased by 1.

CASE 2: Left High - needs double rotation
-right subtree is 2 high but the balancefactor of x is left high
-it's necessary to move 2 levels, to the node w that roots the left
subtree of x, to find the new root
-called double rotation
-first rotate subtree with root x to the right so 2 becomes its
root and then rotating the tree with root r to the left (moving w up
to new root)

enum balancefactor {RH,EH,LH};
struct AVLNode{
treeItemType key;
ptrType LChildPtr;
ptrType RChildPtr;
balancefactor BF;
};AVLNode
void Insert (ptrType& root, ptrType newItem, bool& taller)
{
bool tallersubtree;
if (root == NULL){
root = newItem;
root->LChildPtr = NULL;
root->RChildPtr = NULL;
root->BF = EH;
taller = true;
}
else if (newItem->key == root->key)
error;
else if (newItem->key < root->key){
insert(root->LChildPtr, newItem, tallersubtree;
if (tallersubtree){
switch(root->BF){
case LH: LeftBalance (root); break;
case EH: root->BF = LH; taller = true; break;
case RH: root->BF = EH; taller = false; break;
}//switch
else
taller = false;
}
else{
insert(root->RChildPtr, newItem, tallersubtree);
if (tallersubtree){
switch(root->BF){
case LH: root->BF = EH; taller = false; break;
case EH: root->BF = RH; taller = true; break;
case RH: RightBalance(root);
}//switch
}
else
taller = false;
}//else
}//insert
void RightBalance (ptrType& root)
{
ptrType x, w;
x = root->RChildPtr;
switch (x->BF){
case RH : root->BF = EH;
x->BF = EH;
RotateLeft(root);
taller = false;
break;
case EH : error;
case LH : w = x->LChildPTr;
switch(w->BF){
EH:root->BF = EH; x->BF = EH; break;
LH:root->BF = EH; x->BF = RH; break;
RH:root->BF = LH; x->BF = EH; break;
}//switch
w->BF = EH;
RotateRight(x);
root->RChildPtr = x;
RotateLeft(root);
taller = false;
break;
}//switch
}//RightBalance
void RotateLeft(ptrType& ptr)
{
ptrType temp;
if (ptr == NULL)
error;
else if (ptr->RChildPtr == NULL)
error;
else{
temp = ptr->RChildPtr;
ptr->RChildPtr = temp->LChildPtr;
temp->LChildPtr = ptr;
ptr = temp;
}//else
}//RotateLeft
void RotateRight(ptrType& ptr)
{
ptrType temp;
if (ptr == NULL)
error;
else if (ptr->LChildPtr == NULL)
error;
else{
temp = ptr->LChildPtr;
ptr->LChildPtr = temp->RChildPtr;
temp->RChildPtr = ptr;
ptr = temp;
}//else
}//RotateRight
-shorter is initially true
-case 1: it becomes false if parent node is initially EH.

-case 2: balancefactor of parent is not equal and longer subtree is shortened. Parent becomes EH and shorter is true.

-case 3: balancefactor of parent is not equal and shorter subtree is made shorter. Need to rebalance.


