I’m studying Dyalog APL. I carried out a binary heap, which appears to work. How may I make it look extra like APL (and fewer like Python)?
⎕io←0
heappush←{(⍺,⍵)siftdown 0(≢⍺)}
heappop←{
heap←⍵
final←⊃¯1↑heap ⋄ relaxation←¯1↓heap
0=≢relaxation:relaxation final
r←heap[0]
(((final@0)relaxation)siftup 0)r
}
siftdown←{
heap←⍺
begin pos←⍵
merchandise←pos⌷heap
newpos←{
⍵≤begin:⍵
parentpos←⌊(⍵-1)÷2
guardian←parentpos⌷heap
merchandise<guardian:∇ parentpos⊣heap[⍵]←guardian
⍵
}pos
(merchandise@newpos)heap
}
siftup←{
heap←⍺
p←{
pos←⍵
chp←1+2×pos
chp≥≢heap:pos
rpos←1+chp
chp←((rpos<≢heap)∧~heap[chp]<heap[rpos])⊃chp rpos
heap[pos]←heap[chp]
∇ chp
}⍵
heap[p]←⍺[⍵]
heap siftdown ⍵ p
}
heap←Zero 1 2 5 6 Eight 9
heappop heap
┌→────────────────┐
│ ┌→──────────┐ │
│ │1 5 2 9 6 8│ 0 │
│ └~──────────┘ │
└∊────────────────┘
heap heappush 3
┌→──────────────┐
│Zero 1 2 Three 6 Eight 9 5│
└~──────────────┘