Home > other >  The heap golang implementation issues???
The heap golang implementation issues???

Time:03-07

Small cap pile, pile interface to realize golang:
 
Type bigHeap int []

Len bigHeap func (h) (int) {
Return len (h)
}

BigHeap func (h) Less (I, j int) bool {
Return the h [I] }

Swap bigHeap func (h) (I, j int) {
H [I], [j] h=h [j], [I]
h}

Func (h * bigHeap) Push interface (x) {} {
* h=append (* h, x. (int))
}

Func (h * bigHeap) Pop () interface {} {
N:=len (* h)
X:=* h] [n - 1
* h=* h [: n - 1)
Return the x
}


If I want to making a big pile, then repeat the above five functions, but you only need to modify a Less function, repeat some extra, and realize the heap Interface also implements the sort sequence Interface, ordering Interface inside through sort. The Reverse (sort) Interface) functions, you can get a new Reverse Less function Interface, whether can be at the top of the small pile of, on the basis of the big top pile is obtained by Reverse Less function? I've tried many times, but not so to write, to have bosses to give directions, thank you

CodePudding user response:

Oh oh, the question to write wrong, this code is a little heap
  • Related