Graphics, Figures & TablesPrime Factor Tree Help

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

Prime Factor Tree Help

Post by tpa77l3 »

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 7145 times
Screenshot 2020-03-25 at 04.59.24.png
Screenshot 2020-03-25 at 04.59.24.png (26.46 KiB) Viewed 7145 times

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org

NEW: TikZ book now 40% off at Amazon.com for a short time.

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

Prime Factor Tree Help

Post by Ijon Tichy »

You can use TikZ for this, and make either a forest or a tree. There are other specialized packages for trees.
Sorry, but I can no longer participate here as the administrator is trampling on my wishes on one of his other platforms. :cry:
tpa77l3
Posts: 19
Joined: Mon Feb 24, 2020 9:49 pm

Prime Factor Tree Help

Post by tpa77l3 »

Thanks for the reply.

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

http://www.texample.net/tikz/examples/m ... sion-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

\documentclass[a4paper,landscape]{scrartcl}
\usepackage{fancybox}
\usepackage{tikz}

\title{MergeSort-RecursionTree}
\author{Manuel Kirsch}
\date{}
\begin{document}

\begin{tikzpicture}[level/.style={sibling distance=60mm/#1}]
\node {3780}
  child {node [circle,draw] {2}
  }
  child {node{1890}
  child {node [circle,draw] {2}
  }
  child {node {945}
  child {node [circle,draw] {3}
  }
  child {node {315}
  child {node [circle,draw] {3}
  }
  child {node {105}
  child {node [circle,draw] {3}
  }
  child {node {35}
  child {node [circle,draw] {3}
  }
        child [grow=right] {node (q) {$=$} edge from parent[draw=none]
          child [grow=right] {node (q) {$O_{k = \lg n}(n)$} edge from parent[draw=none]
            child [grow=up] {node (r) {$\vdots$} edge from parent[draw=none]
              child [grow=up] {node (s) {$O_2(n)$} edge from parent[draw=none]
                child [grow=up] {node (t) {$O_1(n)$} edge from parent[draw=none]
                  child [grow=up] {node (u) {$O_0(n)$} edge from parent[draw=none]}
                }
              }
            }
            child [grow=down] {node (v) {$O(n \cdot \lg n)$}edge from parent[draw=none]}
          }
        }
      }
    }
  }
};
\end{tikzpicture}

\end{document}
Bartman
Posts: 369
Joined: Fri Jan 03, 2020 2:39 pm

Prime Factor Tree Help

Post by Bartman »

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

\documentclass[tikz, border=5pt]{standalone}

\begin{document}
\begin{tikzpicture}[
    circled/.style={draw, circle}
]
    \node {3780}
        child {node [circled] {2}}
        child {node{1890}
            child {node [circled] {2}}
            child {node {945}
                child {node [circled] {3}}
                child {node {315}
                    child {node [circled] {3}}
                    child {node {105}
                        child {node [circled] {3}}
                        child {node {35}
                            child {node [circled] {3}}
                            child {node [circled] {7}}
                        }
                    }
                }
            }
        };
\end{tikzpicture}
\end{document}
Another solution with the forest package:

Code: Select all

\documentclass[border=5pt, preview]{standalone}
\usepackage{forest}

\begin{document}
\begin{forest}
    for tree={
        if n children=0{draw, circle}{},
        s sep*=3,
        l sep*=2
    }
    [3780
        [2]
        [1890
            [2]
            [945
                [3]
                [315
                    [3]
                    [105
                        [3]
                        [35
                            [3]
                            [7]
                        ]
                    ]
                ]
            ]
        ]
    ]
\end{forest}
\end{document}
tpa77l3
Posts: 19
Joined: Mon Feb 24, 2020 9:49 pm

Prime Factor Tree Help

Post by tpa77l3 »

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: 369
Joined: Fri Jan 03, 2020 2:39 pm

Prime Factor Tree Help

Post by Bartman »

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

\makeatletter
\preto\forest@pack{\nullfont}
\makeatother
The source of the code.
tpa77l3
Posts: 19
Joined: Mon Feb 24, 2020 9:49 pm

Prime Factor Tree Help

Post by tpa77l3 »

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

\documentclass[tikz, border=5pt]{standalone}
\usepackage{graphicx}
\usepackage{tikz}
\begin{document}
\scalebox{1.5}
\begin{tikzpicture}[
    circled/.style={draw, circle}
]
    \node {3780}
        child {node [circled] {2}}
        child {node{1890}
            child {node [circled] {2}}
            child {node {945}
                child {node [circled] {3}}
                child {node {315}
                    child {node [circled] {3}}
                    child {node {105}
                        child {node [circled] {3}}
                        child {node {35}
                            child {node [circled] {3}}
                            child {node [circled] {7}}
                        }
                    }
                }
            }
        };
\end{tikzpicture}}
\end{document}
Bartman
Posts: 369
Joined: Fri Jan 03, 2020 2:39 pm

Prime Factor Tree Help

Post by Bartman »

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

Prime Factor Tree Help

Post by tpa77l3 »

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