Lab 11 - AVL Tree (04/12/2021)


Description

"In computer science, an AVL tree is a self-balancing binary search tree, and it was the first such data structure to be invented. In an AVL tree, the heights of the two subtrees of any node differ by at most one. Lookup, insertion, and deletion all take O(log n) time in both the average and worst cases, where n is the number of nodes in the tree prior to the operation. Insertions and deletions may require the tree to be rebalanced by one or more tree rotations."

Learning Outcomes

By completing the AVL Tree Lab, you will be able to:


The Lab

Lab Requirements:

  1. There are multiple correct methods for rebalancing nodes in an AVL tree; each method may result in a unique tree. Some conventions will need to be used to ensure that your tree properly matches ours. For this class, you should follow the algorithms presented in the course text, starting on page 634.
    • For example, when a Node is left-imbalanced and its left child is balanced, treating it as a Left-Right case will produce a "balanced" tree, but it would be different from the expected output and would be marked as incorrect. You should treat this as a Left-Left case.
    • When removing a node with 2 children, replace it with its in-order-predecessor, or "greatest left child."
  2. Remember to disallow duplicate entries and handle the case where the element to be removed is not in the tree.
  3. You should remove nodes from the AVL tree in the same manner used in the BST lab.
  4. You are required to make your AVL a template class.
  5. You may not use any Standard Library data structures.
  6. The AVL code used in the textbook can be hard to follow. Feel free to ask the TA's for help and ideas.
  7. The following summarizes the commands used in the AVL lab:

  8. COMMAND DESCRIPTION OUTPUT
    INT Instantiates a AVL object for subsequent commands. true
    STRING Instantiates a AVL object for subsequent commands. true
    add <data> Add data node to AVL. Return false if duplicate. true
    false
    remove <data> Remove node from AVL. Return false if not found. true
    false
    clear Delete all nodes from the AVL. true
    size Output the number of nodes in the AVL. Number of AVL nodes
    print Print AVL (using insertion operator) in level-order. Level-order listing of AVL
    find <data> Find node in AVL. Return "found" or "not found". found
    not found

Steps to Success:


Helps and Hints

Note: Code examples come from readily available, public websites (ie., stackoverflow.com, cplusplus.com, etc.)

Online AVL Visualization:

Online BST Visualization:

Level Order Traversal of a BST.(collapse)

A binary tree is one where each node has at most two children, named left and right. The level of a tree is the number of parent nodes a tree node has. Thus, the root of the tree is at level 1. The root's children are at level 2, grandchildren are at level 3, etc.

Trees can be traversed in level-order by visiting every node on a level before going to a lower level.

/** Output nodes at a given level */
bool outLevel(Node<T>* root, int level, stringstream& out) const
{
   if (root == NULL) return false;
   if (level == 1)
   {
      out << " " << root->data;
      if ((root->left != NULL) || (root->right != NULL)) return true;
      return false;
   }
   if ((level == 2) && !root->left && root->right) out << " _";
   bool left = outLevel(root->left, level - 1, out);
   bool right = outLevel(root->right, level - 1, out);
   if ((level == 2) && root->left && !root->right) out << " _";
   return left || right;
} // end outLevel()

/** Return a level order traversal of a BST as a string */
string toString() const
{
   stringstream out;
   if (root == NULL) out << " empty";
   else
   {
      int level = 0;
      do
      {
         out << endl << "  " << ++level << ":";
      } while (outLevel(root, level, out));
   }
   return out.str();
} // end toString()

Understanding Valgrind Output.(collapse)

  • You can find helpful hints and explanations of Valgrind output here.

AVL Tree Grading

Instructions for submitting your lab:

Use the following input and resulting output files in testing the AVL Tree lab.

Input FileOutput File
Test #1 lab11_in_01.txt lab11_out_01.txt
Test #2 lab11_in_02.txt lab11_out_02.txt
Test #3 lab11_in_03.txt lab11_out_03.txt
Test #4 lab11_in_04.txt lab11_out_04.txt
Test #5 lab11_in_05.txt lab11_out_05.txt

The auto-grader will be expecting to find in your output file the following sections:

Fail
Pass
Score = 0

 

Total Score = 0

 

Your lab will not be peer reviewed.

No
Partial
Yes
Score = 0