Can we use binary search tree to simulate heap ope

2020-03-01 15:53发布

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 there any kind of benefits for doing so?

4条回答
够拽才男人
2楼-- · 2020-03-01 16:36

Yes, we can, by simply inserting and finding the minimum into the BST. There are few benefits, however, since a lookup will take O(log n) time and other functions receive similar penalties due to the stricter ordering enforced throughout the tree.

查看更多
我只想做你的唯一
3楼-- · 2020-03-01 16:37

Sure we can. but with a balanced BST.

The minimum is the leftest element. The maximum is the rightest element. finding those elements is O(logn) each, and can be cached on each insert/delete, after the data structure was modified [note there is room for optimizations here, but this naive approach also doesn't contradict complexity requirement!]

This way you get insert,delete: O(logn), findMin/findMax: O(1)

EDIT:
The only advantage I can think of in this implementtion is that you get both findMin,findMax in one data structure.
However, this solution will be much slower [more ops per step, more cache misses are expected...] and consume more space then the regular array-based implementation of a heap.

查看更多
姐就是有狂的资本
4楼-- · 2020-03-01 16:47

Basically, I agree with @amit answer. I will elaborate more on the implementation of this modified BST.

Heap can do findMin or findMax in O(1) but not both in the same data structure. With a slight modification, the BST can do both findMin and findMax in O(1).

In this modified BST, you keep track of the the min node and max node every time you do an operation that can potentially modify the data structure. For example in insert operation you can check if the min value is larger than the newly inserted value, then assign the min value to the newly added node. The same technique can be applied on the max value. Hence, this BST contain these information which you can retrieve them in O(1). (same as binary heap)

In this BST (specifically Balanced BST), when you pop min or pop max, the next min value to be assigned is the successor of the min node, whereas the next max value to be assigned is the predecessor of the max node. Thus it perform in O(1). Thanks to @JimMischel comment below however we need to re-balance the tree, thus it will still run O(log n). (same as binary heap)

In my opinion, generally Heap can be replaced by Balanced BST because BST perform better in almost all of the heap data structure can do. However, I am not sure if Heap should be considered as an obsolete data structure. (What do you think?)

PS: Have to cross reference to different questions: https://stackoverflow.com/a/27074221/764592

查看更多
放我归山
5楼-- · 2020-03-01 16:55

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.

查看更多
登录 后发表回答