## What's the difference between a binary search tree and a binary heap?

65

30

These two seem very similar and have almost an identical structure. What's the difference? What are the time complexities for different operations of each?

41

Heap just guarantees that elements on higher levels are greater (for max-heap) or smaller (for min-heap) than elements on lower levels, whereas BST guarantees order (from "left" to "right"). If you want sorted elements, go with BST.by Dante is not a geek

Heap is better at findMin/findMax (O(1)), while BST is good at all finds (O(logN)). Insert is O(logN) for both structures. If you only care about findMin/findMax (e.g. priority-related), go with heap. If you want everything sorted, go with BST.

by xysun

According to this video, you can have larger values on a lower level, as long as the larger is not descendant of the lower one.

– whoan – 2017-06-21T00:08:09.010

I think BST is better in findMin & findMax http://stackoverflow.com/a/27074221/764592

– Yeo – 2014-11-22T05:50:19.717

5

I think this is just a common misconception. A binary tree can be easily modified to find min and max as pointed by Yeo. This is actually a restriction of the heap: the only efficient find is min or max. The true advantage of the heap is O(1) average insert as I explain: http://stackoverflow.com/a/29548834/895245

– Ciro Santilli 包子露宪 六四事件 法轮功 – 2015-06-20T05:01:02.650

24

Both binary search trees and binary heaps are are tree-based data structures.

Heaps require the nodes to have a priority over their children. In a max heap, each node's children must be less than itself. This is the opposite for a min heap:

Binary search trees (BST) follow a specific ordering (pre-order, in-order, post-order) among sibling nodes. The tree must be sorted, unlike heaps:

BST have average of $O(\log n)$ for insertion, deletion, and search.
Binary Heaps have average $O(1)$ for findMin/findMax and $O(\log n)$ for insertion and deletion.

1

Sir, correct me if I'm wrong but the heap example you gave (the 1st figure) looks incorrect. AFAIK, heaps are defined as Heap just guarantees that elements on higher levels are greater (for max-heap) or smaller (for min-heap) than elements on lower levels as Ali786 said in his example. In your maxheap, if you look closely, 25 is not less than 19, or 7 is not less than 3, since they are on lower levels.

– Burak. – 2016-11-24T01:57:22.237

@Burak I still don't know if that tree is correct. According to this video, that would be correct.

– whoan – 2017-06-20T23:45:11.923

5

Welcome to [cs.SE]! Note that you can use LaTeX here to typeset mathematics in a more readable way. See here for a short introduction.

– FrankW – 2014-06-20T07:17:17.677

Insertion into binary heaps is $O(\log n)$, not $O(1)$. What is $O(1)$ is extract_min/extract_max. – FrankW – 2014-06-20T07:18:12.307

17

Summary

          Type      BST (*)   Heap
Insert    average   log(n)    1
Insert    worst     log(n)    log(n)
Find any  worst     log(n)    n
Find max  worst     1 (**)    1
Create    worst     n log(n)  n

• *: everywhere in this answer, BST == Balanced BST, since unbalanced sucks asymptotically
• **: using a trivial modification explained in this answer

Advantages of binary heap over a BST

Advantage of BST over binary heap

• search for arbitrary elements is O(log(n)). This is the killer feature of BSTs.

For heap, it is O(n) in general, except for the largest element which is O(1).

"False" advantage of heap over BST

• heap is O(1) to find max, BST O(log(n)).

This is a common misconception, because it is trivial to modify a BST to keep track of the largest element, and update it whenever that element could be changed: on insertion of a larger one swap, on removal find the second largest. https://stackoverflow.com/questions/7878622/can-we-use-binary-search-tree-to-simulate-heap-operation (mentioned by Yeo).

Actually, this is a limitation of heaps compared to BSTs: the only efficient search is that for the largest element.

Average binary heap insert is O(1)

Sources:

Intuitive argument:

• bottom tree levels have exponentially more elements than top levels, so new elements are almost certain to go at the bottom
• heap insertion starts from the bottom, BST must start from the top

In a binary heap, increasing the value at a given index is also O(1) for the same reason. But if you want to do that, it is likely that you will want to keep an extra index up-to-date on heap operations https://stackoverflow.com/questions/17009056/how-to-implement-ologn-decrease-key-operation-for-min-heap-based-priority-queu e.g. for Dijikstra. Possible at no extra time cost.

BST cannot be efficiently implemented on an array

Heap operations only need to bubble up or down a single tree branch, so O(log(n)) worst case swaps, O(1) average.

Keeping a BST balanced requires tree rotations, which can change the top element for another one, and would require moving the entire array around (O(n)).

Philosophy

• BSTs maintain a global property between a parent and all descendants (left smaller, right bigger).

The top node of a BST is the middle element, which requires global knowledge to maintain (knowing how many smaller and larger elements are there).

This global property is more expensive to maintain (log n insert), but gives more powerful searches (log n search).

• Heaps maintain a local property between parent and direct children (parent > children).

The top note of a heap is the big element, which only requires local knowledge to maintain (knowing your parent).

A doubly linked list can be seen as subset of the heap where first item has greatest priority, so let's compare them here as well:

• insertion:
• position:
• doubly linked list: the inserted item must be either the first or last, as we only have pointers to those elements.
• binary heap: the inserted item can end up in any position. Less restrictive than heap.
• time:
• doubly linked list: O(1) worst case we have pointers to the items, and the update is really simple
• binary heap: O(1) average, thus worse. Tradeoff for having more general insertion position.
• search: O(n) for both

An use case for this is when the key of the heap is the current timestamp: in that case, new entries will always go to the beginning of the list. So we can even forget the exact timestamp altogether, and just keep the position in the list as the priority.

This can be used to implement an LRU cache. Just like for heap applications like Dijkstra, you will want to keep an additional hashmap from the key to the corresponding node of the list, to find which node to update quickly.

Similar question on SO: https://stackoverflow.com/questions/6147242/heap-vs-binary-search-tree-bst

Great answer. Common application of heap are median, k min, top k elements. For these most common operation remove min then insert(usually we have small heap with few pure insert operations). So seems like in practice, for these algorithms it does not outperform BST. – yura – 2016-06-19T19:25:05.247

11

With data structure one has to distinguish levels of concern.

1. The abstract data structures (objects stored, their operations) in this question are different. One implements a priority queue, the other a set. A priority queue is not interested in finding an arbitrary element, only the one with largest priority.

2. The concrete implementation of the structures. Here on first sight both are (binary) trees, with different structural properties though. Both the relative ordering of the keys and the possible global structures differ. (Somewhat imprecise, in a BST keys are ordered left-to-right, in a heap they are ordered top-down.) As IPlant correctly remarks an heap should also be "complete".

3. There is a final difference in the low level implementation. A (unbalanced) binary search-tree has a standard implementation using pointers. A binary heap to the contrary has an efficient implementation using an array (precisely because of the restricted structure).

1

On top of the previous answers, the heap must have the heap structure property; the tree must be full, and the bottom most layer, which cannot always be full, must be filled leftmost to rightmost with no gaps.