Can we use binary search tree to simulate heap operation?

后端 未结 4 715
失恋的感觉
失恋的感觉 2021-01-04 13:59

I was wondering if we can use a binary search tree to simulate heap operations (insert, find minimum, delete minimum), i.e., use a BST for doing the same job?

Are t

4条回答
  •  再見小時候
    2021-01-04 14:31

    Yes, but you lose the O(1) average insert of the heap

    As others mentioned, you can use a BST to simulate a heap.

    However this has one major downside: you lose the O(1) insert average time, which is basically the only reason to use the heap in the first place: https://stackoverflow.com/a/29548834/895245

    If you want to track both min and max on a heap, I recommend that you do it with two heaps instead of a BST to keep the O(1) insert advantage.

提交回复
热议问题