From 8f4fc49e7ac74ba49f2304ee15bfe07dbc236933 Mon Sep 17 00:00:00 2001 From: marina <138340846+bt3gl-cryptographer@users.noreply.github.com> Date: Wed, 2 Aug 2023 13:03:15 -0700 Subject: [PATCH] Update README.md --- heaps/README.md | 25 +++++++++++++++++++++---- 1 file changed, 21 insertions(+), 4 deletions(-) diff --git a/heaps/README.md b/heaps/README.md index ecbc122..59bc3e8 100644 --- a/heaps/README.md +++ b/heaps/README.md @@ -16,14 +16,23 @@ * a max heap's **root node must** have all its children either **greater than or equal** to its children. a min heap is the opposite. duplicate values are allowed. * since you always remove the root, insertion and deletion takes `O(log(n))`. the maximum/minimum value in the heap can be obtained with `O(1)` time complexity. -* heaps can be represented with linked lists or queues (arrays). - * in the case of arrays, the index of the parent `node = [(n-1)/2]`. + +* heaps can be represented with linked lists or queues (arrays) or binary trees. + +* in the case of a tree to array heap: + * the parent node index is given by `n / 2` + * the left children index is `2 * n` + * the right children index is `2 * n + 1` + * a node is a leaf when its index `> n / 2` +
---- ### priority queues +
+ * a priority queue is an abstract data type with the following properties: 1. every item has a priority (usually an integer). 2. an item with a high priority is dequeued before an item with low priority. @@ -37,6 +46,8 @@ ### min heaps +
+ * a **min heap** is a complete binary tree where each node is smaller than its children (the root is the min element). * `insert`: @@ -48,7 +59,9 @@ - then bubble down, swapping it with one of its children until the min-heap is properly restored - there is no need for order between right and left, so this operation would only take `O(log n)` runtime. -* the code below is an example: +* the code below is an example of an ad-hoc heap class in python. + +
```python class MinHeap: @@ -128,6 +141,8 @@ class MinHeap: ### max heaps +
+ * a **max heap** is a complete binary tree where each node is larger than its children (the root is the max element). * `insert`: @@ -138,7 +153,9 @@ class MinHeap: * remove/return the top and then replace the tree's top with its bottom rightmost element. * swap up until the max element is on the top. -* the code below is an example: +* the code below is an example of a max heap class built in python: + +
```python class MaxHeap: