DS Lab 13 - AVL Trees
DS Lab 13 - AVL Trees
DS Lab 13 - AVL Trees
Lab 13
AVL Trees
Table of Contents
1. Introduction 150
1.1 AVL Trees 150
1.2 Relevant Lecture Readings 150
8. Evaluation Task (Unseen) [Expected time = 60 mins for two tasks] 157
1. Introduction
In this lab, we will discuss the usage of AVL (Adelson-Velskii and Landis' tree) and heap
data structures. AVL trees are called self-balancing binary search trees. We will see how to
perform insert, remove and search operations on AVL trees.
An AVL tree (Adelson-Velskii and Landis' tree, named after the inventors) is a self-
balancing binary search tree. In an AVL tree, the heights of the two child sub trees of any node
are different by at most one; if at any time they be at variance by more than one, rebalancing is
done to restore this property. 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 proceeding to the operation.
Insertions and deletions may require the tree to be rebalanced by one or more tree rotations.
4. Concept Map
This concept map will help students to understand the main concepts of topic covered in
lab.
An AVL tree is a special type of binary tree that is always "partially" balanced. The
criteria that is used to determine the "level" of "balanced-ness" is the difference between the
heights of subtree of a root in the tree. The "height" of tree is the "number of levels" in the tree.
Or to be more formal, the height of a tree is defined as follows:
An AVL tree is a binary tree in which the dissimilarity between the height of the right and left
sub trees (or the root node) is never more than one.
#include <iostream>
using namespace std;
struct AVLTreeNode
{
int key;
// Other data fields can be inserted here
AVLTreeNode *left;
AVLTreeNode *right;
AVLTreeNode *parent;
char balanceFactor;
};
class Code203_Tree
{
private:
AVLTreeNode *root;
public:
Code203_Tree(); // Constructor
~Code203_Tree(); // Destructor
void Insert(AVLTreeNode *n);
void restoreAVL(AVLTreeNode *ancestor, AVLTreeNode
*newNode);
void adjustBalanceFactors(AVLTreeNode *end,
AVLTreeNode *start);
Department of Computer Science Page 151
MAJU, Islamabad
Lab 13: AVL Trees
5.3.1 Task-1
Compare AVL (self balancing) trees and balanced binary trees and find at least three
differences between these two implementations.
5.3.2 Task-2
Provide algorithmic representation of rotate function for AVL tree.
6.1 Tools
Microsoft Visual Studio 2008 with Visual C++ compiler configured.
Following screens in figure 2 to 6 represent the implementation of AVL tree. You are
required to code and execute this program in Microsoft Visual Studio 2008.
7. Practice Tasks
This section will provide information about the practice tasks which are required to be performed
in lab session. Design solutions of problems discussed in each task and place solution code in a
folder specified by your lab instructor.
Write a program which should implement an AVL tree using linked list. Elements of tree are
objects of “student” class. “student” class contains attributes (privately defined) reg_no(int),
st_name (string) and cgpa (float). “student” class should also contain member functions (publicly
defined); constructor, input and output functions. User will insert objects of class “student” in
AVL tree, values of attributes of objects will be provided by user. Program should display the
objects of student class in pre order, post order and in order traversal formats. Program should
also search an object from tree which is provided by user as input.
After completing this lab, student will be able to understand and develop programs related to
AVL trees and heap trees in C++ using Microsoft Visual Studio 2008 environment.
7.4Testing
Post order: 3 4 2
The lab instructor will give you unseen task depending upon the progress of the class.
9. Evaluation criteria
The evaluation criteria for this lab will be based on the completion of the following tasks. Each
task is assigned the marks percentage which will be evaluated by the instructor in the lab whether
the student has finished the complete/partial task(s).