Creating a Collatz tree

Introduction

This Mathematica notebook is licensed under a MouseOver . It provides a command showtree that constructs the function tree of the Collatz function generated by a set of integers. I hope anyone interested will feel free to improve this work and to use it in their own publications and coursework. 

This notebook is discussed in the post Generating a Collatz tree.

Define the function

cf[1]:=1;cf[n_Integer]:=If[ EvenQ[n],n/2,3n+1]

cf/@Range[1,10]

Find entries generated by an integer

fv[gl_List,n_Integer]:=(nn:=n;ggl:={nn};(Sow[1];While[!MemberQ[ggl,cf[nn]],(Sow[nn];Sow[cf[nn]]);nn=cf[nn]])//Reap)(*Sow[1] first to keep the output from being empty*)

findall[gl_List]:=((fv[gl,#][[2]][[1]]&/@gl)//Flatten//Union)

findall[{2,3,4}]

findall[{2,3}]

#->cf[#]&/@findall[{2,3,4}]

Construct the tree

showtree[gl_List]:=TreePlot[Drop[#->cf[#]&/@findall[gl],1],Bottom,1,VertexLabeling->True,ImageSize->Medium]

Examples

showtree[{2,3,17,21,23,22,24}]

showtree[Range[2,26]]

showtree[Range[2,21]]

showtree[Range[15,19]]

showtree[{29,37}]

showtree[Select[Range[2,26],!PrimeQ[#]&]]

showtree[Select[Range[2,26],OddQ[#]&]]

showtree[Select[Range[2,26],EvenQ[#]&]]

showtree[Select[Range[2,50],EvenQ[#]&]]