- Binary search in O(logn) time.
- Select element given ith order statistic in O(1)
- Computing min/max of array - O(1)
- Predecessor/Successor - O(1), just find that element and return one before/after it
- Rank - how many keys stored are less than or equal to the key - O(logn)
- Output in sorted order - O(n)
Simply using a sorted array would be unacceptable for insertion/deletion because it could use O(n) time. This is where the binary search tree comes in! A BST is like a sorted array, but with logarithmic insertion and deletion. Now, the operations:
- Binary search - O(log n)
- Select - O(log n)
- Min/max - O(log n)
- Pred/succ - O(log n)
- Rank - O(log n)
- Output in sorted order - O(n)
- Insert/Deletion - O(log n)
If you only need min/max, you should use a heap instead. If you only need fast insertion and lookup, use a hash table instead.
No comments:
Post a Comment