4

I have drawn a Calkin–Wilf tree without "For" but I want to draw this tree with loop, instead.

\documentclass[10pt]{article}
\usepackage{mathtools}
\usepackage{pgf,tikz}
\begin{document}
\begin{tikzpicture}[x=1cm,y=1.2cm]
\begin{scope}[line width=1.5pt,draw=red,font=\Huge]
%%%%%سطر صفر و یک
\node (A11) at (0,0) {$\dfrac{1}{1}$};
\node (A12) at (-3,0) {$\frac{1}{2}$};
\node (A21) at (3,0) {$\frac{2}{1}$};
\draw (A12) -- (A11) -- (A21);
%%%
\end{scope}
\begin{scope}[line width=1.5pt,draw=red!90,font=\huge]
%%%سطر دو
\node (A31) at (3,3) {$\frac{3}{1}$};
\node (A23) at (3,-3) {$\frac{2}{3}$};
\draw (A31) -- (A21) -- (A23);

\node (A13) at (-3,3) {$\frac{1}{3}$};
\node (A32) at (-3,-3) {$\frac{3}{2}$};
\draw (A13) -- (A12) -- (A32);
%%%
%%%
\end{scope}
\begin{scope}[line width=1.5pt,draw=red!80,font=\LARGE]
%%%سطر سوم
\node (A43) at (-4.5,3) {$\frac{4}{3}$};
\node (A14) at (-1.5,3) {$\frac{1}{4}$};
\draw (A43) -- (A13) -- (A14);
%%%
%%%
\node (A35) at (-4.5,-3) {$\frac{3}{5}$};
\node (A52) at (-1.5,-3) {$\frac{5}{2}$};
\draw (A35) -- (A32) -- (A52);
%%%
%%%
\node (A41) at (1.5,3) {$\frac{4}{1}$};
\node (A34) at (4.5,3) {$\frac{3}{4}$};
\draw (A41) -- (A31) -- (A34);
%%%
%%%
\node (A53) at (4.5,-3) {$\frac{5}{3}$};
\node (A25) at (1.5,-3) {$\frac{2}{5}$};
\draw (A25) -- (A23) -- (A53);
%%%
%%%
\end{scope}
\begin{scope}[line width=1.5pt,draw=red!70,font=\Large] 
%%%سطر چهارم
\node (A47) at (-4.5,4.5) {$\frac{4}{7}$};
\node (A73) at (-4.5,1.5) {$\frac{7}{3}$};
\draw (A47) -- (A43) -- (A73);
%%%
%%% 
\node (A54) at (-1.5,4.5) {$\frac{5}{4}$};
\node (A15) at (-1.5,1.5) {$\frac{1}{5}$};
\draw (A15) -- (A14) -- (A54);
%%%
%%% 

\node (A74) at (4.5,4.5) {$\frac{7}{4}$};
\node (A37) at (4.5,1.5) {$\frac{3}{7}$};
\draw (A74) -- (A34) -- (A37);
%%%
%%% 
\node (A45) at (1.5,4.5) {$\frac{4}{5}$};
\node (A51) at (1.5,1.5) {$\frac{5}{1}$};
\draw (A51) -- (A41) -- (A45);
%%%
%%% 

\node (A85) at (-4.5,-4.5) {$\frac{8}{5}$};
\node (A38) at (-4.5,-1.5) {$\frac{3}{8}$};
\draw (A85) -- (A35) -- (A38);
%%%
%%% 
\node (A57) at (-1.5,-4.5) {$\frac{5}{7}$};
\node (A72) at (-1.5,-1.5) {$\frac{7}{2}$};
\draw (A57) -- (A52) -- (A72);
%%%
%%% 

\node (A58) at (4.5,-4.5) {$\frac{5}{8}$};
\node (A83) at (4.5,-1.5) {$\frac{8}{3}$};
\draw (A58) -- (A53) -- (A83);
%%%
%%% 
\node (A75) at (1.5,-4.5) {$\frac{7}{5}$};
\node (A27) at (1.5,-1.5) {$\frac{2}{7}$};
\draw (A75) -- (A25) -- (A27);
%%%
%%% 
\end{scope}
\begin{scope}[line width=1.5pt,draw=red!60]
%%%سطر پنجم
%%%%%بالایی ها
\node (A710) at (-5.25,1.5) {$\frac{7}{10}$};
\node (A103) at (-3.75,1.5) {$\frac{10}{3}$};
\draw (A710) -- (A73) -- (A103);
%%%
%%% 
\node (A117) at (-5.25,4.5) {$\frac{11}{7}$};
\node (A411) at (-3.75,4.5) {$\frac{4}{11}$};
\draw (A117) -- (A47) -- (A411);
%%%
%%%
\node (A16) at (-2.25,1.5) {$\frac{1}{6}$};
\node (A65) at (-.75,1.5) {$\frac{6}{5}$};
\draw (A16) -- (A15) -- (A65);
%%%
%%% 
\node (A94) at (-2.25,4.5) {$\frac{9}{4}$};
\node (A59) at (-.75,4.5) {$\frac{5}{9}$};
\draw (A59) -- (A54) -- (A94);
%%%
%%%

\node (A107) at (5.25,1.5) {$\frac{10}{7}$};
\node (A310) at (3.75,1.5) {$\frac{3}{10}$};
\draw (A107) -- (A37) -- (A310);
%%%
%%% 
\node (A711) at (5.25,4.5) {$\frac{7}{11}$};
\node (A114) at (3.75,4.5) {$\frac{11}{14}$};
\draw (A711) -- (A74) -- (A114);
%%%
%%%
\node (A61) at (2.25,1.5) {$\frac{6}{1}$};
\node (A56) at (.75,1.5) {$\frac{5}{6}$};
\draw (A61) -- (A51) -- (A56);
%%%
%%% 
\node (A49) at (2.25,4.5) {$\frac{4}{9}$};
\node (A95) at (.75,4.5) {$\frac{9}{5}$};
\draw (A95) -- (A45) -- (A49);
%%%
%%%
%%%%%پایینی ها
\node (A811) at (5.25,-1.5) {$\frac{8}{11}$};
\node (A113) at (3.75,-1.5) {$\frac{11}{3}$};
\draw (A811) -- (A83) -- (A113);
%%%
%%% 
\node (A138) at (5.25,-4.5) {$\frac{13}{8}$};
\node (A513) at (3.75,-4.5) {$\frac{5}{13}$};
\draw (A513) -- (A58) -- (A138);
%%%
%%%
\node (A29) at (2.25,-1.5) {$\frac{2}{9}$};
\node (A97) at (.75,-1.5) {$\frac{9}{7}$};
\draw (A29) -- (A27) -- (A97);
%%%
%%% 
\node (A125) at (2.25,-4.5) {$\frac{12}{5}$};
\node (A712) at (.75,-4.5) {$\frac{7}{12}$};
\draw (A125) -- (A75) -- (A712);
%%%
%%%

\node (A118) at (-5.25,-1.5) {$\frac{11}{8}$};
\node (A311) at (-3.75,-1.5) {$\frac{3}{11}$};
\draw (A118) -- (A38) -- (A311);
%%%
%%% 
\node (A813) at (-5.25,-4.5) {$\frac{8}{13}$};
\node (A135) at (-3.75,-4.5) {$\frac{13}{5}$};
\draw (A813) -- (A85) -- (A135);
%%%
%%%
\node (A92) at (-2.25,-1.5) {$\frac{9}{2}$};
\node (A79) at (-.75,-1.5) {$\frac{7}{9}$};
\draw (A92) -- (A72) -- (A79);
%%%
%%% 
\node (A512) at (-2.25,-4.5) {$\frac{5}{12}$};
\node (A127) at (-.75,-4.5) {$\frac{12}{7}$};
\draw (A512) -- (A57) -- (A127);
%%%
%%%

\end{scope}
\begin{scope}[line width=1.5pt,draw=red!50]
%%%سطر ششم
%%%%بالا چپ
%%%%بالا چپ
\node (A1118) at (-5.25,5.25) {$\frac{11}{18}$};
\node (A187) at (-5.25,3.75) {$\frac{18}{7}$};
\draw (A1118) -- (A117) -- (A187);
%%%
%%%
\node (A1511) at (-3.75,5.25) {$\frac{15}{11}$};
\node (A415) at (-3.75,3.75) {$\frac{4}{15}$};
\draw (A1511) -- (A411) -- (A415);
%%%
%%%
%%%سطر ششم
%%%%بالا چپ
%%%%پایین چپ
\node (A717) at (-5.25,2.25) {$\frac{7}{17}$};
\node (A1710) at (-5.25,.75) {$\frac{17}{10}$};
\draw (A717) -- (A710) -- (A1710);
%%%
%%%
\node (A133) at (-3.75,2.25) {$\frac{13}{3}$};
\node (A1013) at (-3.75,.75) {$\frac{10}{13}$};
\draw (A133) -- (A103) -- (A1013);
%%%
%%%
%%%سطر ششم
%%%%بالا چپ
%%%%بالا راست
\node (A913) at (-2.25,5.25) {$\frac{9}{13}$};
\node (A134) at (-2.25,3.75) {$\frac{13}{4}$};
\draw (A913) -- (A94) -- (A134);
%%%
%%%
\node (A149) at (-.75,5.25) {$\frac{14}{9}$};
\node (A514) at (-.75,3.75) {$\frac{5}{14}$};
\draw (A149) -- (A59) -- (A514);
%%%
%%%
%%%سطر ششم
%%%%بالا چپ
%%%%پایین راست
\node (A17) at (-2.25,2.25) {$\frac{1}{7}$};
\node (A76) at (-2.25,.75) {$\frac{7}{6}$};
\draw (A17) -- (A16) -- (A76);
%%%
%%%
\node (A115) at (-.75,2.25) {$\frac{11}{5}$};
\node (A611) at (-.75,.75) {$\frac{6}{11}$};
\draw (A115) -- (A65) -- (A611);
%%%
%%%



%%%سطر ششم
%%%%بالا راست
%%%%بالا راست
\node (A1811) at (5.25,5.25) {$\frac{18}{11}$};
\node (A718) at (5.25,3.75) {$\frac{7}{18}$};
\draw (A1811) -- (A711) -- (A718);
%%%
%%%
\node (A1115) at (3.75,5.25) {$\frac{11}{15}$};
\node (A154) at (3.75,3.75) {$\frac{15}{4}$};
\draw (A1115) -- (A114) -- (A154);
%%%
%%%
%%%سطر ششم
%%%%بالا راست
%%%%پایین راست
\node (A177) at (5.25,2.25) {$\frac{17}{7}$};
\node (A1017) at (5.25,.75) {$\frac{10}{17}$};
\draw (A177) -- (A107) -- (A1017);
%%%
%%%
\node (A313) at (3.75,2.25) {$\frac{3}{13}$};
\node (A1310) at (3.75,.75) {$\frac{13}{10}$};
\draw (A313) -- (A310) -- (A1310);
%%%
%%%
%%%سطر ششم
%%%%بالا راست
%%%%بالا چپ
\node (A139) at (2.25,5.25) {$\frac{13}{9}$};
\node (A413) at (2.25,3.75) {$\frac{4}{13}$};
\draw (A139) -- (A49) -- (A413);
%%%
%%%
\node (A914) at (.75,5.25) {$\frac{9}{14}$};
\node (A145) at (.75,3.75) {$\frac{14}{5}$};
\draw (A914) -- (A95) -- (A145);
%%%
%%%
%%%سطر ششم
%%%%بالا راست
%%%%پایین چپ
\node (A71) at (2.25,2.25) {$\frac{7}{1}$};
\node (A67) at (2.25,.75) {$\frac{6}{7}$};
\draw (A71) -- (A61) -- (A67);
%%%
%%%
\node (A511) at (.75,2.25) {$\frac{5}{11}$};
\node (A116) at (.75,.75) {$\frac{11}{5}$};
\draw (A511) -- (A56) -- (A116);
%%%
%%%



%%%سطر ششم
%%%%پایین چپ
%%%%پایین چپ
\node (A2113) at (-5.25,-5.25) {$\frac{21}{13}$};
\node (A821) at (-5.25,-3.75) {$\frac{8}{21}$};
\draw (A2113) -- (A813) -- (A821);
%%%
%%%
\node (A1318) at (-3.75,-5.25) {$\frac{13}{18}$};
\node (A185) at (-3.75,-3.75) {$\frac{18}{5}$};
\draw (A1318) -- (A135) -- (A185);
%%%
%%%
%%%سطر ششم
%%%%پایین چپ
%%%%بالا چپ
\node (A198) at (-5.25,-2.25) {$\frac{19}{8}$};
\node (A1119) at (-5.25,-.75) {$\frac{11}{19}$};
\draw (A198) -- (A118) -- (A1119);
%%%
%%%
\node (A314) at (-3.75,-2.25) {$\frac{3}{14}$};
\node (A1411) at (-3.75,-.75) {$\frac{14}{11}$};
\draw (A314) -- (A311) -- (A1411);
%%%
%%%
%%%سطر ششم
%%%%پایین چپ
%%%%پایین راست
\node (A1712) at (-2.25,-5.25) {$\frac{17}{12}$};
\node (A517) at (-2.25,-3.75) {$\frac{5}{17}$};
\draw (A1712) -- (A512) -- (A517);
%%%
%%%
\node (A1219) at (-.75,-5.25) {$\frac{12}{19}$};
\node (A197) at (-.75,-3.75) {$\frac{19}{7}$};
\draw (A1219) -- (A127) -- (A197);
%%%
%%%
%%%سطر ششم
%%%%پایین چپ
%%%%بالا راست
\node (A112) at (-2.25,-2.25) {$\frac{11}{2}$};
\node (A911) at (-2.25,-.75) {$\frac{9}{11}$};
\draw (A112) -- (A92) -- (A911);
%%%
%%%
\node (A716) at (-.75,-2.25) {$\frac{7}{16}$};
\node (A169) at (-.75,-.75) {$\frac{16}{9}$};
\draw (A716) -- (A79) -- (A169);
%%%
%%%



%%%سطر ششم
%%%%پایین راست
%%%%پایین راست
\node (A1321) at (5.25,-5.25) {$\frac{13}{21}$};
\node (A218) at (5.25,-3.75) {$\frac{21}{8}$};
\draw (A1321) -- (A138) -- (A218);
%%%
%%%
\node (A1813) at (3.75,-5.25) {$\frac{18}{13}$};
\node (A518) at (3.75,-3.75) {$\frac{5}{18}$};
\draw (A1813) -- (A513) -- (A518);
%%%
%%%
%%%سطر ششم
%%%%پایین راست
%%%%بالا راست
\node (A819) at (5.25,-2.25) {$\frac{8}{19}$};
\node (A1911) at (5.25,-.75) {$\frac{19}{11}$};
\draw (A819) -- (A811) -- (A1911);
%%%
%%%
\node (A143) at (3.75,-2.25) {$\frac{14}{3}$};
\node (A1114) at (3.75,-.75) {$\frac{11}{14}$};
\draw (A143) -- (A113) -- (A1114);
%%%
%%%
%%%سطر ششم
%%%%پایین راست
%%%%پایین چپ
\node (A1217) at (2.25,-5.25) {$\frac{12}{17}$};
\node (A175) at (2.25,-3.75) {$\frac{17}{15}$};
\draw (A1217) -- (A125) -- (A175);
%%%
%%%
\node (A1912) at (.75,-5.25) {$\frac{19}{12}$};
\node (A719) at (.75,-3.75) {$\frac{7}{19}$};
\draw (A1912) -- (A712) -- (A719);
%%%
%%%
%%%سطر ششم
%%%%پایین راست
%%%%بالا چپ
\node (A211) at (2.25,-2.25) {$\frac{2}{11}$};
\node (A119) at (2.25,-.75) {$\frac{11}{9}$};
\draw (A211) -- (A29) -- (A119);
%%%
%%%
\node (A167) at (.75,-2.25) {$\frac{16}{7}$};
\node (A916) at (.75,-.75) {$\frac{9}{16}$};
\draw (A167) -- (A97) -- (A916);
%%%
%%%
\end{scope}


\end{tikzpicture}
\end{document}

enter image description here

  • Do you mean you want to use a loop to draw the tree? – Torbjørn T. Dec 24 '16 at 14:26
  • It's not a for loop but lindenmayer system rule definition you need to invent I think. – percusse Dec 24 '16 at 16:09
  • Yes. I want to use a loop to draw the tree. – hosein Dec 24 '16 at 16:16
  • 1
    This is a labeled H-tree you can google for growth rules or use tex.stackexchange.com/questions/6258/… – percusse Dec 24 '16 at 16:52
8

Some variation on this perhaps?

\documentclass[tikz,border=5]{standalone} 
\usetikzlibrary{lindenmayersystems}
\newcount\cwtcounta
\newcount\cwtcountb
\newcount\cwtlevel
\pgfdeclarelindenmayersystem{calkin-wilf tree}{
  \symbol{I}{%
    \cwtcounta=1
    \cwtcountb=1
    \cwtlevel=1
  }
  \symbol{F}{%
    \path [every calkin-wilf tree branch/.try] (0,0) -- (\pgflsystemstep,0);
    \pgflsystemdrawforward
    \pgflsystemstep=0.707106\pgflsystemstep
    \advance\cwtlevel by1
  }
  \symbol{P}{%
    \advance\cwtcounta by\cwtcountb
  }
  \symbol{Q}{%
    \advance\cwtcountb by\cwtcounta
  }
  \symbol{N}{%
    \node [every calkin-wilf tree node/.try] 
      {$\frac{\the\cwtcounta}{\the\cwtcountb}$};
  }
  \symbol{+}{\pgflsystemturnright} 
  \symbol{-}{\pgflsystemturnleft}
  \rule{S -> IN}
  \rule{N -> [-FPN][+FQN]N}
}
\tikzset{
  calkin-wilf tree/.style={
    lindenmayer system={calkin-wilf tree, axiom=+S, angle=90, #1},
    insert path={lindenmayer system}
  },
  every calkin-wilf tree node/.style={
    fill=white, inner sep=0.25ex, scale=1/sqrt(\cwtlevel)
  },
  every calkin-wilf tree branch/.style={
    draw=red
  }
}
\begin{document} 
\begin{tikzpicture}[xscale=0.75]
\path [calkin-wilf tree={order=7, step=2cm}]; 
\end{tikzpicture}
\end{document}

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.