Code Monkey home page Code Monkey logo

max-heap's Introduction

Implementation of Max Heap data structure in C++ using dynamic arrays.

The following methods are available in the public interface of MaxHeap

-> MaxHeap() : default constructor

-> MaxHeap(int _capacity) : assigns a capacity of given size to the heap.

-> void insert(k const key, v const value) : insert a new <key,value> pair in the heap. The key will be used to maintain heap ordering.

-> void getMax(v & value) : returns the value whose key is maximum via the parameter passed by reference.

-> void deleteMax() : deletes the heap item whose key is maximum

-> bool isEmpty() const : returns true if the heap has no item, else false

-> void deleteAll() : completely destroys the heap. The capacity is set to 0. and the array in the heap is set to nullptr.

-> ~MaxHeap() : destructor

The following private methods are available:

-> void doubleCapacity() : doubles the capacity of heap. For example, if the current capacity of heap is 10, then after calling this function, the capacity will become 20. This method is used by insert method to increase the capacity when the heap is full.

-> void shiftUp(int index) : It is a recursive method that, in a single recursive call, shifts the item stored at given index one step up if its key is greater than the key of its parent. Insert method uses this method to recursivley shift the new item to the root if its key is maximum.

-> void shiftDown(int index) : It is a recursive method that, in a single recursive call, shifts the item stored at given index one step down if its key is smaller than the key of any of its children. deleteMax method uses this method. To delete the max item (the one at index 0), deleteMax method first moves the last item to index 0, and then it calls shiftDown method, starting from index 0, to recursively move down the last item to its appropriate location.

max-heap's People

Contributors

bigwheel92 avatar

Stargazers

 avatar  avatar

Watchers

 avatar  avatar

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.