-
Notifications
You must be signed in to change notification settings - Fork 0
/
heap.swift
141 lines (111 loc) · 2.87 KB
/
heap.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
class Heap {
enum HeapType {
case max, min
func compare(_ num1: Int, _ num2: Int) -> Bool {
switch self {
case .max:
return num1 > num2
case .min:
return num1 < num2
}
}
}
private var list = [Int]()
private let heapType: HeapType
init(heapType: HeapType = .min) {
self.heapType = heapType
}
func add(_ item: Int) {
list.append(item)
heapup(index: list.count - 1)
}
func pop() -> Int? {
guard !list.isEmpty else { return nil }
if list.count == 1 {
return list.removeLast()
}
let first = list[0]
list[0] = list.removeLast()
heapdown(index: 0)
return first
}
func display() -> String {
guard !list.isEmpty else { return "Empty" }
var result = [String]()
var queue = [Int]()
queue.append(0)
while !queue.isEmpty {
var level = [String]()
for _ in 0..<queue.count {
let index = queue.removeFirst()
level.append(String(list[index]))
if index.left < list.count {
queue.append(index.left)
}
if index.right < list.count {
queue.append(index.right)
}
}
result.append(level.joined(separator: " "))
}
return result.joined(separator: "\n")
}
private func heapup(index: Int) {
var i = index
while i.parent >= 0, heapType.compare(list[i], list[i.parent]) {
swap(i, i.parent)
i = i.parent
}
}
private func heapdown(index: Int) {
guard index < list.count else { return }
var i = index
if index.left < list.count, !heapType.compare(list[index], list[index.left]) {
i = index.left
}
if index.right < list.count, !heapType.compare(list[i], list[index.right]) {
i = index.right
}
if i <= index {
return
}
swap(index, i)
heapdown(index: i)
}
private func swap(_ index: Int, _ index2: Int) {
let temp = list[index]
list[index] = list[index2]
list[index2] = temp
}
}
extension Int {
var left: Int {
self * 2 + 1
}
var right: Int {
self * 2 + 2
}
var parent: Int {
(self - 1) / 2
}
}
let heap = Heap(heapType: .max)
heap.add(2)
heap.add(22)
heap.add(1)
heap.add(56)
heap.add(8)
heap.add(100)
heap.add(4)
heap.add(12)
heap.display()
heap.pop()
heap.pop()
heap.pop()
heap.pop()
heap.pop()
heap.pop()
heap.pop()
heap.pop()
heap.pop()
heap.display()