I was building a queue using a BST, but I realized that a min/max heap might be better. However, a BST might work, since if we store a reference to head/tail in the BST, then lookup is very close to O(1)..for example:
(5)
/ \
(3) (6)
/ \ \
(2) (4) (7)
/
(1)
if we have a reference to head element = (1), then if (1) has no right child, that its parent (2) is next smallest. Otherwise, if (1) has a right child, then the distance to find that right child should only be 1 or 2 elements.
So I have some questions:
- is finding min/max in a BST considered O(1)?
- is there an advantage to building a queue with a min heap or max heap that a BST cannot accomplish?
- Is a minmax-heap the same as a BST?
I have some requirements:
- find smallest (and ideally also largest) in O(1)
- be able to insert/delete from middle of queue in <= log(n) time
Somehow, heaps seem to be implemented with arrays underneath, but I don't understand how to do inserts/deletes from middle of structure in O(1) time.