Following nice suggestions given in response to my earlier query, I used to be inspired to think about a special sort of heap construction, the Leftist Tree. It lends itself higher to a useful implementation. It is already shorter, however is there anything I ought to think about with the intention to make this extra idiomatic APL? Many thanks.
⍝ APL implementation of a leftist tree.
⍝
⍝ https://en.wikipedia.org/wiki/Leftist_tree
⍝ http://typeocaml.com/2015/03/12/heap-leftist-tree/
⎕io←0
Insert←{ ⍝ Insert merchandise into leftist tree, returning the ensuing tree
(tree merchandise)←⍵
1 merchandise ⍬ ⍬ Merge tree
}
Pop←{ ⍝ Pop off smallest factor from a leftist tree
0=≢⍵:⍬
(v l r)←1↓⍵ ⍝ worth left proper
(l Merge r) v ⍝ Return the ensuing tree and the worth
}
Merge←{ ⍝ Merge two leftist bushes, t1 and t2
t1←⍺ ⋄ t2←⍵
0=≢t1:t2 ⋄ 0=≢t2:t1 ⍝ If both is a leaf, return the opposite
(key1 left proper)←1↓t1 ⋄ key2←1⌷t2
key1>key2:t2∇t1 ⍝ Flip to make sure smallest is root of merged
merged←proper∇t2 ⍝ Merge rightwards
(⊃left)≥⊃merged:(1+⊃merged) key1 left merged ⍝ Proper is shorter
(1+⊃left) key1 merged left ⍝ Left is shorter; make it the brand new proper
}
⍝ Instance heap merge from http://typeocaml.com/2015/03/12/heap-leftist-tree/
h←Insert ⍬ 2
h←Insert h 10
h←Insert h 9
s←Insert ⍬ 3
s←Insert s 6
h Merge s
┌→─────────────────────────────────────────────────────────────┐
│ ┌→────────────────────────────────────┐ ┌→─────────────┐ │
│ 2 2 │ ┌→────────────┐ ┌→────────────┐ │ │ ┌⊖┐ ┌⊖┐ │ │
│ │ 2 3 │ ┌⊖┐ ┌⊖┐ │ │ ┌⊖┐ ┌⊖┐ │ │ │ 1 10 │0│ │0│ │ │
│ │ │ 1 6 │0│ │0│ │ │ 1 9 │0│ │0│ │ │ │ └~┘ └~┘ │ │
│ │ │ └~┘ └~┘ │ │ └~┘ └~┘ │ │ └∊─────────────┘ │
│ │ └∊────────────┘ └∊────────────┘ │ │
│ └∊────────────────────────────────────┘ │
└∊─────────────────────────────────────────────────────────────┘