7

I have the following decision tree taken from this example with little modification. When I added two sub-nodes to the second right node, the inner left and inner right nodes have been overlapped. How can I separate those nodes?

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{positioning}
\newdimen\nodeDist
\nodeDist=35mm
\begin{document}

\begin{tikzpicture}[
    node/.style={%
      draw,
      rectangle,
    },
  ]

    \node [node] (A) {SimilarityScore1};
    \path (A) ++(-135:\nodeDist) node [node] (B){loooooongeteeext};
    \path (A) ++(-45:\nodeDist) node [node] (C) {loooooongeteeext};
    \path (C) ++(-135:\nodeDist) node [node] (D) {loooooongeteeext};
    \path (C) ++(-45:\nodeDist) node [node] (E) {loooooongeteeext};
       \path (B) ++(-135:\nodeDist) node [node] (F) {loooooongeteeext};
    \path (B) ++(-45:\nodeDist) node [node] (G) {loooooongeteeext};

    \draw (A) -- (B) node [left,pos=0.25] {$<= 0.904712$}(A);
    \draw (A) -- (C) node [right,pos=0.25] {$> 0.904712$}(A);
    \draw (C) -- (D) node [left,pos=0.25] {$<= 0.896089$}(A);
    \draw (C) -- (E) node [right,pos=0.20] {$> 0.896089$}(A);
    \draw (B) -- (F) node [left,pos=0.2] {$<= 0.997372$}(A);
    \draw (B) -- (G) node [right,pos=0.20] {$> 0.997372$}(A);
\end{tikzpicture}

\end{document}

Thanks in advance

4

You have to change either the lengths of the connectors or the angles. Or both, here's a suggestion that does both to achieve something I'd find reasonable:

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{positioning}
\newdimen\nodeDist
\nodeDist=25mm
\newdimen\nodeDistUpper
\nodeDistUpper=40mm
\begin{document}

\begin{tikzpicture}[
    node/.style={%
      draw,
      rectangle,
    },
  ]

    \node [node] (A) {SimilarityScore1};
    \path (A) ++(-157.5:\nodeDistUpper) node [node] (B){loooooongeteeext};
    \path (A) ++(-22.5:\nodeDistUpper) node [node] (C) {loooooongeteeext};
    \path (C) ++(-135:\nodeDist) node [node] (D) {loooooongeteeext};
    \path (C) ++(-45:\nodeDist) node [node] (E) {loooooongeteeext};
    \path (B) ++(-135:\nodeDist) node [node] (F) {loooooongeteeext};
    \path (B) ++(-45:\nodeDist) node [node] (G) {loooooongeteeext};

    \draw (A) -- (B) node [left,pos=0.25] {$\leq 0.904712$}(A);
    \draw (A) -- (C) node [right,pos=0.25] {$> 0.904712$}(A);
    \draw (C) -- (D) node [left,pos=0.25] {$\geq 0.896089$}(A);
    \draw (C) -- (E) node [right,pos=0.20] {$> 0.896089$}(A);
    \draw (B) -- (F) node [left,pos=0.2] {$\leq 0.997372$}(A);
    \draw (B) -- (G) node [right,pos=0.20] {$> 0.997372$}(A);
\end{tikzpicture}

\end{document}

enter image description here

5

another diferently approach. since your diagram is tree (decision) diagram, it can be easy draw by help of the forest package:

\documentclass[border=5mm]{standalone}
\usepackage{forest}

\begin{document}
\forestset{EL/.style 2 args={edge label={%
    node[midway, font=\footnotesize, 
         inner sep=2pt, anchor=south #1]{$#2$}},
                     },
        }% end of forestset

\begin{forest}
for tree={
  draw,
  l sep=17 mm,
  s sep= 1 mm,
        }% end of for tree
[SimilarityScore1
    [loooooongeteeext, EL={east}{\leq 0.904712},
        [loooooongeteeext,EL={east}{\leq 0.896089}]
        [loooooongeteeext,EL={west}{\geq 0.896089}]
    ]
    [loooooongeteeext, EL={west}{\geq 0.904712}
        [loooooongeteeext,EL={east}{\leq 0.896089}]
        [loooooongeteeext,EL={west}{\geq 0.896089}]
    ]
]
\end{forest}
\end{document}

enter image description here

addendum: based on answers on my question is possible to slightly improve (make shorter code for tree) above mwe:

\documentclass[border=3mm, preview]{standalone}
\usepackage{forest}

\begin{document}
\tikzset{ELS/.style={% Edge Label Style
      font=\footnotesize\sffamily, inner sep=2pt,
      anchor=south #1, % label position: "ELS=west" or "EL=east"
      pos=0.6}
        }
    \begin{forest}
for tree={
    draw,
    semithick,
    parent anchor=children,
  l sep=13 mm,
  s sep=4mm,
    where level=1{s sep=1mm}{} % <---
        },
   EL/.style = {% <---
   before typesetting nodes={%
    where n=1{%
      edge label/.wrap value={node[ELS=east]{$#1$}}
    }{%
      edge label/.wrap value={node[ELS=west]{$#1$}}
    }
                            }
                }
[SimilarityScore1
    [loooooongeteeext, EL=\leq 0.904712    
        [loooooongeteeext,EL=\leq 0.896089]
        [loooooongeteeext,EL=\geq 0.896089]
    ]
    [loooooongeteeext, EL=\geq 0.904712
        [loooooongeteeext,EL=\leq 0.896089]
        [loooooongeteeext,EL=\geq 0.896089]
    ]
]
    \end{forest}
\end{document}

which gives nicer result:

enter image description here

4

An entirely differnt approach that would allow you to make this whole approach a bit more general is to do something like this. This would also help you if you have more than one trees and they do not all share the exact same structure

\documentclass{article}
\usepackage{tikz}
\begin{document}
    \begin{tikzpicture}[-,thick,
    level 1/.style={sibling distance=6cm},
    level 2/.style={sibling distance=2.5cm}
    ]
    \footnotesize
    \node {similarity score}
        child {node {looooong text 1} 
            child {node {looooong text 1a}} 
            child {node {looooong text 1b}}
        }
        child {node {looooong text 2} 
            child {node {looooong text 2a}}
            child {node {looooong text 2b}}
        }
    ;
    \end{tikzpicture}
\end{document}

You can also add boxes and labels

\documentclass{article}
\usepackage{tikz}
\begin{document}
    \begin{tikzpicture}[-,thick,
    level 1/.style={sibling distance=6cm},
    level 2/.style={sibling distance=2.5cm}, 
  box/.style={draw=blue!80}
    ]
    \footnotesize
    \node[box] {similarity score}
        child {node[box] {looooong text 1} 
            child {node[box] {looooong text 1a} edge from parent node[left] {$<3$} } 
            child {node[box] {looooong text 1b} edge from parent node[right] {$\geq3$} }
            edge from parent node[left] {$<3$}
        }
        child {node[box] {looooong text 2} 
            child {node[box] {looooong text 2a} edge from parent node[left] {$<3$}}
            child {node[box] {looooong text 2b} edge from parent node[right] {$\geq3$}}
            edge from parent node[right] {$\geq3$}
        }
    ;
    \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.