|
| 1 | +// The MIT License (MIT) |
| 2 | + |
| 3 | +// Copyright (c) 2016 Mike Taghavi (mitghi[at]me.com) |
| 4 | + |
| 5 | +// Permission is hereby granted, free of charge, to any person obtaining a copy |
| 6 | +// of this software and associated documentation files (the "Software"), to deal |
| 7 | +// in the Software without restriction, including without limitation the rights |
| 8 | +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 9 | +// copies of the Software, and to permit persons to whom the Software is |
| 10 | +// furnished to do so, subject to the following conditions: |
| 11 | + |
| 12 | +// The above copyright notice and this permission notice shall be included in all |
| 13 | +// copies or substantial portions of the Software. |
| 14 | + |
| 15 | +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 16 | +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 17 | +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 18 | +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 19 | +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 20 | +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 21 | +// SOFTWARE. |
| 22 | + |
| 23 | +class PriorityQueue{ |
| 24 | + var heap: [Int] = [0] |
| 25 | + var currentSize: Int = 0 |
| 26 | + |
| 27 | + enum Direction { case UP,DOWN } |
| 28 | + |
| 29 | + func insert(item: Int){ |
| 30 | + heap.append(item) |
| 31 | + currentSize += 1 |
| 32 | + heapify(currentSize,direction:.UP) |
| 33 | + } |
| 34 | + |
| 35 | + private func heapify(size: Int,direction: Direction){ |
| 36 | + switch direction{ |
| 37 | + case .UP: |
| 38 | + var i = size |
| 39 | + while i / 2 > 0 { |
| 40 | + if heap[i] < heap[i/2]{ |
| 41 | + let temp = heap[i] |
| 42 | + heap[i] = heap[i/2] |
| 43 | + heap[i/2] = temp |
| 44 | + } |
| 45 | + i /= 2 |
| 46 | + } |
| 47 | + case .DOWN: |
| 48 | + var i = size |
| 49 | + while i*2 <= currentSize { |
| 50 | + let min_child = minChild(i) |
| 51 | + if heap[i] > heap[min_child]{ |
| 52 | + let temp = heap[i] |
| 53 | + heap[i] = heap[min_child] |
| 54 | + heap[min_child] = temp |
| 55 | + } |
| 56 | + i = min_child |
| 57 | + } |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + private func minChild(currentChild: Int) -> Int{ |
| 62 | + if currentChild * 2 + 1 > currentSize { return currentChild * 2 } |
| 63 | + else { |
| 64 | + if heap[currentChild * 2] < heap[(currentChild * 2) + 1] { return currentChild * 2 } |
| 65 | + else { return (currentChild * 2) + 1 } |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + func pop() -> Int? { |
| 70 | + if heap.count < 2 { return nil } |
| 71 | + |
| 72 | + let item: Int? = heap.removeAtIndex(1) |
| 73 | + currentSize -= 1 |
| 74 | + |
| 75 | + if currentSize > 1 { |
| 76 | + heap.insert(heap.removeLast(),atIndex:1) |
| 77 | + heapify(1,direction:.DOWN) |
| 78 | + } |
| 79 | + |
| 80 | + return item |
| 81 | + } |
| 82 | +} |
0 commit comments