Files
Yudong Jin 2778a6f9c7 Translate all code to English (#1836)
* Review the EN heading format.

* Fix pythontutor headings.

* Fix pythontutor headings.

* bug fixes

* Fix headings in **/summary.md

* Revisit the CN-to-EN translation for Python code using Claude-4.5

* Revisit the CN-to-EN translation for Java code using Claude-4.5

* Revisit the CN-to-EN translation for Cpp code using Claude-4.5.

* Fix the dictionary.

* Fix cpp code translation for the multipart strings.

* Translate Go code to English.

* Update workflows to test EN code.

* Add EN translation for C.

* Add EN translation for CSharp.

* Add EN translation for Swift.

* Trigger the CI check.

* Revert.

* Update en/hash_map.md

* Add the EN version of Dart code.

* Add the EN version of Kotlin code.

* Add missing code files.

* Add the EN version of JavaScript code.

* Add the EN version of TypeScript code.

* Fix the workflows.

* Add the EN version of Ruby code.

* Add the EN version of Rust code.

* Update the CI check for the English version  code.

* Update Python CI check.

* Fix cmakelists for en/C code.

* Fix Ruby comments
2025-12-31 07:44:52 +08:00

63 lines
1.6 KiB
Swift

/**
* File: heap.swift
* Created Time: 2024-03-17
* Author: nuomi1 (nuomi1@qq.com)
*/
import HeapModule
import utils
func testPush(heap: inout Heap<Int>, val: Int) {
heap.insert(val)
print("\nAfter element \(val) pushes to heap\n")
PrintUtil.printHeap(queue: heap.unordered)
}
func testPop(heap: inout Heap<Int>) {
let val = heap.removeMax()
print("\nAfter heap top element \(val) pops from heap\n")
PrintUtil.printHeap(queue: heap.unordered)
}
@main
enum _Heap {
/* Driver Code */
static func main() {
/* Initialize heap */
// Swift's Heap type supports both max heap and min heap
var heap = Heap<Int>()
/* Element enters heap */
testPush(heap: &heap, val: 1)
testPush(heap: &heap, val: 3)
testPush(heap: &heap, val: 2)
testPush(heap: &heap, val: 5)
testPush(heap: &heap, val: 4)
/* Check if heap is empty */
let peek = heap.max()
print("\nHeap top element is \(peek!)\n")
/* Time complexity is O(n), not O(nlogn) */
testPop(heap: &heap)
testPop(heap: &heap)
testPop(heap: &heap)
testPop(heap: &heap)
testPop(heap: &heap)
/* Get heap size */
let size = heap.count
print("\nHeap size is \(size)\n")
/* Check if heap is empty */
let isEmpty = heap.isEmpty
print("\nIs heap empty \(isEmpty)\n")
/* Input list and build heap */
// Time complexity is O(n), not O(nlogn)
let heap2 = Heap([1, 3, 2, 5, 4])
print("\nAfter input list and build heap")
PrintUtil.printHeap(queue: heap2.unordered)
}
}