I need to construct a data structure that uses only O(n) bits of storage. The worst time complexity of insert, delete, and maximum needs to be O(log n) but it needs to be O(1) for contains. I have been trying to use a binary heap with only 1s and 0s (to satisfy the O(n) bits of storage) but I can't seem to get far with the maximum and contains functions (on how their worst time complexity looks). Can anyone give me a clue on where I'm going wrong? Thank you.
Asked
Active
Viewed 40 times
0
-
for binary heap you cannot achieve `O(1)` lookup time – Dimitar Jan 28 '16 at 00:39
-
how about using an array of only 1s and 0s? – Daniel Cook Jan 28 '16 at 00:41
-
if they are in a heap, yes. Let top be max (1) then if left is 0(at `A[1]`), it contains both 1,0. Analogous for min. In general you will need then only `A[0] == A[1]` to check. But it doesn't make much sense to me to sort only bare 1s and 0s. – Dimitar Jan 28 '16 at 00:48
1 Answers
0
Have two data structures working tandem: a balanced BST (such as AVL tree) and a hash table. Inserting an element takes O(log(n)) time for the BST, O(1) time for the hash table, so O(log(n)) time total. Deletion takes O(log(n)) time for BST, O(1) time for hash table. Maximum takes O(log(n)) time for the BST, and once you know which element is the maximum, takes O(1) time for the hash table. Contains takes O(1) time for the hash table (after which theres no need to check the BST, since they contain the same elements). Actually implementing it would be difficult because you'd need to keep pointers between elements in the BST and the hash table, but this structure achieves the required specifications.

ghui
- 156
- 3
- 12