Construction of BST - data-structures

http://www.geeksforgeeks.org/group-multiple-occurrence-of-array-elements-ordered-by-first-occurrence/
Please check this question.
How to do BST method of this problem.
They have mentioned that total time complexity will be O(NLogN).
How is time complexity of tree is LogN for traversal?
Please help

search, delete and insert running time all depend on the height of tree, or O(h) for BST. A degenerate tree almost looks like a linked list can produce a running time of O(N).
On the other hand, consider a self-balancing tree such as AVL tree, the running time for lookup is lower bounded by O(logN) because like Binary Search, we divide the search space by half each time as in left and right subtree have almost identical height.

Related

What is the Big-O complexity of a general tree?

What I mean by general tree is an unbalanced tree with multiple child nodes (not restricted to 2 child node for each branch like Binary Tree). What is the Big-O complexity of remove node, insert node, find node
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).
If you're talking about a regular k-ary tree that does nothing special with its data, then to find any one node in the tree would take O(n) time assuming there are n nodes.
Inserting a node would be O(1) since you can store it wherever you want, and removing a node would be O(n) since you'd have to look at every node (worst case) to find the one to delete, and since there's no order to the data you don't have to do anything with the rest of the nodes.

Time complexity of binary search in a slightly unbalanced binary tree

The best case running time for binary search is O(log(n)), if the binary tree is balanced. The worst case would be, if the binary tree is so unbalanced, that it basically represents a linked list. In that case the running time of a binary search would be O(n).
However, what if the tree is only slightly unbalanced, as is teh case for this tree:
Best case would still be O(log n) if I am not mistaken. But what would be the worst case?
Typically, when we say something like "the cost of looking up an element in a balanced binary search tree is O(log n)," what we mean is "in the worst case, we have to do O(log n) work in the course of performing a search on a balanced binary search tree." And since we're talking about big-O notation here, the previous statement is meant to be taken about balanced trees in general rather than a specific concrete tree.
If you have a specific BST in mind, you can work out the maximum number of comparisons required to find any element. Just find the deepest node in the tree, then imagine searching for a value that's bigger than that value but smaller than the next value in the tree. That will cause you to walk all the way down the tree as deeply as possible, making the maximum number of comparisons possible (specifically, h + 1 of them, where h is the height of the tree).
To be able to talk about the big-O cost of performing lookups in a tree, you'd need to talk about a family of trees of different numbers of nodes. You could imagine "kinda balanced" trees whose depth is Θ(√n), for example, where lookups would take time O(√n), for example. However, it's uncommon to encounter trees like that in practice, since generally you'd either (1) have a totally imbalanced tree or (2) use some sort of balanced tree that would prevent the height from getting that high.
In a sorted array of n values, the run-time of binary search for a value, is
O(log n), in the worst case. In the best case, the element you are searching for, is in the exact middle, and it can finish up in constant-time. In the average case too, the run-time is O(log n).

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

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?
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).
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).
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.
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).

Time complexity in BST

In what situation, searching a term using a binary search tree requires a time complexity that is linear to the size of the term vocabulary (say M)? How to ensure a worst time complexity of log M?
A complete binary tree is one for which every level, except possibly the last, is completely filled. The worst case search peformance is the height of the tree, which in this case would be O(lgM), assuming M vocabulay terms in the tree.
One way to ensure this performance would be to use a self-balancing tree, e.g. a red-black tree.
Since binary search is a divide-and-conquer algorithm, we can ensure O(log M) if the tree is balanced, with equal number of terms under the sub-trees of any node. O(log M) basically means that time goes up linearly while M goes up exponentially. If it takes 1 second to search a balanced binary tree that is 10 nodes, it’d take 2 seconds to search an equally balanced tree with 100 nodes, 3 seconds to search 1000 nodes, and so on.
But if the binary search tree is extremely unbalanced to the point where it looks a lot like a linked list, we would have to go through every node, requiring a time complexity that is linear to M.

Complexity of balancing an unbalanced/partially balanced BST?

In an AVL tree, it takes a constant number of single and double rotations every time we rebalance on insertion and deletion since we only have to check the path from point of insertion or deletion to the root.
If we had an unbalanced tree, we would have to check if every possible node is balanced, so it would cost O(n) to rebalance an unbalanced tree. Is this correct?
It does take time O(n) to rebalamce an unbalanced tree, but not for the reason you mentioned. In an AVL tree, insertions and deletions may require Θ(log n) rotations if the tree was maximally imbalanced before the element was inserted. This could potentially require O(n log n) time to rebalance the tree, since you might do O(log n) work per each of the n nodes.
However, using other algorithms, you can rebalance a tree in O(n) time. One simple option is to do an inorder traversal of the tree to get the elements in sorted order, then reconstruct an optimal BST from those elements by recursively building the tree bottom-up. Alternatively, you can use the Day-Stout-Warren algorithm, which balances any tree in O(n) time and O(1) space.
Hope this helps!

Resources