Heaps are either a type of tree
or an array
/list
data structure.
A heap can be implemented with several data structures; usually, you would use either an array/list or a tree structure.
A heap is useful for storing information in an order where you're assured the highest value (or, when used for things like a priority queue, the highest priority) is always the root/top node.
Heaps really excel at insert and removal. They are O(log n)
for time complexity. Searching them is O(n)
because you are not guaranteed to have your nodes in order like in a true binary search tree.
The space complexity is O(n)
where n
is the number of nodes.