You are on page 1of 4

# AVL Tree Operation: Various operations performed on AVL trees are as: Searching a key or value

## Search and traversal operation is exactly same as

Binary search tree. Insert and Delete operation are

also same as

## perform an extra operation called Rotation to

restore AVL-ness property, if get violated by

## Insertion of a Key or value

insert/delete operation.

## AVL Tree Search

Searching a particular value or key in an AVL tree is same as Binary Search Tree.
As AVL tree always remain balanced tree. So to search a value in AVL tree
require log (n) time (in Best Case/Worst Case).
Worst case Running Time to search a key or value in an AVL tree is O (log n)

## AVL Tree Insert

Insertion of nodes in an AVL had discussed earlier during rotation, but lets
again discuss the same concept in greater detail.
Insertion of a node in AVL Tree is same as insertion of node in BST.
If insertion of a node in AVL tree violates AVL-ness property, restore
its AVL-ness property by performing Rotation (as discussed earlier).
To perform rotation, trace the path from newly inserted node towards
the root and along the path check height difference at each node.
If a node satisfies AVL-ness property, proceed to next node
toward root node or Exit (Tree satisfy AVL-ness property).
If found any imbalance node, then perform required rotation.

Example

Insert 40, 30, 20, 50, 60, 43, 10, 35, 33 and 34 into an Empty AVL Tree.
40
Insert 40, 30, 20.

30

30
Right Rotation

30

## Insert 50, 60.

40

20

40

20

20

50
60
Left Rotation

20

Right-Left Rotation

50

30

30

30

40

43

40

60

40

60

50

20

Insert 43

50

20

43
Insert 10, 35,
33, and 34.

35

40

50

30
20
10

35
33
34

43

40

30

Left-Right Rotation

20

60
10

33
34

50
43

60

60

## Analysis of AVL Tree insert Operation

Insertion of new node in AVL tree is same as that of binary search tree, so
insertion of node in AVL tree take: O (log n) time.
Insertion of new node may violates "AVL-ness" property of the Tree, so
need to check balance factor of each node in the path from the newly
inserted node towards the root by traversing the path. Path traversed from
newly inserted node to imbalanced node is equal to Height of AVL tree
(In worst case). So cost of traversing the path is: O (log n). Along the
path from newly inserted node up-to imbalanced node(root node in
worst case) we will perform following operation as: To Check height difference (BF) at each node require: O (1) time
If a node along the path violate AVL-ness property, then
perform a rotation, each rotation will require: O (1) time
If satisfies, then proceed to next node or exit: O (1) time
Total time required to perform above operation is: O (log n)
Therefore Time complexity to insert a node in AVL tree is: O (log n)
Example:
-

Imbalance, so rotate

35

## Insert 10, need to

traverse height of AVL
tree. (I.e. h= log n)

30

40

O (1)

35

## Move toward root in

search of imbalanced
node (root node have
BF=2, I.e. imbalanced)

O (1)

30

40

O (1)

20
h = log n

33

20

h = log n
O (1)

10

10

33

Important Point

## Insertion of a node in an AVL tree take log (n) time.

As insertion of a node in an AVL tree take log(n) time so insertion of n
nodes in AVL will take n log(n) time.
As we know AVL tree is always a balanced tree, and always maintain height
equal to log (n) so worst case running time of insert operation is log (n).

Example: -