Escolar Documentos
Profissional Documentos
Cultura Documentos
also same as
insert/delete operation.
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)
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
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
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
30
40
O (1)
35
O (1)
30
40
O (1)
20
h = log n
33
20
h = log n
O (1)
10
10
33
Important Point
Example: -