Re: Heap vs BST
- From: "Googmeister" <googmeister@xxxxxxxxx>
- Date: 23 Dec 2005 11:08:58 -0800
alex.gman@xxxxxxxxx wrote:
> sillybanter@xxxxxxxxx wrote:
> > alex.gman@xxxxxxxxx wrote:
> >
> > > Ok, if I cache max of a BST, then get_max is O(1), and my BST behaves
> > > like a Heap, but also provides the usual extras. Makes me wonder, what
> > > are the advantages of Heaps over BSTs now, after get_max is no longer
> > > one of them?
> >
> > Are you going to keep your BST balanced?
>
> Yes
>
> > If so, then the code is much
> > more complex. Compare an AVL implementation (insert with rebalance
> > would probably be 100+ lines of code) to a heap (insert can be done in
> > about 5-6 lines of code).
>
> I don't think this matters much, because this is library code. Written
> once, used by many people many times.
>
> > Add to that the fact that balanced BST
> > require pointers or indices to indicate parent/child relationships,
> > whereas a heap can be thrown into an array with implied (or computed)
> > parent/child relationships, and you see that heaps are in fact quite a
> > bit more space efficient.
> Firstly, there are pluses and minuses to using "pointers" instead of
> storing everything in an array. Arrays are difficult to grow.
Right, with binary heap (the standard array implementation), you
get O(log n) amortized per op. It's only an amortized bound since
you need to grow the array periodically, say by repeated doubling.
> Secondly, why shouldn't I put my AVL tree onto an array as well, and
> use the same parent/children formula?
The parent/child formula works on binary heaps because the
tree is perfectly balanced. Your AVL tree won't stay perfectly
balanced.
.
- Follow-Ups:
- Re: Heap vs BST
- From: Paul E. Black
- Re: Heap vs BST
- From: Gene
- Re: Heap vs BST
- References:
- Heap vs BST
- From: alex . gman
- Re: Heap vs BST
- From: sillybanter
- Re: Heap vs BST
- From: alex . gman
- Heap vs BST
- Prev by Date: Re: Heap vs BST
- Next by Date: Re: Heap vs BST
- Previous by thread: Re: Heap vs BST
- Next by thread: Re: Heap vs BST
- Index(es):
Relevant Pages
|
|