Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is O(logn) always a tree?

We always see operations on a (binary search) tree has O(logn) worst case running time because of the tree height is logn. I wonder if we are told that an algorithm has running time as a function of logn, e.g m + nlogn, can we conclude it must involve an (augmented) tree?

EDIT: Thanks to your comments, I now realize divide-conquer and binary tree are so similar visually/conceptually. I had never made a connection between the two. But I think of a case where O(logn) is not a divide-conquer algo which involves a tree which has no property of a BST/AVL/red-black tree.

That's the disjoint set data structure with Find/Union operations, whose running time is O(N + MlogN), with N being the # of elements and M the number of Find operations.

Please let me know if I'm missing sth, but I cannot see how divide-conquer comes into play here. I just see in this (disjoint set) case that it has a tree with no BST property and a running time being a function of logN. So my question is about why/why not I can make a generalization from this case.

like image 448
Martin08 Avatar asked Feb 22 '10 04:02

Martin08


People also ask

Why is a tree O log n?

A lookup for a node with value 1 has O(n) time complexity. To make a lookup more efficient, the tree must be balanced so that its maximum height is proportional to log(n) . In such case, the time complexity of lookup is O(log(n)) because finding any leaf is bounded by log(n) operations.

What is O n/o Logn?

O(nlogn) is known as loglinear complexity. O(nlogn) implies that logn operations will occur n times. O(nlogn) time is common in recursive sorting algorithms, sorting algorithms using a binary tree sort and most other types of sorts. The above quicksort algorithm runs in O(nlogn) time despite using O(logn) space.

Why time complexity of binary search tree is log n?

The beauty of balanced Binary Search Trees (BSTs) is that it takes O(log n) time to search the tree. Why is this? As the number of inputted elements increase, the number of operations stays the same for O(log n). With a balanced BST, we are always halving the number of elements that we look at.

Is binary search O Logn?

Binary Search is a searching algorithm used in a sorted array by repeatedly dividing the search interval in half. The idea of binary search is to use the information that the array is sorted and reduce the time complexity to O(Log n).


1 Answers

What you have is exactly backwards. O(lg N) generally means some sort of divide and conquer algorithm, and one common way of implementing divide and conquer is a binary tree. While binary trees are a substantial subset of all divide-and-conquer algorithms, the are a subset anyway.

In some cases, you can transform other divide and conquer algorithms fairly directly into binary trees (e.g. comments on another answer have already made an attempt at claiming a binary search is similar). Just for another obvious example, however, a multiway tree (e.g. a B-tree, B+ tree or B* tree), while clearly a tree is just as clearly not a binary tree.

Again, if you want to badly enough, you can stretch the point that a multiway tree can be represented as sort of a warped version of a binary tree. If you want to, you can probably stretch all the exceptions to the point of saying that all of them are (at least something like) binary trees. At least to me, however, all that does is make "binary tree" synonymous with "divide and conquer". In other words, all you accomplish is warping the vocabulary and essentially obliterating a term that's both distinct and useful.

like image 157
Jerry Coffin Avatar answered Sep 28 '22 00:09

Jerry Coffin