Basic Operations of AVL TREES Adelson Velenskii and Landis
Posted By : Adarsh Singh | 30-Nov-2018
Searching :
An AVL Tree can be searched for a key by visiting nodes starting from the root. The visited node is checked for the presence of K. If found, then the search stops otherwise if the value of K is less then the key value of the visited node, then the left subtree else the right subtree is searched. the algorithm for an AVL Tree is given below.
Algorithm searchAVL (Tree, K) {
if (DATA (Tree) == K)
prompt "search successful"; Stop;
if (DATA (Tree) < K)
searchAVL (leftChild (Tree), K);
else
searchAVL (rightChild (Tree), K);
}
Inserting and deleting a node in an AVL Tree :
To make sure that the tree remains AVL after every operation, we must add the standard BST insert operation to perform some re-balancing. There two basic operations that can be performed to re-balance a BST without violating the BST property (keys(left) < key(root) < keys(right)).
1. Left Rotation
2. Right Rotation.
Steps to follow for insertion :
Let the newly inserted node be a
1) Perform BST insert for a.
2) Starting from a, travel up and find the first unbalanced node. Let b be the first unbalanced node, c be the child of b that comes on the path from a to b and d be the grandchild of b that comes on the path from a to b.
3) Balance the tree by performing appropriate rotations on the subtree rooted with b. There can be 4 possible cases that
a) c is left
b) c is left
c) c is right child of
d) c is right child of
Steps to follow for deletion :
Let a be the node to be deleted
1) Perform BST delete for a.
2) Starting from a, travel up and find the first unbalanced node. Let b be the first unbalanced node, c be the larger height child of b, and d be the larger height child of c. Note that the definitions of d and
3) Balance the tree by performing appropriate rotations on the subtree rooted with b. There can be 4 possible cases that
a) c is left
b) c is left
c) c is right child of
d) c is right child of
Time Complexity:
Rotation operations (right and left rotate) take constant time as only a few pointers are being changed there. Height and the balance factor also takes constant time. So we can say the time complexity of AVL insert remains same as BST insert which is O(h) where h is the height of the tree. If the tree is AVL tree, the height is O(Logn) and time complexity of AVL insert is O(Logn).
Cookies are important to the proper functioning of a site. To improve your experience, we use cookies to remember log-in details and provide secure log-in, collect statistics to optimize site functionality, and deliver content tailored to your interests. Click Agree and Proceed to accept cookies and go directly to the site or click on View Cookie Settings to see detailed descriptions of the types of cookies and choose whether to accept certain cookies while on the site.
About Author
Adarsh Singh
Adarsh Singh is working as Front-End developer, having good knowledge of Angularjs, Javascript, Html, Less.