summaryrefslogtreecommitdiff
path: root/doc/unit/10
diff options
context:
space:
mode:
authormo khan <mo.khan@gmail.com>2020-08-27 12:05:33 -0600
committermo khan <mo.khan@gmail.com>2020-08-27 12:05:33 -0600
commit67a2c78001222fa71f391bdf48b6a95b92f11461 (patch)
tree7f23f908e9b84f32cbedb44aad794d7a2045f4fb /doc/unit/10
parentb8f3a0b76f8add36b7f4c67025b2e4bd48eda0cc (diff)
Add notes
Diffstat (limited to 'doc/unit/10')
-rw-r--r--doc/unit/10/README.md31
1 files changed, 31 insertions, 0 deletions
diff --git a/doc/unit/10/README.md b/doc/unit/10/README.md
index e69de29..1c6b645 100644
--- a/doc/unit/10/README.md
+++ b/doc/unit/10/README.md
@@ -0,0 +1,31 @@
+# Heaps
+
+Eytzinger's method can represent a complete binary tree as an array.
+
+`2i + 1` and the right child of the node at index `i` is at
+index `right(i) = 2i +2`. The parent of the node at index `i` is at index
+`parent(i) = (i-1)/2`.
+
+```java
+class BinaryHeap {
+ T[] a;
+ int n;
+
+ int left(int i) {
+ return 2*i + 1;
+ }
+ int right(int i) {
+ return 2*i + 2;
+ }
+ int parent(int i) {
+ return (i-1)/2;
+ }
+}
+```
+
+A `BinaryHeap` uses this technique to implicitly represent a complete
+binary tree in which the elements are `heap-ordered.`
+
+heap-ordered: The value stored at any index `i` is not smaller than the value stored at index `parent(i)`, with the exception of the root value, `i = 0`.
+The smallest value in the priority Queue is at position 0.
+