3

Can you design a data structure just like a queue which contains 'enqueue', 'dequeue' 'minimum' and 'maximum'? I know a method to make a queue using 2 stacks to find the minimum and maximums respectively, but how can I get both simultaneously?

Thanks

2
  • Finding minimum and maximum in any given stack or queue requires a big O of n. This means you have to do a full iteration of the stack. Commented Sep 17, 2012 at 3:17
  • 3
    This might help set you on the right path. stackoverflow.com/questions/4077101/… Commented Sep 17, 2012 at 4:44

5 Answers 5

3

Using standard containers, a completely ordered data structure like a std::set will provide access to both extrema, e.g. using *s.begin() and *s.rbegin(). If you have multiple objects of the same priority, you might want to break ties in an arbitrary fashion, or use a std::multiset instead.

Most implementations will probably use some form of red-black tree for the implementation of such a set. As the data structure will be kept sorted at all times, asymptotic performance might be worse than what a regular single-ended heap-based priority queue would give you, but for many applications the difference isn't crucial, and so the effort of implementing a custom data structure should be avoided.

Sign up to request clarification or add additional context in comments.

Comments

1

Using Priority Queue!

C++ STL

#include<queue> 

Usually, priority queue is implemented by Binary-Heap. But it cannot maintain the maximum value and minimum value simultaneously. @_@

Maybe Balanced Search Tree, such as AVL, Splay, or Red-Black Tree should be a better choice.

Comments

1

Usually a priority queue is implemented using some kind of heap structure. There is a variation called a min-max heap which allows for constant-time access to both the maximum and the minimum element. There already is a question asking for C++ implementations of such a min-max heap. Its answers should prove useful to you as well.

This comment by Paul Dixon first mentioned min-max heaps, whereas this comment by Chris Mansley pointed out the implementation question on Stack Overflow.

Comments

0

The data structure is a well known structure called a Priority Queue. Each element in the queue has an associated priority, and the queue is kept in high to low sorted order.

Each element in the queue must carry a a priority, and the code must maintain the order contract.

Some STLs contain Priority Queue Implementations, though now that i look at it i am uncertain if the whole queue is kept in priority order.

4 Comments

Unfortunately, I believe that priority queues are set up to enable you to pick off the highest remaining element cheaply. Most implementations don't give you access to the lowest element. See en.cppreference.com/w/cpp/container/priority_queue for a description of the STL interface.
A min-max heap can access the largest and smallest elements in constant time en.wikipedia.org/wiki/Min-max_heap
+1 to @PaulDixon. a regular std::priority_queue<> will NOT do what he wants on all bases.
@PaulDixon, I suggest turning your comment into a separate answer. So far we don't have an answer here which suggests min-max heaps in its body text.
0

You can store both the minimum stack and the maximum stack at once - and so on for each stack. This is victory. You can also make an implementation with two decks and one queue. Push into the queue: we push into the normal one, from the beginning of the deck of minimums we remove elements that are larger than the one we threw in (maybe all the elements of the deck). Then we put our element at the beginning of the deck. Pop from the queue: see if our element is equal to the minimum in the deck (and the minimum will always be at the end of the deck). If it is equal, then we also remove it from the deck, otherwise we leave it. Same with the maximum.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.