Week 10 Data Structures

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 2

AVL Trees

Implementation
Lab Task

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.

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

1. Determine if a Binary Search Tree is critically imbalanced and distinguish between


the various types of imbalance.
2. Implement functions to rotate nodes and balance a Binary Search Tree. 3. Insert and
remove nodes from a Binary Search Tree while maintaining tree balance. Consider a tree in
figure 1, you are supposed to write a code to implement the given tree as it is. Your class shall
contains the following methods:
● insert(); to insert a new element into the tree.
● getHeight(); to find the height of a tree (simply a specific node).
● isBalanced(); to check whether the tree is balanced or not.
● rotate(); to rotate the tree in order to make it balance.
● inorderPrint(); to perform the inorder traversal of the tree.
Now insert a new element i.e., 23 to the tree which makes the balance factor of the 17 to 2 which
makes the tree unbalanced.
● your function isBalanced(); shall print the tree as unbalanced.
● use rotate(); to make the tree balanced.
● use inorderPrint(); to traverse the tree by printing the left node, root node and then right
node (inorder traversal).
● Your output shall looks like:
3-7-10-12-17-23-27-38-44-45

Good Luck!

You might also like