AVL Trees

  1. Efficiency of Binary Search Tree Operations
    • BSTs come in all shapes and sizes. The efficiency of a retrieval operation, deletion or insertion correlates directly with the height of the tree.

    • Each search travels from the root to the node with the proper SearchKey or NULL is the node is not in the tree, so the maximum number of comparisons = number of nodes on the longest path through the tree = height of the BST.

    • A tree of maximum height is one where each node has at most one child. This resembles a linked list.

    • To have the minimum height of a tree, you need to fill in each level as completely as possible.

    • The maximum number of nodes that binary tree of height h can have is 2h - 1. So the minimum height of a tree with N nodes is

         the upper bound of log2(N+1)
      
    • The minimum height is the same as the maximum number of comparisons a binary search would need.

    • As you move away from a balanced tree to a more linear structure, height approaches N and the maximum comparisons increases with the height of the the tree - ultimately becoming the same as searching a linked list.

    • The shape of a binary search tree is determined based on the order of the input data. If it comes in order, you get a tree of maximum height. With more random order of input, the tree tends to be more bushy. Is there a way of guaranteeing a tree close to or at minimum height.?
  • AVL Trees
  • Insertion into an AVL tree
  • Code
    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
    
  • Deletion of a node x from an AVL tree
    D. Noonan 3/31/2003