An open API service indexing awesome lists of open source software.

https://github.com/mr-g254/application-programming-heaps


https://github.com/mr-g254/application-programming-heaps

Last synced: about 2 months ago
JSON representation

Awesome Lists containing this project

README

        

**Heap Data Structure Implementation in Java**

**Introduction**

A heap is a binary tree-based data structure that adheres to a specific heap property. It is widely used in various algorithms and applications like priority queues, heap-sort, and Dijkstra's algorithm. The main idea behind a heap is that each parent node has a specific relationship with its children, which is used to maintain an efficient structure for inserting, deleting, and accessing the root element.

**Max-Heap vs Min-Heap**

*Max-Heap*: In a max-heap, the value of each parent node is greater than or equal to the values of its children. This ensures that the largest element is always at the root. Max-heaps are commonly used for max-priority queues.

*Min-Heap*: In a min-heap, the value of each parent node is less than or equal to the values of its children. This ensures that the smallest element is always at the root. Min-heaps are commonly used for min-priority queues.

Heaps are often represented as binary trees, and these binary trees are typically stored in an array for efficient access.

**Key Operations**

This repository provides a Java implementation of both Max-Heap and Min-Heap. Below are the key heap operations and their time complexities:

1. Insert:

This operation adds a new element to the heap.
The new element is initially added at the end of the tree (the next available position in the array). The heap property is then restored by "bubbling up" the new element.

Time Complexity: O(log n), where n is the number of elements in the heap.

2. Extract:

This operation removes and returns the root element (either the maximum or minimum, depending on whether it's a max-heap or min-heap). After removing the root, the last element in the heap is moved to the root position, and the heap property is restored by "bubbling down" this element.

Time Complexity: O(log n), where n is the number of elements in the heap.

4. Heapify:

This operation is used to restore the heap property of a subtree. Given a node, heapify ensures that the parent node satisfies the heap property relative to its children. This process is repeated recursively for all nodes in the tree.

Time Complexity: O(n), where n is the number of elements in the heap. This operation is typically used to build a heap from an unsorted array.

Heaps are an essential data structure for various algorithms and are particularly useful for implementing priority queues. This repository provides a clear implementation of Max-Heap and Min-Heap in Java, along with examples of priority queue interfaces.

*GROUP 8*

VANESSA WAMBUI-SCT212-0108/2022

EVE MAINA-SCT212-0109/2022

EZEKIEL GIKUHI-SCT212-0112/2022

MARK OGARI-0715/2022

SALEM MAINA-0167/2022