|
| 1 | +import Foundation |
| 2 | + |
| 3 | +//struct MinHeap { |
| 4 | +// var items: [Int] = [] |
| 5 | +// |
| 6 | +// //Get Index |
| 7 | +// private func getLeftChildIndex(_ parentIndex: Int) -> Int { |
| 8 | +// return 2 * parentIndex + 1 |
| 9 | +// } |
| 10 | +// private func getRightChildIndex(_ parentIndex: Int) -> Int { |
| 11 | +// return 2 * parentIndex + 2 |
| 12 | +// } |
| 13 | +// private func getParentIndex(_ childIndex: Int) -> Int { |
| 14 | +// return (childIndex - 1) / 2 |
| 15 | +// } |
| 16 | +// |
| 17 | +// // Boolean Check |
| 18 | +// private func hasLeftChild(_ index: Int) -> Bool { |
| 19 | +// return getLeftChildIndex(index) < items.count |
| 20 | +// } |
| 21 | +// private func hasRightChild(_ index: Int) -> Bool { |
| 22 | +// return getRightChildIndex(index) < items.count |
| 23 | +// } |
| 24 | +// private func hasParent(_ index: Int) -> Bool { |
| 25 | +// return getParentIndex(index) >= 0 |
| 26 | +// } |
| 27 | +// |
| 28 | +// // Return Item From Heap |
| 29 | +// private func leftChild(_ index: Int) -> Int { |
| 30 | +// return items[getLeftChildIndex(index)] |
| 31 | +// } |
| 32 | +// private func rightChild(_ index: Int) -> Int { |
| 33 | +// return items[getRightChildIndex(index)] |
| 34 | +// } |
| 35 | +// private func parent(_ index: Int) -> Int { |
| 36 | +// return items[getParentIndex(index)] |
| 37 | +// } |
| 38 | +// |
| 39 | +// // Heap Operations |
| 40 | +// mutating private func swap(indexOne: Int, indexTwo: Int) { |
| 41 | +// let placeholder = items[indexOne] |
| 42 | +// items[indexOne] = items[indexTwo] |
| 43 | +// items[indexTwo] = placeholder |
| 44 | +// } |
| 45 | +// |
| 46 | +// public func peek() -> Int { |
| 47 | +// if items.count != 0 { |
| 48 | +// return items[0] |
| 49 | +// } else { |
| 50 | +// fatalError() |
| 51 | +// } |
| 52 | +// } |
| 53 | +// |
| 54 | +// mutating public func poll() -> Int { |
| 55 | +// if items.count != 0 { |
| 56 | +// let item = items[0] |
| 57 | +// items[0] = items[items.count - 1] |
| 58 | +// heapifyDown() |
| 59 | +// items.removeLast() |
| 60 | +// return item |
| 61 | +// } else { |
| 62 | +// fatalError() |
| 63 | +// } |
| 64 | +// } |
| 65 | +// |
| 66 | +// mutating public func add(_ item: Int) { |
| 67 | +// items.append(item) |
| 68 | +// heapifyUp() |
| 69 | +// } |
| 70 | +// |
| 71 | +// mutating private func heapifyUp() { |
| 72 | +// var index = items.count - 1 |
| 73 | +// while hasParent(index) && parent(index) > items[index] { |
| 74 | +// swap(indexOne: getParentIndex(index), indexTwo: index) |
| 75 | +// index = getParentIndex(index) |
| 76 | +// } |
| 77 | +// } |
| 78 | +// |
| 79 | +// mutating private func heapifyDown() { |
| 80 | +// var index = 0 |
| 81 | +// while hasLeftChild(index) { |
| 82 | +// var smallerChildIndex = getLeftChildIndex(index) |
| 83 | +// if hasRightChild(index) && rightChild(index) < leftChild(index) { |
| 84 | +// smallerChildIndex = getRightChildIndex(index) |
| 85 | +// } |
| 86 | +// |
| 87 | +// if items[index] < items[smallerChildIndex] { |
| 88 | +// break |
| 89 | +// } else { |
| 90 | +// swap(indexOne: index, indexTwo: smallerChildIndex) |
| 91 | +// } |
| 92 | +// index = smallerChildIndex |
| 93 | +// } |
| 94 | +// } |
| 95 | +//} |
| 96 | + |
| 97 | +struct MaxHeap<Element: Comparable> { |
| 98 | + var items: [Element] = [] |
| 99 | + |
| 100 | + public mutating func add(_ item: Element) { |
| 101 | + items.append(item) |
| 102 | + heapifyUp() |
| 103 | + } |
| 104 | + |
| 105 | + public mutating func poll() -> Element? { |
| 106 | + if isEmpty() {return nil} |
| 107 | + let result = items.first! |
| 108 | + items[0] = items.last! |
| 109 | + heapifyDown() |
| 110 | + items.removeLast() |
| 111 | + |
| 112 | + return result |
| 113 | + } |
| 114 | + |
| 115 | + public func peek() -> Element? { |
| 116 | + if isEmpty() {return nil} |
| 117 | + return items.first! |
| 118 | + } |
| 119 | + |
| 120 | + public func isEmpty() -> Bool { |
| 121 | + return items.count == 0 |
| 122 | + } |
| 123 | + |
| 124 | + private mutating func heapifyUp() { |
| 125 | + if isEmpty() {return} |
| 126 | + |
| 127 | + var index = items.count - 1 |
| 128 | + while hasParent(index) && items[index] > items[parentIndex(index)] { |
| 129 | + swap(index, parentIndex(index)) |
| 130 | + index = parentIndex(index) |
| 131 | + } |
| 132 | + } |
| 133 | + |
| 134 | + private mutating func heapifyDown() { |
| 135 | + if isEmpty() {return} |
| 136 | + |
| 137 | + var index = 0 |
| 138 | + while hasLeftChild(index) { |
| 139 | + if hasRightChild(index) { |
| 140 | + if leftChild(index) > rightChild(index) { |
| 141 | + swap(index, leftChildIndex(index)) |
| 142 | + index = leftChildIndex(index) |
| 143 | + continue |
| 144 | + } else { |
| 145 | + swap(index, rightChildIndex(index)) |
| 146 | + index = rightChildIndex(index) |
| 147 | + continue |
| 148 | + } |
| 149 | + } |
| 150 | + swap(index, leftChildIndex(index)) |
| 151 | + index = leftChildIndex(index) |
| 152 | + } |
| 153 | + } |
| 154 | + |
| 155 | + private func leftChild(_ index: Int) -> Element { |
| 156 | + return items[leftChildIndex(index)] |
| 157 | + } |
| 158 | + |
| 159 | + private func rightChild(_ index: Int) -> Element { |
| 160 | + return items[rightChildIndex(index)] |
| 161 | + } |
| 162 | + |
| 163 | + private func parent(_ index: Int) -> Element { |
| 164 | + return items[parentIndex(index)] |
| 165 | + } |
| 166 | + |
| 167 | + private func leftChildIndex(_ index: Int) -> Int { |
| 168 | + return 2 * index + 1 |
| 169 | + } |
| 170 | + |
| 171 | + private func rightChildIndex(_ index: Int) -> Int { |
| 172 | + return 2 * index + 2 |
| 173 | + } |
| 174 | + |
| 175 | + private func parentIndex(_ index: Int) -> Int { |
| 176 | + return (index - 1) / 2 |
| 177 | + } |
| 178 | + |
| 179 | + private func hasLeftChild(_ index: Int) -> Bool { |
| 180 | + return leftChildIndex(index) < items.count |
| 181 | + } |
| 182 | + |
| 183 | + private func hasRightChild(_ index: Int) -> Bool { |
| 184 | + return rightChildIndex(index) < items.count |
| 185 | + } |
| 186 | + |
| 187 | + private func hasParent(_ index: Int) -> Bool { |
| 188 | + return parentIndex(index) >= 0 |
| 189 | + } |
| 190 | + |
| 191 | + private mutating func swap(_ indexOfFirst: Int, _ indexOfSecond: Int) { |
| 192 | + let temp = items[indexOfFirst] |
| 193 | + items[indexOfFirst] = items[indexOfSecond] |
| 194 | + items[indexOfSecond] = temp |
| 195 | + } |
| 196 | +} |
| 197 | + |
| 198 | +var array = Array(0...10) |
| 199 | +var randomArray = array.shuffled() |
| 200 | +print(randomArray) |
| 201 | + |
| 202 | +var heap = MaxHeap<Int>() |
| 203 | +for i in randomArray { |
| 204 | + heap.add(i) |
| 205 | +} |
| 206 | + |
| 207 | +var result: [Int] = [] |
| 208 | +while heap.items.count != 0 { |
| 209 | + result.append(heap.poll()!) |
| 210 | +} |
| 211 | + |
| 212 | +print(result) |
0 commit comments