4 Replies - 593 Views - Last Post: 28 November 2017 - 06:18 AM

#1 chloeCodes   User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 227
  • Joined: 05-January 17

Time Complexity of Binary Search Tree

Posted 27 November 2017 - 04:06 PM

Hi!

I've been doing research into best/average/worst case performance. I'm have just started understanding Big-O notation.
I was under the impression that the worst case performance of a Binary Search Tree would be be o(n) if the data set is ordered, and thus the efficiency would be the same as a Linked List. This makes sense to me!

I was under the impression that that the best case would be O(Log N) due to the binary aspect of the data structure. Would this be the best case or average case? On Wikipedia, it says that deletion, search and addition on average are O(Log N), so what would be the best case? I have done lots of research online, different websites say conflicting things. Some blog points said that the best case would be O(1). My understanding of O(1) describes an algorithm that will always execute in the same time (space) regardless of the size of the data set. So I'm struggling to understand how this can be the best case Time Complexity for Binary Search tree. I would appreciate some insight.

Many Thanks!

Is This A Good Question/Topic? 0
  • +

Replies To: Time Complexity of Binary Search Tree

#2 macosxnerd101   User is online

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12607
  • View blog
  • Posts: 45,747
  • Joined: 27-December 08

Re: Time Complexity of Binary Search Tree

Posted 27 November 2017 - 04:22 PM

Moved to Computer Science.

Quote

I was under the impression that the worst case performance of a Binary Search Tree would be be o(n) if the data set is ordered, and thus the efficiency would be the same as a Linked List. This makes sense to me!


Performance for what operation(s)? This needs to be specified. Also note that Big-O is very different from little-oh. Suppose an algorithm runs in time T(n). We say that T(n) is O(n) if there exist constants c, N such that T(n) <= cn for all n >= N. In limit notation, we are saying that:
lim(n->infinity) T(n)/n < infinity



To contrast, if T(n) is o(n), then we are saying that:
lim(n->infinity) T(n)/n = 0



That is, T(n) grows strictly slower than n. The function T(n) = log(n) is o(n), while the function T(n) is O(n) but not o(n).


Quote

My understanding of O(1) describes an algorithm that will always execute in the same time (space) regardless of the size of the data set.


This is a correct interpretation for worst-case analysis. For best-case analysis, O(1) is correct. Consider an empty BST. How many steps does it take to insert an element?

Now suppose we have a BST T, and are performing a search. If the element we are looking for is the root, then search only takes O(1) time. This is the best case, not the worst or average case.
Was This Post Helpful? 1
  • +
  • -

#3 chloeCodes   User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 227
  • Joined: 05-January 17

Re: Time Complexity of Binary Search Tree

Posted 28 November 2017 - 03:54 AM

Thanks for getting back to me!

Sorry, I should have been more clear. When I meant performance, I meant for search, add and remove operations in a Binary Search Tree. For search, remove and add operations, then yes the worst case performance would be O(n), in the case that the Binary Tree is like a Linked List.

On average the performance for search, add and remove operations will be O(Log n) due to the nature of the halving at each step.


Many online sources still state that the best case is also O(Log n), but in fact it should be o(1) correct?
Was This Post Helpful? 0
  • +
  • -

#4 macosxnerd101   User is online

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12607
  • View blog
  • Posts: 45,747
  • Joined: 27-December 08

Re: Time Complexity of Binary Search Tree

Posted 28 November 2017 - 05:29 AM

That is correct.
Was This Post Helpful? 1
  • +
  • -

#5 chloeCodes   User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 227
  • Joined: 05-January 17

Re: Time Complexity of Binary Search Tree

Posted 28 November 2017 - 06:18 AM

Cheers!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1