Вы находитесь на странице: 1из 10

Lab Manual for Data Structures

Lab 13
AVL Trees

Department of Computer Science Page 148


MAJU, Islamabad
Lab 13: AVL Trees

Table of Contents
1. Introduction 150
1.1 AVL Trees 150
1.2 Relevant Lecture Readings 150

2. Activity Time boxing 150

3. Objectives of the experiment 151

4. Concept Map 151


4.1 AVL Trees in C++ 151

5. Homework before Lab 152


5.1 Problem Solution Modeling 152
5.2 Problem description: 152
5.3 Practices from home 152
5.3.1 Task-1 152
5.3.2 Task-2 152

6. Procedure & Tools 152


6.1 Tools 152
6.2 Walk through Tasks [Expected time = 20 mins] 153

7. Practice Tasks 155


7.1 Practice Task 1 [Expected time = 20 mins] 155
7.2 Practice Task 2 [Expected time = 30 mins] 156
7.3 Out comes 156
7.4Testing 156

8. Evaluation Task (Unseen) [Expected time = 60 mins for two tasks] 157

9. Evaluation criteria 157

10. Further Readings 157


10.1 Web sites related to C++ tutorials about AVL and Heap trees 157
10.2 Web sites containing supporting material 157

Department of Computer Science Page 149


MAJU, Islamabad
Lab 13: AVL Trees

Lab 13: AVL Trees and Heaps.

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.

1.1 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.

Figure 1 shows a representation of AVL Tree.

Figure 1: Representation of AVL Tree.

1.2 Relevant Lecture Readings

a) Revise Lecture No. 29 and 30 available at \\dataserver\jinnah$\ in instructor’s


folder.
b) From books: C++ Data Structures by Nell Dale (Page 535 - 546) and Data
structures using C++ by D. S. Malik (Page 635 - 653).

2. Activity Time boxing

Table 1: Activity Time Boxing


Task No. Activity Name Activity time Total Time
5.1 Design Evaluation 20mins 20mins
6.2 Walk through tasks 20mins 20mins
7 Practice tasks 20 mins for task 1 and 2, 30 70mins
mins for task 3
Department of Computer Science Page 150
MAJU, Islamabad
Lab 13: AVL Trees

9 Evaluation Task 60mins for all assigned tasks 60mins

3. Objectives of the experiment

 To understand and implement AVL trees with their operations in C++.

4. Concept Map
This concept map will help students to understand the main concepts of topic covered in
lab.

4.1 AVL Trees in C++

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:

The height of a tree with no elements is 0


The height of a tree with 1 element is 1
The height of a tree with > 1 element is equal to 1 + the height of its tallest subtree.

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

void rotateLeft(AVLTreeNode *n);


void rotateRight(AVLTreeNode *n);
void adjustLeftRight(AVLTreeNode *end, AVLTreeNode
*start);
void adjustRightLeft(AVLTreeNode *end, AVLTreeNode
*start);
// void Delete(int key); // Not implemented yet
void PrintTree();
private:
void ClearTree(AVLTreeNode *n);
void Print(AVLTreeNode *n);
};
#endif

5. Homework before Lab


This homework will help students to study the concepts of topic before start of lab.

5.1 Problem Solution Modeling


After studying the introduction and concept map sections you should be ready to provide
the solution of following problems. Design the solutions of the problems in C++ and
bring your code to lab so that lab instructor should assess and grade it.

5.2 Problem description:


Write a program of AVL tree using linked list for objects of “person” class. Attributes of
“person” class (privately defined) are per_id (int), per_name (string) and per_age (int).
“person” class contains member functions (publicly defined): constructor, input and
output functions. You are required to define two more classes: one of these classes should
define the node implementation of linked list for objects of “person” class. Other class
should define the AVL tree implementation for objects of “person” class; this class
should contain the member functions like insert, search, delete and rotate for AVL trees.

5.3 Practices from home

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. Procedure & Tools


This section provides information about tools and programming procedures used for the
lab.

6.1 Tools
Microsoft Visual Studio 2008 with Visual C++ compiler configured.

Department of Computer Science Page 152


MAJU, Islamabad
Lab 13: AVL Trees

6.2 Walk through Tasks [Expected time = 20 mins]

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.

Figure 2: Implementation of AVL tree.

Figure 3: Implementation of AVL tree.

Department of Computer Science Page 153


MAJU, Islamabad
Lab 13: AVL Trees

Figure 4: Implementation of AVL tree.

Figure 5: Implementation of AVL tree.


Department of Computer Science Page 154
MAJU, Islamabad
Lab 13: AVL Trees

Figure 6: Implementation of AVL tree.

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.

7.1 Practice Task 1 [Expected time = 20 mins]


Write a program to implement an AVL tree for integer values. User should be allowed to insert
values in tree and program should perform in order, pre order and post order traversals of this
tree.

Department of Computer Science Page 155


MAJU, Islamabad
Lab 13: AVL Trees

7.2 Practice Task 2 [Expected time = 30 mins]

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.

7.3 Out comes

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

Test Cases for Practice Task-1


Sample Input Sample Output
Enter the values for elements of AVL
tree:
2
3
4
5
6
Pre order traversal: 2 3 5 4 6
Post order traversal: 5 3 6 4 2
In order traversal: 5 3 2 6 4

Test Cases for Practice Task-2


Sample Input Sample Output
Insert elements in AVL tree:
Regno: 2
Name: ahmad
Cgpa: 2.35
Regno: 3
Name: Ali
Cgpa: 3.33
Regno: 4
Name: Amjad
Cgpa: 2.21
Enter the regno to find in the tree: 4
Following element is found in tree
Regno: 4
Name: Amjad
Cgpa: 2.21
Pre order: 2 3 4
Department of Computer Science Page 156
MAJU, Islamabad
Lab 13: AVL Trees

Post order: 3 4 2

8. Evaluation Task (Unseen) [Expected time = 60 mins for two tasks]

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).

Table 2: Evaluation of the Lab


Sr. No. Task No Description Marks
1 4 Problem Modeling 20
2 6 Procedures and Tools 10
3 7,8 Practice tasks and Testing 35
4 8.1 Evaluation Tasks (Unseen) 20
5 Comments 5
6 Good Programming Practices 10

10. Further Readings


10.1 Web sites related to C++ tutorials about AVL and Heap trees
1. http://www.cs.uah.edu/~rcoleman/Common/CodeVault/Code/Code203_Tree.html
2. http://login2win.blogspot.com/2011/06/c-heaps.html
3. http://www.cplusplus.com/reference/algorithm/make_heap/

10.2 Web sites containing supporting material


1. http://courses.csail.mit.edu/6.006/fall09/lecture_notes/lecture04.pdf
2. http://oopweb.com/Algorithms/Files/Algorithms.html

Department of Computer Science Page 157


MAJU, Islamabad

Вам также может понравиться