0% found this document useful (0 votes)
54 views

Data Structure - AVL Tree

This document discusses AVL trees and their balancing properties compared to binary search trees. It explains that AVL trees require balancing to maintain a height difference of at most 1 between the left and right subtrees of each node. Rotations are used during insertions and deletions to rebalance the tree if this balancing property is violated. The document provides examples of inserting values into empty binary search trees and AVL trees to compare their worst case runtimes for operations like insertion, deletion and search.

Uploaded by

Weerasinghe
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
54 views

Data Structure - AVL Tree

This document discusses AVL trees and their balancing properties compared to binary search trees. It explains that AVL trees require balancing to maintain a height difference of at most 1 between the left and right subtrees of each node. Rotations are used during insertions and deletions to rebalance the tree if this balancing property is violated. The document provides examples of inserting values into empty binary search trees and AVL trees to compare their worst case runtimes for operations like insertion, deletion and search.

Uploaded by

Weerasinghe
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 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