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

Question Detail: 

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

Asked By : piperchester
Best Answer from StackOverflow

Question Source : http://cs.stackexchange.com/questions/27860

Answered By : Ali786

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

No comments

Powered by Blogger.