You are on page 1of 6

CS 201 Data Structures – AVL tree Tutorial

1) Explain how balancing is required to build a Binary Search Tree and an AVL tree.
For binary search tree balancing is not necessary. But for AVL tree balancing is necessary.

2)

a) Height of the left sub tree of the node v and height of the right sub tree of the node v
differ at most 1. That difference should be exactly -1,0,1. Balance of the node v can
calculate from fellow equation,
balance = height of left sub tree of node v – height of right sub tree of node v
Balance for node a = height of left sub tree – height of right sub tree
= 2–2
= 0
Balance for node b = height of left sub tree – height of right sub tree
= 1–0
= 1
Balance for node c = height of left sub tree – height of right sub tree
= 0–1
= -1
Balance for node d = height of left sub tree – height of right sub tree
= 0–0
= 0
Balance for node e = height of left sub tree – height of right sub tree
= 0–0
= 0

b) Insertion, deletion and searching operations of AVL tree take O(log n) in both the
average and worst cases.

3)
a) Explain the usage of rotations when building an AVL tree.
When inserting a new node to AVL tree if AVL tree become unbalanced, rotations are
used for rebalance it.

b) What are the four types of rotations that use to balance an AVL tree?
RR rotation – Right-Right rotation
LL rotation – Left-Left rotation
RL rotation – Right-Left rotation
LR rotation – Left- Right rotation

c) Categorize the above rotations as single and double rotations.


Single rotation - RR rotation, LL rotation
Double rotation - RL rotation, LR rotation
4)
a) Consider a binary search tree obtained by starting with an empty tree. Insert the values
1,2,3,4 and 5, to the binary tree respectively.

b) Consider an AVL tree obtained by starting with an empty tree. Insert the values 1,2,3,4 and
5, to the AVL tree respectively.

Worst case scenarios Part a) BST Part b) AVL tree


of runtime of
Insertion O(1) O(log n)

Deletion O(n)

Search O(n)
c) Compare the worst case scenario of runtime of searching for the trees in part a. and b.

5)
a) Insert the following sequence of elements into an AVL tree, starting with an empty tree:
10, 20, 15, 25, 30, 16, 18, 19 b.
P.T.O
b) Delete 30 in the AVL tree that you got.

You might also like