mirror of
https://github.com/krahets/hello-algo.git
synced 2026-05-12 03:27:44 +08:00
* 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
102 lines
2.5 KiB
Go
102 lines
2.5 KiB
Go
// File: heap_test.go
|
|
// Created Time: 2023-01-12
|
|
// Author: Reanon (793584285@qq.com)
|
|
|
|
package chapter_heap
|
|
|
|
import (
|
|
"container/heap"
|
|
"fmt"
|
|
"strconv"
|
|
"testing"
|
|
|
|
. "github.com/krahets/hello-algo/pkg"
|
|
)
|
|
|
|
func testPush(h *intHeap, val int) {
|
|
// Call heap.Interface function to add element
|
|
heap.Push(h, val)
|
|
fmt.Printf("\nAfter element %d pushes to heap \n", val)
|
|
PrintHeap(*h)
|
|
}
|
|
|
|
func testPop(h *intHeap) {
|
|
// Call heap.Interface function to remove element
|
|
val := heap.Pop(h)
|
|
fmt.Printf("\nAfter heap top element %d pops from heap \n", val)
|
|
PrintHeap(*h)
|
|
}
|
|
|
|
func TestHeap(t *testing.T) {
|
|
/* Initialize heap */
|
|
// Consider negating the elements before entering the heap, which can reverse the size relationship, thus implementing max heap
|
|
maxHeap := &intHeap{}
|
|
heap.Init(maxHeap)
|
|
/* Element enters heap */
|
|
testPush(maxHeap, 1)
|
|
testPush(maxHeap, 3)
|
|
testPush(maxHeap, 2)
|
|
testPush(maxHeap, 5)
|
|
testPush(maxHeap, 4)
|
|
|
|
/* Check if heap is empty */
|
|
top := maxHeap.Top()
|
|
fmt.Printf("Heap top element is %d\n", top)
|
|
|
|
/* Time complexity is O(n), not O(nlogn) */
|
|
testPop(maxHeap)
|
|
testPop(maxHeap)
|
|
testPop(maxHeap)
|
|
testPop(maxHeap)
|
|
testPop(maxHeap)
|
|
|
|
/* Get heap size */
|
|
size := len(*maxHeap)
|
|
fmt.Printf("Heap size is %d\n", size)
|
|
|
|
/* Check if heap is empty */
|
|
isEmpty := len(*maxHeap) == 0
|
|
fmt.Printf("Is heap empty %t\n", isEmpty)
|
|
}
|
|
|
|
func TestMyHeap(t *testing.T) {
|
|
/* Initialize heap */
|
|
// Consider negating the elements before entering the heap, which can reverse the size relationship, thus implementing max heap
|
|
maxHeap := newMaxHeap([]any{9, 8, 6, 6, 7, 5, 2, 1, 4, 3, 6, 2})
|
|
fmt.Printf("After input array and building heap\n")
|
|
maxHeap.print()
|
|
|
|
/* Check if heap is empty */
|
|
peek := maxHeap.peek()
|
|
fmt.Printf("\nHeap top element is %d\n", peek)
|
|
|
|
/* Element enters heap */
|
|
val := 7
|
|
maxHeap.push(val)
|
|
fmt.Printf("\nAfter element %d enters heap\n", val)
|
|
maxHeap.print()
|
|
|
|
/* Time complexity is O(n), not O(nlogn) */
|
|
peek = maxHeap.pop()
|
|
fmt.Printf("\nAfter heap top element %d exits heap\n", peek)
|
|
maxHeap.print()
|
|
|
|
/* Get heap size */
|
|
size := maxHeap.size()
|
|
fmt.Printf("\nHeap element count is %d\n", size)
|
|
|
|
/* Check if heap is empty */
|
|
isEmpty := maxHeap.isEmpty()
|
|
fmt.Printf("\nIs heap empty %t\n", isEmpty)
|
|
}
|
|
|
|
func TestTopKHeap(t *testing.T) {
|
|
/* Initialize heap */
|
|
// Consider negating the elements before entering the heap, which can reverse the size relationship, thus implementing max heap
|
|
nums := []int{1, 7, 6, 3, 2}
|
|
k := 3
|
|
res := topKHeap(nums, k)
|
|
fmt.Printf("The largest " + strconv.Itoa(k) + " elements are")
|
|
PrintHeap(*res)
|
|
}
|