Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/Canhassi12/binary-heap
Binary heap studies in Rust
https://github.com/Canhassi12/binary-heap
Last synced: 8 days ago
JSON representation
Binary heap studies in Rust
- Host: GitHub
- URL: https://github.com/Canhassi12/binary-heap
- Owner: Canhassi12
- Created: 2023-03-13T19:29:22.000Z (over 1 year ago)
- Default Branch: main
- Last Pushed: 2023-04-22T15:57:34.000Z (over 1 year ago)
- Last Synced: 2024-08-01T21:56:12.744Z (4 months ago)
- Language: Rust
- Homepage:
- Size: 62.5 KB
- Stars: 18
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# binary-heap
## What is?
A binary heap is a heap data structure that takes the form of a binary tree.
## Characteristics
- Shape property: a binary heap is a complete binary tree; that is, all levels of the tree, except possibly the last one (deepest) are fully filled, and, if the last level of the tree is not complete, the nodes of that level are filled from left to right.
- Heap property: the key stored in each node is either greater than or equal to (≥) or less than or equal to (≤) the keys in the node's children, according to some total order.
## Structure
Heaps are commonly implemented with an array. Any binary tree can be stored in an array, but because a binary heap is always a complete binary tree, it can be stored compactly.
Let n be the number of elements in the heap and i be an arbitrary valid index of the array storing the heap. If the tree root is at index 0, with valid indices 0 through n − 1, then each element a at index i has:
- Indice 2i + 1 to left children and 2i + 2 to the right children.
- Parent at index floor((i − 1) / 2).Alternatively, if the tree root is at index 1, with valid indices 1 through n, then each element a at index i has
- Children at indices 2i and 2i + 1 respectively.
- Parent at index floor(i / 2).## Binary heap types
As you read above, binary heap have two differents types, the first is *`Min heap`*, when this is organized(heapify), the root of our binary heap must be the smallest value of heap.
The second one is *`Max heap`* when you heapify, the root it must be biggest value of heap.
## Where its used?
Binary heaps are also commonly employed in the heapsort sorting algorithm, which is an in-place algorithm because binary heaps can be implemented as an implicit data structure, storing keys in an array and using their relative positions within that array to represent child–parent relationships.
## My code
I did the *`Min heap`* in rust to practice, and in the code, we have a 'insert' function, to insert a new value in heap, 'delete_minimun' to delete the root of heap, and 'delete_element', to delete a specific element by index number, and the last one, 'heapify' function to organize and reach binary heap conditions. I also did tests with all possible cases.
## Reference
[Binary heap - wikipedia](https://en.wikipedia.org/wiki/Binary_heap)