# what is the time complexity of finding an element in a binary search tree with n elements

### Mohammed

Guys, does anyone know the answer?

get what is the time complexity of finding an element in a binary search tree with n elements from screen.

## algorithm

The given answer is O(nlog(n)) but I also look it up on Wikipedia and it says it is log(n). Which one is correct? Also what is the worst case complexity for search an unbalanced binary tree?

## What is the time complexity of searching in a binary search tree if the tree is balanced?

Ask Question

Asked 5 years, 9 months ago

Modified 1 year ago Viewed 38k times 1

The given answer is O(nlog(n)) but I also look it up on Wikipedia and it says it is log(n).

Which one is correct?

Also what is the worst case complexity for search an unbalanced binary tree?

algorithmsearchtreetime-complexity

Share

Improve this question

edited Dec 9, 2016 at 9:31

abhiarora 9,0735 5 gold badges 30 30 silver badges 52 52 bronze badges

asked Dec 9, 2016 at 7:12

ted law 111 1 gold badge 1 1 silver badge 2 2 bronze badges Add a comment

## 4 Answers

7

The time complexity for a single search in a balanced binary search tree is O(log(n)). Maybe the question requires you to do n searches in the binary tree, hence the total complexity is O(nlog(n)).

The worst case complexity for a single search in an unbalanced binary search tree is O(n). And similarly, if you are doing n searches in the unbalanced tree, the total complexity will turn out to be O(n^2).

Share Improve this answer

answered Dec 9, 2016 at 7:39

Shubham 2,7353 3 gold badges 21 21 silver badges 35 35 bronze badges Add a comment 2

The average time complexity of searching in balanced BST in O(log(n)).

The worst case complexity of searching in unbalanced binary tree is O(n).

Share Improve this answer

answered Dec 9, 2016 at 7:30

Adhiyamaan 338 8 bronze badges Add a comment 1

The Time complexity of a Balanced Binary Searched Tree is logN, as stated in Wikipedia, because as it traverses the tree, it either goes left or right eliminating half of the whole Tree. For an unbalanced Binary search tree, the time complexity is O(n), it's basically similar to a linear search.

To make the tree balanced, you can use one red-black algorithm, AVL algorithm or several others.

Share Improve this answer

edited Sep 16, 2021 at 6:35

Ziyaddin Sadigov 8,24311 11 gold badges 32 32 silver badges 41 41 bronze badges

answered Dec 9, 2016 at 7:46

Muyide Ibukun 1,19613 13 silver badges 22 22 bronze badges Add a comment 0

In any binary search tree the time complexity taken is O(h), where h is the height of the tree.. Since it is given that tree is balanced binary search tree so searching for an element in worst case is O(logn).

Share Improve this answer

answered Oct 4, 2018 at 4:59

ashish 12 2 bronze badges Add a comment

Not the answer you're looking for? Browse other questions tagged algorithmsearchtreetime-complexity or ask your own question.

The Overflow Blog

I spent two years trying to do what Backstage does for free

A serial entrepreneur finally embraces open source (Ep. 486)

Featured on Meta

Recent Color Contrast Changes and Accessibility Updates

Reviewer overboard! Or a request to improve the onboarding guidance for new...

Should I explain other people's code-only answers?

### Related

558

What is the most efficient/elegant way to parse a flat table into a tree?

883

Use grep --exclude/--include syntax to not grep through certain files

2591

What does O(log n) mean exactly?

1255

Easy interview question got harder: given numbers 1..100, find the missing number(s) given exactly k are missing

1531

What are the options for storing hierarchical data in a relational database?

779

How can building a heap be O(n) time complexity?

1843

Image Processing: Algorithm Improvement for 'Coca-Cola Can' Recognition

1024

How can I find the time complexity of an algorithm?

4122

How can I pair socks from a pile efficiently?

2037

What is the optimal algorithm for the game 2048?

### Hot Network Questions

Has NASA released any JWST images of Trappist-1? If not, why?

What does it mean to "end up with a Desmond"?

How to change row width?

Sci-fi horror short story about alien that wants to be eaten

Would you pass this 13 high card point hand?

Is the food of the type created by the synthesizer in By Any Other Name ever shown again?

How to prevent humans from being killed by alien diseases?

Roadside repair: how to use a screw-on Presta pump on a tube with a removable valve core

Is it okay that part of the power jack is outside the device?

What are some good blogs for Mathematical Statistics and Machine Learning?

Is it achievable to keep all of Halacha?

Join and Union nested lists if they intersect

Were doctors who were drafted in the 1950/1960 years, compelled to take an officer's oath of allegiance?

Advantages of a law license other than the ability to practice law

The same flight ticket got much cheaper a few days later. How come?

Which runway should I use, and how can I recognise it in further flights?

"are sitting" vs "have sat"

Avoid consuming broken cereal?

Filter design with constrained impulse response

Combining a list with a certain index of a list

## Complexity of different operations in Binary tree, Binary Search Tree and AVL tree

A Computer Science portal for geeks. It contains well written, well thought and well explained computer science and programming articles, quizzes and practice/competitive programming/company interview Questions.

## Complexity of different operations in Binary tree, Binary Search Tree and AVL tree

Difficulty Level : Basic

Last Updated : 01 Aug, 2022

In this article, we will discuss complexity of different operations in binary trees including BST and AVL trees. Before understanding this article, you should have basic idea about: Binary Tree, Binary Search Tree and AVL Tree. The main operations in binary tree are: search, insert and delete. We will see the worst case time complexity of these operations in binary trees.

**Binary Tree:**In a binary tree, a node can have maximum two children. Consider the left skewed binary tree shown in Figure 1.

**Searching:**For searching element 2, we have to traverse all elements (assuming we do breadth first traversal). Therefore, searching in binary tree has worst case complexity of O(n).

**Insertion:**For inserting element as left child of 2, we have to traverse all elements. Therefore, insertion in binary tree has worst case complexity of O(n).

**Deletion:**For deletion of element 2, we have to traverse all elements to find 2 (assuming we do breadth first traversal). Therefore, deletion in binary tree has worst case complexity of O(n).

**Binary Search Tree (BST):**BST is a special type of binary tree in which left child of a node has value less than the parent and right child has value greater than parent. Consider the left skewed BST shown in Figure 2.

**Searching:**For searching element 1, we have to traverse all elements (in order 3, 2, 1). Therefore, searching in binary search tree has worst case complexity of O(n). In general, time complexity is O(h) where

**h**is height of BST.

**Insertion:**For inserting element 0, it must be inserted as left child of 1. Therefore, we need to traverse all elements (in order 3, 2, 1) to insert 0 which has worst case complexity of O(n). In general, time complexity is O(h).

**Deletion:**For deletion of element 1, we have to traverse all elements to find 1 (in order 3, 2, 1). Therefore, deletion in binary tree has worst case complexity of O(n). In general, time complexity is O(h).

**AVL/ Height Balanced Tree:**AVL tree is binary search tree with additional property that difference between height of left sub-tree and right sub-tree of any node can’t be more than 1. For example, BST shown in Figure 2 is not AVL as difference between left sub-tree and right sub-tree of node 3 is 2. However, BST shown in Figure 3 is AVL tree.

**Searching:**For searching element 1, we have to traverse elements (in order 5, 4, 1) = 3 = log2n. Therefore, searching in AVL tree has worst case complexity of O(log2n).

**Insertion:**For inserting element 12, it must be inserted as right child of 9. Therefore, we need to traverse elements (in order 5, 7, 9) to insert 12 which has worst case complexity of O(log2n).

**Deletion:**For deletion of element 9, we have to traverse elements to find 9 (in order 5, 7, 9). Therefore, deletion in binary tree has worst case complexity of O(log2n).

We will discuss questions based on complexities of binary tree operations.

**Que-1.**What is the worst case time complexity for search, insert and delete operations in a general Binary Search Tree?

**(A)**O(n) for all

**(B)**O(Logn) for all

**(C)**O(Logn) for search and insert, and O(n) for delete

**(D)**O(Logn) for search, and O(n) for insert and delete

**Solution:**As discussed, all operations in BST have worst case time complexity of O(n). So, the correct option is (A).

**Que-2.**What are the worst case time complexities of searching in binary tree, BST and AVL tree respectively?

**(A)**O(n) for all

**(B)**O(Logn) for all

**(C)**O(n) for binary tree, and O(Logn) for others

**(D)**O(n) for binary tree and BST, and O(Logn) for AVL

**Solution:**As discussed, search operation in binary tree and BST have worst case time complexity of O(n). However, AVL tree has worst case time complexity of O(logn). So, the correct option is (D).

Recommended

Solve DSA problems on GfG Practice.

Solve Problems

## Time Complexity of Binary Search Tree

Time complexity of binary search tree- Time complexity of BST operations is O(h) where h is the height of binary search tree. Binary search tree is a special kind of binary tree.

## Time Complexity of Binary Search Tree

Data Structures

**Binary Search Tree-**

Before you go through this article, make sure that you have gone through the previous article on **BST Operations**.

Commonly performed operations on binary search tree are-

Search Operation Insertion Operation Deletion Operation

In this article, we will discuss time complexity of BST Operations.

**Time Complexity-**

Time complexity of all BST Operations = O(h).

Here, h = Height of binary search tree

Now, let us discuss the worst case and best case.

**Worst Case-**

In worst case,

The binary search tree is a skewed binary search tree.

Height of the binary search tree becomes n.

So, Time complexity of BST Operations = O(n).

In this case, binary search tree is as good as unordered list with no benefits.

**Best Case-**

In best case,

The binary search tree is a balanced binary search tree.

Height of the binary search tree becomes log(n).

So, Time complexity of BST Operations = O(logn).

To gain better understanding about Time Complexity of BST Operations,

**Watch this Video Lecture**

**Download Handwritten Notes Here-**

**Next Article-**

**Introduction to AVL Trees**

Get more notes and other study material of **Data Structures**.

Watch video lectures by visiting our YouTube channel **LearnVidFun**.

Summary

Article NameTime Complexity of Binary Search Tree

DescriptionTime complexity of binary search tree- Time complexity of BST operations is O(h) where h is the height of binary search tree. Binary search tree is a special kind of binary tree.

AuthorAkshay Singhal

Publisher NameGate Vidyalay

Publisher Logo

Guys, does anyone know the answer?