68 / 100

CptS 215 Data Analytics Systems and Algorithms. Define a field self.graph within AVLTree class. Initialize it to None within the constructor method.

Washington State University

Srini Badri (Instructor)

Gina Sprint (Original Author)

PA4 AVL Trees (100 pts)


Learner Objectives

At the conclusion of this programming assignment, participants should be able to:

  • Implement AVT Trees
  • Visualize Trees


Before starting this programming assignment, participants should be able to:

  • Write object-oriented code in Python
  • Work with trees
  • Work with lists


Content used in this assignment is based upon information in the following sources:

Overview and Requirements

AVL Trees provide a balanced binary tree for efficient storage, retreival and deletion of data. We learnt about implementing AVL Trees in the class. We will expand on the implementation by supporting delete operations. We will also practice visualizing the tree using Graphviz and Python’s pydot module.

Note: for this assignment, do not use Jupyter Notebook to code your solution. Use standard Python script (.py) files.

Program Details

Delete operations:

delete(key), remove(node) & update_balance_delete(node) methods:

Write a program that implements and tests an AVL tree delete(key) method. This method accepts a key to remove. If the key is in the tree, the method finds the node with the key, removes the node, re-balances the tree, and returns True. If the key is not in the tree, False is returned.

Similar to the Binary Search Tree implementation, the delete(key) method would need a remove(node) method to remove the node containing the key. This method would be similar in implementation to BST’s remove(node) method except that it would also need to ensure the tree is balanced (similar to _put() method).

So, to support the re-balancing operation, implement update_balance_delete(node) method that would recursively update the balance factor of the parent node and call rebalance(node) method if needed.

Finally, make sure update_balance_delete(node) method is called from within remove(node) method during node removal operation.

Here is the pseudo-code (not actual code) for update_balance_delete(node) method:

def update_balance_delete(self, node):

if node has parent:

update parent’s balance factor

if node’s parent is unbalanced (balance factor < 1 or balance factor > 1:


recursively call this method on node’s parent’s parent

elif base condition is met (node’s parent’s balance factor != 0):



call this method recursively on parent if needed

Note: update_balance_delete(node) method is needed since the update_balance(node) method is designed for rebalance during insertion (put) operations.


Test your implementation by adding and removing the following items in the order shown: Data Analytics Systems and Algorithms

In [ ]:














Below is the expected output from the above test code:

Level order traversal output – original tree:



Level order traversal output – after delete operations:




Tree Visualization

Visualize the tree using Python pydot module.

  • Define a field self.graph within AVLTree class. Initialize it to None within the constructor method
  • Define methods visualize(file) and visualize_helper(node) methods within AVLTree class.
  • The visualize(file) method should initialize the self.graph field to a new graph object using Graph() constructor method. It should also add root node to the graph plot and call visualize_helper(node) with root node as agrument. Finally, the visuallize(file) should render and save the tree visualization to a .png, .jpeg or .pdf file.
  • The visualize(node) method should recursively add tree nodes and their parent-child relationships (edges) to self.graph for visualization.

Data Analytics Systems and Algorithms

Note: You may use the provided level_order_traversal() and level_order_helper() methods as templates for defining visualize(file) and visualize_helper(node) methods.

Here is a sample of the expected original tree:  Diagram

Description automatically generated

Here is a sample view of the expected updated tree after delete operations shown in the Test code:  Diagram

Description automatically generated

Submitting Assignments

  1. Use the Blackboard tool https://learn.wsu.edu to submit your assignment. You will submit your code to the corresponding programming assignment under the “Content” tab. You must upload your solutions as <your last name>_pa4.zip by the due date and time.
  2. Your .zip file should contain your .py files and your .txt files used to test your program.

Grading Guidelines

This assignment is worth 100 points bonus. Your assignment will be evaluated based on a successful compilation and adherence to the program requirements. We will grade according to the following criteria:

Standard (100 points):

  • 50 pts for correct delete(key), remove(node) and update_balance_delete(node) implementations
  • 10 pts for correct test result output
  • 30 pts for correct visualize(file) and visuallize_helper(node) implementations
  • 5 pts for generating and including the correct tree visualization plots (.png, .jpg or .pdf) files
  • 5 pts for adherence to proper programming style and comments established for the class