|
| 1 | +# Class BinaryHeap |
| 2 | + |
| 3 | +> JavaScript Implementation of the Binary Heap. |
| 4 | +> Author: xdf |
| 5 | +
|
| 6 | +## Constructor Detail |
| 7 | + |
| 8 | +### BinaryHeap() |
| 9 | + |
| 10 | +Constructs a new minimum binary heap, Minimum heap is default; |
| 11 | + |
| 12 | +### BinaryHeap(Boolean isMinHeap) |
| 13 | + |
| 14 | +Constructs a new minimum binary heap. |
| 15 | + |
| 16 | +` Parameters: ` |
| 17 | + |
| 18 | +* isMinHeap - true to use the order imposed by the given comparator; false to reverse that order |
| 19 | + |
| 20 | +### BinaryHeap(Function comparator) |
| 21 | + |
| 22 | +Constructs a new BinaryHeap that will use the given comparator to order its elements. |
| 23 | + |
| 24 | +` Parameters: ` |
| 25 | + |
| 26 | +* capacity - the initial capacity for the heap |
| 27 | + |
| 28 | +### BinaryHeap(Number capacity) |
| 29 | + |
| 30 | +Constructs a new minimum binary heap with the specified initial capacity. |
| 31 | + |
| 32 | +` Parameters: ` |
| 33 | + |
| 34 | +* capacity - The initial capacity for the heap. This value must be greater than zero. |
| 35 | + |
| 36 | +` Throws: ` |
| 37 | + |
| 38 | +* IllegalArgumentException - if capacity is <= 0 |
| 39 | + |
| 40 | +### BinaryHeap(Number capacity, Boolean isMinHeap, Function comparator) |
| 41 | + |
| 42 | +Constructs a new BinaryHeap. |
| 43 | + |
| 44 | +` Parameters: ` |
| 45 | + |
| 46 | +* capacity - the initial capacity for the heap |
| 47 | +* isMinHeap - true to use the order imposed by the given comparator; false to reverse that order |
| 48 | +* comparator - the comparator used to order the elements, null means use natural order |
| 49 | + |
| 50 | +` Throws: ` |
| 51 | + |
| 52 | +* IllegalArgumentException - if capacity is <= 0 |
| 53 | + |
| 54 | + |
| 55 | +## Method Detail |
| 56 | + |
| 57 | +### void clear() |
| 58 | + |
| 59 | +Clears all elements from queue. |
| 60 | + |
| 61 | +### boolean isEmpty() |
| 62 | + |
| 63 | +Tests if queue is empty. |
| 64 | + |
| 65 | +` Returns: ` |
| 66 | + |
| 67 | +* true if queue is empty; false otherwise. |
| 68 | + |
| 69 | +### boolean isFull() |
| 70 | + |
| 71 | +Tests if queue is full. |
| 72 | + |
| 73 | +` Returns: ` |
| 74 | + |
| 75 | +* true if queue is full; false otherwise. |
| 76 | + |
| 77 | +### void insert(Object element) |
| 78 | + |
| 79 | +Inserts an element into queue. |
| 80 | + |
| 81 | +` Parameters: ` |
| 82 | + |
| 83 | +* element - the element to be inserted |
| 84 | + |
| 85 | +### object peek() |
| 86 | + |
| 87 | +Returns the element on top of heap but don't remove it. |
| 88 | + |
| 89 | +` Returns: ` |
| 90 | + |
| 91 | +* the element at top of heap |
| 92 | + |
| 93 | +` Throws: ` |
| 94 | + |
| 95 | +* NoSuchElementException - if isEmpty() == true |
| 96 | + |
| 97 | +### object pop() |
| 98 | + |
| 99 | +Returns the element on top of heap and remove it. |
| 100 | + |
| 101 | +` Returns: ` |
| 102 | + |
| 103 | +* the element at top of heap |
| 104 | + |
| 105 | +` Throws: ` |
| 106 | + |
| 107 | +* NoSuchElementException - if isEmpty() == true |
| 108 | + |
| 109 | + |
| 110 | +### string toString() |
| 111 | + |
| 112 | +Returns a string representation of this heap. |
| 113 | + |
| 114 | +` Returns: ` |
| 115 | + |
| 116 | +* a string representation of this heap |
| 117 | + |
| 118 | +### object iterator() |
| 119 | + |
| 120 | +Returns an iterator over this heap's elements. |
| 121 | + |
| 122 | +` Returns: ` |
| 123 | + |
| 124 | +* an iterator over this heap's elements |
| 125 | + |
| 126 | +### boolean add(Object object) |
| 127 | + |
| 128 | +Adds an object to this heap. Same as insert(Object). |
| 129 | + |
| 130 | +` Parameters: ` |
| 131 | + |
| 132 | +* object - the object to add |
| 133 | + |
| 134 | +` Returns: ` |
| 135 | + |
| 136 | +* true, always |
| 137 | + |
| 138 | +### object get() |
| 139 | + |
| 140 | +Returns the priority element. Same as peek(). |
| 141 | + |
| 142 | +` Returns: ` |
| 143 | + |
| 144 | +* the priority element |
| 145 | + |
| 146 | +` Throws: ` |
| 147 | + |
| 148 | +* BufferUnderflowException - if this heap is empty |
| 149 | + |
| 150 | +### object remove() |
| 151 | + |
| 152 | +Removes the priority element. Same as pop(). |
| 153 | + |
| 154 | +` Returns: ` |
| 155 | + |
| 156 | +* the removed priority element |
| 157 | + |
| 158 | +` Throws: ` |
| 159 | + |
| 160 | +* BufferUnderflowException - if this heap is empty |
| 161 | + |
| 162 | +### number size() |
| 163 | + |
| 164 | +Returns the number of elements in this heap. |
| 165 | + |
| 166 | +` Returns: ` |
| 167 | + |
| 168 | +* the number of elements in this heap |
| 169 | + |
| 170 | +## License |
| 171 | + |
| 172 | +The MIT License (MIT) |
| 173 | + |
| 174 | +Copyright (c) 2013 xdf |
| 175 | + |
| 176 | +Permission is hereby granted, free of charge, to any person obtaining a copy of |
| 177 | +this software and associated documentation files (the "Software"), to deal in |
| 178 | +the Software without restriction, including without limitation the rights to |
| 179 | +use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of |
| 180 | +the Software, and to permit persons to whom the Software is furnished to do so, |
| 181 | +subject to the following conditions: |
| 182 | + |
| 183 | +The above copyright notice and this permission notice shall be included in all |
| 184 | +copies or substantial portions of the Software. |
| 185 | + |
| 186 | +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 187 | +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS |
| 188 | +FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR |
| 189 | +COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER |
| 190 | +IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN |
| 191 | +CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
0 commit comments