(d B(q(S S
(d C(q((S T)(i T(C(c(a(h S)(h S))S)(s T 1))S
(d D(C(q(1 1))30
(d E(q((U )(a(a U U)U
(d F(q((S T)(i T(F(c(E(h(t S)))S)(s T 1))S
(d G(F(q(1 1))38
(d H(q((S T)(i(e T(q(1 1)))S(i(l(h T)(a S 1))(H(s S(h T))(t T))(H S(t T
(d I(q((S)(e 0(H S G
(d J(q((S)(e 0(H S D
(d K(q((S T)(i(e T(q(1 1)))S(i(l(h T)(a S 1))(a(h(t(t T)))(K(s S(h T))(t T)))(K S(t T
(d L(q((S)(K S G
(d M(q((S)(i(I(s S 1))(i(e(L(s S 1))1)(B(a S S))(i(J(L(s S 1)))(B(a S S))(B(L(s S 1))(a S S))))(B(a S S
(d N(q((S T)(i S(N(t S)(c(h S)T))T
(d O(q((S T)(i S(O(t S)(N(M(h S))T))T
(d P(q((S T)(i(e S())T(Q(t S)(c(h S)T
(d Q(q((S T)(i(e S())T(P(t S)T
(d R(q((S T U)(i(e S())(i(e T())U(R S(t T)(c(h T)U)))(i(e T())(R(t S)T(c(h S)U))(i(e(h S)(h T))(R(t S)(t T)(c(h S)U))(i(l(h S)(h T))(R(t S)T(c(h S)U))(R S(t T)(c(h T)U
(d V(q((S T)(i(e S())T(V(t S)(c(h S)T
(d W(q((S)(i(e()S)S(i(e()(t S))S(V(R(W(Q S()))(W(P S()))())(
(d X(q((T)(i T(O T())(
(d Y(q((T S)(i(e S 0)(B T S)(Y(X T)(s S 1
(d A(q((S)(i(e S 0)(q(1))(W(h(Y(q(2))(s S 1
Try it online!
Ungolfed:
(d list
(q(args
args
)))
(d make-pow2
(q((S T)
(i
T
(make-pow2 (c (a (h S) (h S)) S) (s T 1))
S
)
)))
(d pow2 (make-pow2 (q(1 1)) 30))
(d times3
(q((U )
(a (a U U) U)
)))
(d make-pow3
(q((S T)
(i
T
(make-pow3 (c (times3 (h (t S))) S) (s T 1))
S
)
)))
(d pow3 (make-pow3 (q (1 1)) 38))
(d trim
(q((S T)
(i
(e T (q(1 1)))
S
(i
(l (h T) (a S 1))
(trim (s S (h T)) (t T))
(trim S (t T))
)
)
)))
(d div3?
(q((S)
(e 0 (trim S pow3))
)))
(d div2?
(q((S)
(e 0 (trim S pow2))
)))
(d div3*
(q((S T)
(i
(e T (q(1 1)))
S
(i
(l (h T) (a S 1))
(a (h (t (t T))) (div3* (s S (h T)) (t T)))
(div3* S (t T))
)
)
)))
(d div3
(q((S)
(div3* S pow3)
)))
(d G
(q((S)
(i
(div3? (s S 1))
(i
(e (div3 (s S 1)) 1)
(list (a S S))
(i
(div2? (div3 (s S 1)))
(list (a S S))
(list (div3 (s S 1)) (a S S))
)
)
(list (a S S))
)
)))
(d concat
(q((S T)
(i
S
(concat (t S) (c (h S) T))
T
)
)))
(d step*
(q((N acc)
(i
N
(step* (t N) (concat (G (h N)) acc ))
acc
)
)))
(d even
(q((S T)
(i
(e S ())
T
(odd (t S) (c (h S) T))
)
)))
(d odd
(q((S T)
(i
(e S ())
T
(even (t S) T)
)
)))
(d merge
(q((S T U)
(i
(e S ())
(i
(e T ())
U
(merge S (t T) (c (h T) U))
)
(i
(e T ())
(merge (t S) T (c (h S) U))
(i
(e (h S) (h T))
(merge (t S) (t T) (c (h S) U))
(i
(l (h S) (h T))
(merge (t S) T (c (h S) U))
(merge S (t T) (c (h T) U))
)
)
)
)
)))
(d rev
(q((S T)
(i
(e S ())
T
(rev (t S) (c (h S) T))
)
)))
(d sort
(q((S)
(i
(e () S)
S
(i
(e () (t S))
S
(rev (merge (sort(odd S ())) (sort (even S ())) ()) ())
)
)
)))
(d step
(q((N)
(i
N
(step* N ())
()
)
)))
(d stopping
(q((N S)
(i
(e S 0)
(list N S)
(stopping (step N) (s S 1))
)
)))
(d A
(q((S)
(i
(e S 0)
(q(1))
(sort (h (stopping (q(2)) (s S 1))) )
)
)))
Try it online!
Explanation
My initial approach was a naive one where I started from 2^n and counted down checking each number to see if it's stopping number was n. That was way too slow. After consulting other answers for inspiration, I switched to using the reverse Collatz function, generating the tree, and stopping after n iterations.
I discovered by experimenting, that the tree does not grow exponentially - although n*2 is always a hit, the (n-1)/3 path only hits occasionally. So by the time we get to n=30 we have about twice the number of integers that we want, but no more than that.
As a result, I put the sort and de-dup at the very end, and called it just once - this saves some execution time.
The sort is a merge sort - this makes it convenient to de-dup while merging.
The arithmetic operations required: divides-by-two? divides-by-three? and divide-by-three all had to be optimized - the naive implementations of these were too slow. To optimize, I created a list of powers of 2 and 3, and used these to make the operations faster.
Testing
My code has been tested and verified for n = 0,1,5,9,15 and 30. All are correct. n=30 runs in about 15 seconds on TIO.