Wednesday 10 April 2019

How AVL tree is better than BST?

How AVL tree is better than BST?
Ans: A major advantage of using the tree structure is to be able to access data very efficiently.
The access time is O(h) where h is the height of the tree.
When a Binary search tree is balanced with n nodes, h = O(log n). Example of balanced BST is shown below
so the access time is O(log n) much better than O( n) provided by linear data structure such as an array or linked list
But a BST may degenerate into a linear structure if not balanced
Consider a situation when 1,2,3 ... n are inserted into BST, BST becomes degenerate tree and search take O(n) time.
Hence BST must be balanced. AVL is a balanced BST and ensures the height of the tree is always O(log n) by rotations.
Search, insertion and deletion in AVL tree is always  log n  always whereas search, insertion and deletion in BST is O(n) in worst case.

No comments:

Post a Comment