Git Product home page Git Product logo

heap's Introduction

Heap Implementation in C++

This is an implementation of a binary min-heap.
Heaps maintain an implicit binary tree structure in an array (I used the STL vector so that we do not have to specify an array size at creation time).
Min-heap property: All nodes are less than or equal of its children. Thus, the minimum key always sits at the top of the heap.

Usage

Compile:

$ g++ heap.cpp example.cpp

Run:

$ ./a.out

Tests

Compile (needs googletest header files):

$ make

Run:

$ ./unittest.out

You should see that all tests have PASSED.

##Operations n = number of items in the heap

  • Insert - O(log n): add a new item to the heap
  • Extract min - O(log n): remove an item with minimum value
  • Delete - O(log n): remove an item from the heap
  • bubbleUp/bubbleDown - O(log n): rearrage heap to maintain its property
  • Heapsort - O(n log n): calls extract-min n items to sort the heap
  • Make heap - O(n): given an unsorted array, each element for n elements has to be added into the internal queue and bubbleDown O(log n). Not all heapify operations are O(log n), this is why you are getting O(n).

Applications:

  • Event manager
  • Median maintenance
  • Speeding up Dijkstra's shortest-path algorithm with n vertices and m edges.
    O(nm) => O(m log n). n loop iteations, m work per iteration (linear scan through edges for min computations)

Google C++ Testing

Note that I have included the Google Test object file (gtest_main.a) so that you do not need to re-build Google's C++ test framework. The object file is built against version 1.7.

If you wish to re-build another version, you may download the test framework at http://code.google.com/p/googletest/

Move the gtest folder somewhere permenant, e.g. ~/Documents.
Create a symlink to the gtest folder where Makefile resides.

> ln -s ~/Documents/lib/gtest-1.7.0 gtest  

To use it, comment out the .INTERMEDIATE line in the Makefile.
This will build the gtest object file located at GTEST_DIR.
Replace $(GTEST_OBJECT) with $(GTEST_HEADERS)

Random Trivia

max-heap vs sorted array:

  1. Find the maximum element quickly:
    O(1) for both. Root of max-heap and last element in array.
  2. Delete an element quickly:
    O(log n) for max-heap. O(n) for array without the element index, otherwise, O(1).
  3. Form the structure quickly:
    O(n) to build a heap, O(n log n) to sort an array.
  4. Find the minimum element quickly:
    O(log n) to find the min element. O(1) for the array as the first element.

heap's People

Contributors

alyssaq avatar

Stargazers

Ionut Radu Barbalata avatar Ricardo Morato Rocha avatar Branden Pinney avatar Yanshu WANG avatar Arseny Ravin avatar Shawn(Shaofeng) Jiang avatar LiuYang avatar Tom Lin avatar Paave Kevle avatar

Watchers

James Cloos avatar  avatar edfeff avatar

heap's Issues

Heap::find() is not O(log n)

"int Heap::find(unsigned int idx, int val) const { //O(log n)"

The function is not O(log n) and AFAIU cannot be done in O(log n).
Take for example
1 2 3 4 5 6 7
and find(7).
It will go through all the n nodes. So it is O(n).

Accordingly, same for the documentation of delete.

Remove doesn't work correctly

int main()
{
int A[] = {3, 70, 4, 80,90,5,6,82,84,91,92,7,8};
Heap h;
h.makeHeap(A, 13);
cout << h.toString() << endl;
h.remove(80);
cout << h.toString() << endl;
return 0;

}

3 70 4 80 90 5 6 82 84 91 92 7 8
3 70 4 8 90 5 6 82 84 91 92 7 // It's not heap any more

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.