CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    Aug 2010
    Posts
    1

    Why heaps exist when we have trees

    Any ideas about a serious advantage of a heap over a balanced BST?

    I see that heap can be implemented in an array, and the code is simpler. Fine, that's something. When only heap is needed, it'll be somewhat more efficient, but not asimptoticaly.

    Does anybody see any qualitive advantage of heaps over balanced BSTs?

    (If we cache the maximum in a BST we have the same O(1) getMax operation. )

    Thanks

  2. #2
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: Why heaps exist when we have trees

    Advantage over a balanced BST in what situation?

    For example, suppose you intend to sort an array in place with O(n log n) worst case for a comparison based sort. That's a seriously impossible-to-beat advantage of a heap compared to a balanced BST

    This example actually is quite realistic, e.g., introsort, a hybrid comparison based sorting algorithm that does see actual use in libraries, uses quicksort, but switches to heap sort when it detects that quicksort may hit a worst case scenario.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured