45 45 votes Which of the following is TRUE? The cost of searching an AVL tree is $\Theta (\log n)$ but that of a binary search tree is $O(n)$ The cost of searching an AVL tree is $\Theta (\log n)$ but that of a complete binary tree is $\Theta (n \log n)$ The cost of searching a binary search tree is $O (\log n )$ but that of an AVL tree is $\Theta(n)$ The cost of searching an AVL tree is $\Theta (n \log n)$ but that of a binary search tree is $O(n)$ Data Structures gateit-2008 data-structures binary-search-tree easy avl-tree + – Ishrat Jahan 19.8k views answer comment Share Follow Print See all 6 Comments 6 6 Comments reply Chaithu555 commented Feb 4, 2024 reply Follow flag What would be the cost of searching an element in the complete binary tree (considering the worst case) ??(like how can we modify option B to be correct?) 2 2 replyShare ꧁༒☬ĿọŗԀ 🆂🅷🅸🆅🅰☬༒꧂ commented May 9, 2024 reply Follow flag @Chaithu555 for searching cost in CBT will be O(n) in worst case u've to traverse all the nodes . 9 9 replyShare Tushar Rana commented Jan 2, 2025 reply Follow flag Since AVL is self balancing and it's balance factor is either -1, 0 or 1 it can never be skewed but BST can if all values are sorted. Therefore AVL search is "logn" and BST search is "n" 4 4 replyShare js__ commented Jan 24 reply Follow flag @Tushar Rana Is there any type of binary tree whose worst-case search exceeds O(n) ? i think no 0 0 replyShare Tushar Rana commented Jan 24 reply Follow flag @js__ Yes it's obvious that when you take the worst case just become a linear thing that is an array. So it is just the number of elements, now apply linear search on it, and it's evident that we can't/ do not need more than n-1 comparisons and it will be O(n), also we can say it in theta terms as max cap is n so theta can be used. You have used O(n) that means at most but since you are considering the worst case like the ultimate case therefore it's better to use theta that is "equal to". 0 0 replyShare Gowtham_Kumar commented Sep 11 reply Follow flag AVL IS SELF BALANCING SO EVEN THOUGH THE BEST AND AVG CASE ARE SAME FOR BST AND AVL TREE IN WORST CASE BST BECOMES O(n) due to skewed trees while incase of AVL trees due to the balancing factor the skewed case does not exist where all elements are either in left or right subtree alone so worst case for AVL tree is STILL logn only! 0 0 replyShare Please log in or register to add a comment.
Best answer 67 67 votes A) is true as AVL tree is a balanced search tree that has time complexity of searching $\Theta ( \log n)$, but in binary search tree, we can have a completely left/right skewed tree, in which search is $O(n)$. Happy Mittal answered Oct 28, 2014 • edited Dec 21, 2017 by kenzou Happy Mittal comment Share Follow See all 9 Comments 9 9 Comments reply Show 6 previous comments Abhrajyoti00 commented Nov 2, 2022 reply Follow flag Proof that AVL tree is always of height O(log n):- If $n(h)$ is the minimum number of internal nodes of an AVL tree of height $h$. We know that $n(h) = 1 + n(h-1) + n(h-2)$; $n(0) = 1, n(1) = 2$ Also, $n(h-1) > n(h-2)$ $\implies n(h) > 2n(h-2). $ So $n(h) > 2n(h-2)$ $ n(h) > 4n(h-4)$, $n(h) > 8n(n-6)$ … , $n(h) > 2^i n(h-2i)$ Solving the base case we get: $n(h) > 2 ^{h/2-1}$ Taking logarithms: $h < 2.log n(h) +2$ $\implies h = O(log (n))$ Good read: AVLTrees.pdf (uci.edu) 4 4 replyShare shikhar500 commented Dec 14, 2022 reply Follow flag @ASNR1010 how did u get it can u plz explain it to me also ? 0 0 replyShare cormen commented Sep 22, 2024 reply Follow flag @shikhar500, I think what he meant was in some cases, the search might finish earlier than the worst-case scenario. So it's best to use O(n) insead of theta(n). Please correct me if I'm wrong. 0 0 replyShare Please log in or register to add a comment.
3 3 votes option A is right RAJESHWAR YADAV answered Nov 27, 2016 RAJESHWAR YADAV comment Share Follow 0 reply Please log in or register to add a comment.
0 0 votes AVL trees guarantee height Θ(logn)\Theta(\log n)Θ(logn), so search is Θ(logn)\Theta(\log n)Θ(logn). A BST can be skewed with height Θ(n)\Theta(n)Θ(n), so worst-case search is O(n)O(n)O(n). engineerbug answered Jan 31 engineerbug comment Share Follow 0 reply Please log in or register to add a comment.
0 0 votes (A) The cost of searching in AVL Tree is O(log n) because the height of AVL Tree is ⌈log n⌉ and The cost of searching in Binary Search Tree is O(n) because in worst case BST can be left/right Skewed , So we have to traverse all nodes for searchingSo, Option(A) is correctFor Complete Binary Tree cost of serching is O(n) there is no ordering. So, for searching an element we have to traverse all nodes Vishal_Jaiswal answered Aug 26 Vishal_Jaiswal comment Share Follow 0 reply Please log in or register to add a comment.