LaTeX forum ⇒ Graphics, Figures & TablesPrime Factor Tree Help Topic is solved

Information and discussion about graphics, figures & tables in LaTeX documents.
tpa77l3
Posts: 19
Joined: Mon Feb 24, 2020 9:49 pm

Prime Factor Tree Help

Postby tpa77l3 » Wed Mar 25, 2020 7:00 am

Hi guys,

Hope your all keeping well under the circumstances.

I'd like a bit of help creating the following factor tree in the shown format. Try as I might, I'm getting a bit overwhelmed by the apparent possible complexity of it all! Thanks in advance :)

Screenshot 2020-03-25 at 04.58.33.png
Screenshot 2020-03-25 at 04.58.33.png (179.3 KiB) Viewed 593 times


Screenshot 2020-03-25 at 04.59.24.png
Screenshot 2020-03-25 at 04.59.24.png (26.46 KiB) Viewed 593 times

User avatar
Ijon Tichy
Posts: 296
Joined: Mon Dec 24, 2018 10:12 am

Postby Ijon Tichy » Wed Mar 25, 2020 9:23 am

You can use TikZ for this, and make either a forest or a tree. There are other specialized packages for trees.

tpa77l3
Posts: 19
Joined: Mon Feb 24, 2020 9:49 pm

Postby tpa77l3 » Thu Apr 02, 2020 6:44 am

Thanks for the reply.

I spent some time playing around trying to alter this example:

http://www.texample.net/tikz/examples/merge-sort-recursion-tree/

but have come unstuck when approaching the end. It's more than likely because I don't fully understand the syntax even after looking through the tikz manual. For whatever reason the tree will not carry on consistently after '105' and I am unable to remove the expressions on the right without it messing up the whole tree. Any ideas of where I'm going wrong?

Code: [Select all] [Expand/Collapse] [Download] ({untitled.tex})
  1. \documentclass[a4paper,landscape]{scrartcl}
  2. \usepackage{fancybox}
  3. \usepackage{tikz}
  4.  
  5. \title{MergeSort-RecursionTree}
  6. \author{Manuel Kirsch}
  7. \date{}
  8. \begin{document}
  9.  
  10. \begin{tikzpicture}[level/.style={sibling distance=60mm/#1}]
  11. \node {3780}
  12. child {node [circle,draw] {2}
  13. }
  14. child {node{1890}
  15. child {node [circle,draw] {2}
  16. }
  17. child {node {945}
  18. child {node [circle,draw] {3}
  19. }
  20. child {node {315}
  21. child {node [circle,draw] {3}
  22. }
  23. child {node {105}
  24. child {node [circle,draw] {3}
  25. }
  26. child {node {35}
  27. child {node [circle,draw] {3}
  28. }
  29. child [grow=right] {node (q) {$=$} edge from parent[draw=none]
  30. child [grow=right] {node (q) {$O_{k = \lg n}(n)$} edge from parent[draw=none]
  31. child [grow=up] {node (r) {$\vdots$} edge from parent[draw=none]
  32. child [grow=up] {node (s) {$O_2(n)$} edge from parent[draw=none]
  33. child [grow=up] {node (t) {$O_1(n)$} edge from parent[draw=none]
  34. child [grow=up] {node (u) {$O_0(n)$} edge from parent[draw=none]}
  35. }
  36. }
  37. }
  38. child [grow=down] {node (v) {$O(n \cdot \lg n)$}edge from parent[draw=none]}
  39. }
  40. }
  41. }
  42. }
  43. }
  44. };
  45. \end{tikzpicture}
  46.  
  47. \end{document}

Bartman
Posts: 97
Joined: Fri Jan 03, 2020 2:39 pm

Postby Bartman » Thu Apr 02, 2020 3:17 pm

I think in this case the example confuses a beginner rather than helping him. It is more advisable to start with a simple example and expand it until you reach your target.

In your example, a missing closing brace causes an error message:

Code: [Select all] [Expand/Collapse] [Download] ({untitled.tex})
  1. \documentclass[tikz, border=5pt]{standalone}
  2.  
  3. \begin{document}
  4. \begin{tikzpicture}[
  5. circled/.style={draw, circle}
  6. ]
  7. \node {3780}
  8. child {node [circled] {2}}
  9. child {node{1890}
  10. child {node [circled] {2}}
  11. child {node {945}
  12. child {node [circled] {3}}
  13. child {node {315}
  14. child {node [circled] {3}}
  15. child {node {105}
  16. child {node [circled] {3}}
  17. child {node {35}
  18. child {node [circled] {3}}
  19. child {node [circled] {7}}
  20. }
  21. }
  22. }
  23. }
  24. };
  25. \end{tikzpicture}
  26. \end{document}


Another solution with the forest package:

Code: [Select all] [Expand/Collapse] [Download] ({untitled.tex})
  1. \documentclass[border=5pt, preview]{standalone}
  2. \usepackage{forest}
  3.  
  4. \begin{document}
  5. \begin{forest}
  6. for tree={
  7. if n children=0{draw, circle}{},
  8. s sep*=3,
  9. l sep*=2
  10. }
  11. [3780
  12. [2]
  13. [1890
  14. [2]
  15. [945
  16. [3]
  17. [315
  18. [3]
  19. [105
  20. [3]
  21. [35
  22. [3]
  23. [7]
  24. ]
  25. ]
  26. ]
  27. ]
  28. ]
  29. ]
  30. \end{forest}
  31. \end{document}

tpa77l3
Posts: 19
Joined: Mon Feb 24, 2020 9:49 pm

Postby tpa77l3 » Fri Apr 03, 2020 6:43 am

This is great - many thanks. I guess you'd use one over the other for different situations where the complexity is greater?

Whats the best way to scale something like this down so that it doesn't take up a whole page?

Bartman
Posts: 97
Joined: Fri Jan 03, 2020 2:39 pm

Postby Bartman » Fri Apr 03, 2020 7:31 pm

The size of the drawing without the forest package can be changed in the following ways:

  • command \resizebox
  • command \scalebox or
  • the options scale and transform shape of the tikzpicture environment

Drawings with forest can only be scaled with the commands. The following source code must be inserted in the preamble so that the drawing is not shifted to the right by white space.

Code: [Select all] [Expand/Collapse] [Download] ({untitled.tex})
  1. \makeatletter
  2. \preto\forest@pack{\nullfont}
  3. \makeatother


The source of the code.

tpa77l3
Posts: 19
Joined: Mon Feb 24, 2020 9:49 pm

Postby tpa77l3 » Sat Apr 04, 2020 6:26 am

Frustratingly I'm even having trouble with this! Could you please provide an example of \scalebox when not using the forest option?

Why doesn't this work (taken from: https://latex.org/forum/viewtopic.php?t=14944)?

Code: [Select all] [Expand/Collapse] [Download] ({untitled.tex})
  1. \documentclass[tikz, border=5pt]{standalone}
  2. \usepackage{graphicx}
  3. \usepackage{tikz}
  4. \begin{document}
  5. \scalebox{1.5}
  6. \begin{tikzpicture}[
  7. circled/.style={draw, circle}
  8. ]
  9. \node {3780}
  10. child {node [circled] {2}}
  11. child {node{1890}
  12. child {node [circled] {2}}
  13. child {node {945}
  14. child {node [circled] {3}}
  15. child {node {315}
  16. child {node [circled] {3}}
  17. child {node {105}
  18. child {node [circled] {3}}
  19. child {node {35}
  20. child {node [circled] {3}}
  21. child {node [circled] {7}}
  22. }
  23. }
  24. }
  25. }
  26. };
  27. \end{tikzpicture}}
  28. \end{document}

Bartman
Posts: 97
Joined: Fri Jan 03, 2020 2:39 pm

Postby Bartman » Sat Apr 04, 2020 2:58 pm

First of all, the respondent of the other topic does not use the standalone class. It is not necessary to load the graphicx package as this is already done by pgf/TikZ.

The documentation graphicx and the example of the other topic show the structure of the \scalebox command. Make sure that all required braces have been set.

tpa77l3
Posts: 19
Joined: Mon Feb 24, 2020 9:49 pm

Postby tpa77l3 » Sun Apr 05, 2020 5:59 am

Got it :) Once again, many thanks for your help.


Return to “Graphics, Figures & Tables”

Who is online

Users browsing this forum: No registered users and 3 guests