mirror of
https://github.com/duke-git/lancet.git
synced 2026-02-23 13:52:26 +08:00
feat: add MaxHeap func PrintStructure
This commit is contained in:
@@ -5,6 +5,8 @@
|
|||||||
package datastructure
|
package datastructure
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
|
|
||||||
"github.com/duke-git/lancet/v2/lancetconstraints"
|
"github.com/duke-git/lancet/v2/lancetconstraints"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -101,6 +103,67 @@ func (h *MaxHeap[T]) Data() []T {
|
|||||||
return h.data
|
return h.data
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// PrintStructure print the structure of the heap
|
||||||
|
func (h *MaxHeap[T]) PrintStructure() {
|
||||||
|
level := 1
|
||||||
|
data := h.data
|
||||||
|
length := len(h.data)
|
||||||
|
index := 0
|
||||||
|
|
||||||
|
list := [][]string{}
|
||||||
|
temp := []string{}
|
||||||
|
for index < length {
|
||||||
|
start := powerTwo(level-1) - 1
|
||||||
|
end := start + powerTwo(level-1) - 1
|
||||||
|
|
||||||
|
temp = append(temp, fmt.Sprintf("%v", data[index]))
|
||||||
|
index++
|
||||||
|
|
||||||
|
if index > end || index >= length {
|
||||||
|
list = append(list, temp)
|
||||||
|
temp = []string{}
|
||||||
|
|
||||||
|
if index < length {
|
||||||
|
level++
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
lastNum := powerTwo(level - 1)
|
||||||
|
lastLen := lastNum + (lastNum - 1)
|
||||||
|
|
||||||
|
heapTree := make([][]string, level, level)
|
||||||
|
for i := 0; i < level; i++ {
|
||||||
|
heapTree[i] = make([]string, lastLen, lastLen)
|
||||||
|
for j := 0; j < lastLen; j++ {
|
||||||
|
heapTree[i][j] = ""
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
for k := 0; k < len(list); k++ {
|
||||||
|
vals := list[k]
|
||||||
|
tempLevel := level - k
|
||||||
|
st := powerTwo(tempLevel-1) - 1
|
||||||
|
for _, v := range vals {
|
||||||
|
heapTree[k][st] = v
|
||||||
|
gap := powerTwo(tempLevel)
|
||||||
|
st = st + gap
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
for m := 0; m < level; m++ {
|
||||||
|
for n := 0; n < lastLen; n++ {
|
||||||
|
val := heapTree[m][n]
|
||||||
|
if val == "" {
|
||||||
|
fmt.Printf(" ")
|
||||||
|
} else {
|
||||||
|
fmt.Printf(val)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
fmt.Println()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// parentIndex get parent index of the given index
|
// parentIndex get parent index of the given index
|
||||||
func parentIndex(i int) int {
|
func parentIndex(i int) int {
|
||||||
return (i - 1) / 2
|
return (i - 1) / 2
|
||||||
@@ -120,3 +183,7 @@ func rightChildIndex(i int) int {
|
|||||||
func (h *MaxHeap[T]) swap(i, j int) {
|
func (h *MaxHeap[T]) swap(i, j int) {
|
||||||
h.data[i], h.data[j] = h.data[j], h.data[i]
|
h.data[i], h.data[j] = h.data[j], h.data[i]
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func powerTwo(n int) int {
|
||||||
|
return 1 << n
|
||||||
|
}
|
||||||
|
|||||||
@@ -34,6 +34,8 @@ func TestMaxHeap_Push(t *testing.T) {
|
|||||||
assert.Equal(expected, heap.data)
|
assert.Equal(expected, heap.data)
|
||||||
|
|
||||||
assert.Equal(12, heap.Size())
|
assert.Equal(12, heap.Size())
|
||||||
|
|
||||||
|
heap.PrintStructure()
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestMaxHeap_Pop(t *testing.T) {
|
func TestMaxHeap_Pop(t *testing.T) {
|
||||||
|
|||||||
Reference in New Issue
Block a user