Java – why use heaps instead of binary trees when implementing priority queues?
In my opinion, the only advantage of heap over binary tree is to find the smallest item in heap with O (1) rather than o (log (2) n) complexity in binary tree
When implementing priority queues, you need to delete each smallest item from the data structure The smallest item is deleted from the tree, and both heaps are completed with O (log (2) n) complexity Altough removing items from the tree can be more complex Deleting items without children is very simple
My question is why the heap is used instead of the binary tree when implementing the priority queue (which is simpler in this case)?
Solution
When the binary tree converges to the array, the worst-case complexity in the binary tree case is O (n), while it remains o (log (n)) in the heap You can use a balanced binary tree, such as red black or AVL, but then it becomes more complex and requires more memory