You’re correct about the iterator in both directions, fixed versions below.
◼
Define the iterator for the Collatz conjecture:
In[]:=
collatz[n_?EvenQ]:=n/2collatz[n_?OddQ]:=3n+1
◼
Compute the orbit of an integer:
In[]:=
collatzList[n_]:=NestWhileList[collatz,n,Not@*EqualTo[1]]
In[]:=
collatzList[25]
Out[]=
{25,76,38,19,58,29,88,44,22,11,34,17,52,26,13,40,20,10,5,16,8,4,2,1}
◼
Compute a graph of orbits:
In[]:=
collatzTreeGraph[n_]:=EdgeDelete[ResourceFunction["NestWhileGraph"][collatz,Range[n],Not@*ConnectedGraphQ@*UndirectedGraph,VertexLabels->Automatic],14]//LayeredGraph
In[]:=
collatzTreeGraph[25]
Out[]=
◼
For the reverse, first define the iterator that produces the branches:
In[]:=
anticollatz[n_]:=With[{m=(n-1)/3},If[OddQ[m]&&m>1,{2n,m},{2n}]]
◼
Compute the tree down to a certain level:
In[]:=
collatzTree[level_]:=NestTree[anticollatz,1,level]
In[]:=
collatzTree[12]
Out[]=