This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
struct MinHeap { | |
var items: [Int] = [] | |
//Get Index | |
private func getLeftChildIndex(_ parentIndex: Int) -> Int { | |
return 2 * parentIndex + 1 | |
} | |
private func getRightChildIndex(_ parentIndex: Int) -> Int { | |
return 2 * parentIndex + 2 | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
struct MinHeap { | |
var items: [Int] = [] | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public func peek() -> Int { | |
if items.count != 0 { | |
return items[0] | |
} else { | |
fatalError() | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
mutating public func poll() -> Int { | |
if items.count != 0 { | |
let item = items[0] | |
items[0] = items[items.count - 1] | |
heapifyDown() | |
items.removeLast() | |
return item | |
} else { | |
fatalError() | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
mutating public func add(_ item: Int) { | |
items.append(item) | |
heapifyUp() | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
mutating private func swap(indexOne: Int, indexTwo: Int) { | |
let placeholder = items[indexOne] | |
items[indexOne] = items[indexTwo] | |
items[indexTwo] = placeholder | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
mutating private func heapifyUp() { | |
var index = items.count - 1 | |
while hasParent(index) && parent(index) > items[index] { | |
swap(indexOne: getParentIndex(index), indexTwo: index) | |
index = getParentIndex(index) | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
mutating private func heapifyDown() { | |
var index = 0 | |
while hasLeftChild(index) { | |
var smallerChildIndex = getLeftChildIndex(index) | |
if hasRightChild(index) && rightChild(index) < leftChild(index) { | |
smallerChildIndex = getRightChildIndex(index) | |
} | |
if items[index] < items[smallerChildIndex] { | |
break |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var myHeap = MinHeap() | |
myHeap.add(2) | |
myHeap.add(10) | |
myHeap.add(8) | |
myHeap.add(9) | |
myHeap.add(7) | |
myHeap.add(3) | |
myHeap.add(4) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
myHeap.peek() // Prints 2 to the console on the right in Swift Playgrounds | |
myHeap.poll() | |
dump(myHeap.items) |
OlderNewer