0

I'm wondering, can there exists such a data stucture under the following criterions and times(might be complicated)?

if we obtain an unsorted list L an build a data structure out of it like this:

  • Build(L,X) - under O(n) time, we build the structure S from an unsorted list of n elements
  • Insert (y,S) under O(lg n) we insert z into the structure S
  • DEL-MIN(S) - under O(lg n) we delete the minimal element from S
  • DEL-MAX(S) - under O(lg n) we delete the maximal element from S
  • DEL-MId(S) - under O(lg n) we delete the upper medial(ceiling function) element from S

the problem is that the list L is unsorted. can such a data structure exist?

user unknown
  • 35,537
  • 11
  • 75
  • 121
  • How could you *build* a datastructure in sublinear time? What properties are you assuming that this list has? Is it a linked list? Something else? – John Coleman Apr 21 '18 at 16:13
  • the list contains numbers, in an unsorted way. if it was a linked list, will that be possible? i know that it's complicated, that's why i'm wodering if it can exist – BeginningMath Apr 21 '18 at 16:20
  • Of course not. You can't put items into a data structure without processing the items. Your question is sort of like "I want to do a million things, can I do a million things by doing less than a million things?" – John Coleman Apr 21 '18 at 16:23
  • @JohnColeman let's do it slowly, case by case. what is the best way to implement Build(L,X) (under O(n)) and Insert (y,S) (under O(log n))? – BeginningMath Apr 21 '18 at 20:03
  • 1
    I hope "under O(.)" means "in O(.)". Under this premise, a balanced binary search tree which keeps the sizes of subtrees is just such a data structure. In fact, it is possible with only initial comprehending effort with some popular languages: for example, one can construct such a tree from [this GCC extension](https://gcc.gnu.org/onlinedocs/libstdc++/ext/pb_ds/tree_based_containers.html) of C++ standard library. – Gassa Apr 22 '18 at 07:38
  • @Gassa you can’t build a balanced binary search tree in O(n) time. – Paul Hankin Apr 22 '18 at 12:45
  • @PaulHankin Hmm right, I can but only when the items are already sorted, otherwise it would be comparison-based sorting in O(n). For unsorted data, it is O(n log n). Thanks for the catch! Heaps are the way to go then, as in your answer. – Gassa Apr 22 '18 at 13:07

1 Answers1

2

DEL-MIN and DEL-MAX are easy: keep a min-heap and max-heap of all the elements. The only trick is that you have to keep indices of the value in the heap so that when (for example) you remove the max, you can also find it and remove it in the min-heap.

For DEL-MED, you can keep a max-heap of the elements less than the median and a min-heap of the elements greater than or equal to the median. The full description is in this answer: Data structure to find median. Note that in that answer the floor-median is returned, but that's easily fixed. Again, you need to use the cross-indexing trick to refer to the other datastructures as in the first part. You will also need to think how this handles repeated elements if that's possible in your problem formulation. (If necessary, you can do it by storing repeated elements as (count, value) in your heap, but this complicates rebalancing the heaps on insert/remove a little).

Can this all be built in O(n)? Yes -- you can find the median of n things in O(n) time (using the median-of-median algorithm), and heaps can be built in O(n) time.

So overall, the datastructure is 4 heaps (a min-heap of all the elements, a max-heap of all the elements, a max-heap of the floor(n/2) smallest elements, a min-heap of the ceil(n/2) largest elements. All with cross-indexes to each other.

Paul Hankin
  • 54,811
  • 11
  • 92
  • 118
  • You don't need separate min-heap and max-heap for DEL-MIN and DEL-MAX. There's a [min-max heap](https://en.wikipedia.org/wiki/Min-max_heap). – Jim Mischel Apr 24 '18 at 12:56