4

I am creating a decision tree with flow chart in the using the tikz-package. A MWE is as follow:

\documentclass{article}
\usepackage[latin1]{inputenc}
\usepackage{tikz}
\usetikzlibrary{shapes,arrows}
\usetikzlibrary{positioning}
\newdimen\nodeDist
\nodeDist=35mm
\begin{document}
\pagestyle{empty}
\tikzstyle{block} = [rectangle, draw, fill=white!20,
    text width=10em, text centered, rounded corners, minimum height=4em]
\tikzstyle{line} = [draw, very thick, color=black!50, -latex']
\begin{tikzpicture}[
    node/.style={%
      draw,
         },
  ]
    \node [node] (A) {Both HF and LF waves important?};
    \path (A) ++(-135:\nodeDist) node [node] (B) {Phase averaged};
    \path (B) ++(-90:\nodeDist) node [node] (I) {nonlinearity?};
    \path (A) ++(-45:\nodeDist) node [node] (C) {Phase resolving};
    \path (C) ++(-90:\nodeDist) node [node] (D) {Is it complex?};
    \path (D) ++(-90:\nodeDist) node [node] (E) {Spectral?};
    \path (E) ++(-45:\nodeDist) node [node] (F) {NH, Boussinesq};
    \path (E) ++(-135:\nodeDist) node [node] (G) {Large gradient?};
    \path (G) ++(-45:\nodeDist) node [node] (H) {mild slope};
    \path (I) ++(-135:\nodeDist) node [node] (J) {SWE + groups};
    \path (I) ++(-45:\nodeDist) node [node] (K) {energies};

    \draw (A) -- (B) node [left,pos=0.25] {no}(A);
    \draw (A) -- (C) node [right,pos=0.25] {yes}(A);
    \draw (D) -- (E) node [right , pos = 0.25]{yes/no} (D);
    \draw (E) -- (F) node [right, pos = 0.25]{yes} (E);
    \draw (E) -- (G) node [left, pos = 0.25]{no} (E);
    \draw (G) -- (F) node [above, pos = 0.5]{yes} (G);
    \draw (G) -- (H) node [right, pos=0.25]{no} (G);
    \draw (D) -- (I) node [above , pos = 0.5]{no*} (D);
    \draw (I) -- (J) node [left, pos = 0.25]{yes} (I);
    \draw (I) -- (K) node [right, pos = 0.25]{no} (I);
    \draw (B) -- (I);
    \draw (C) -- (D) ;
\end{tikzpicture}
\end{document}

which looks like this: enter image description here

The problems associated with this decision tree are:

  • there are no arrows from one path (node) to another.
  • the blocks (or nodes) does not have rounded corners as I have defined.
  • the decision tree looks a little bit clumsy, I do not like the diagonal lines. I prefer straight lines.
  • The end of the left part (phase averaged) does not have the same 'ending height' as the 'phase-resolving' part.

Preferably, I would like to have something that looks like this (more organised, less clumsy):

enter image description here

I am already stuck on it for a while. How to proceed further?

4

I don't understand all the desiderata. I also don't know if an alternative approach is an option. But, for whatever it is worth, here's a forest solution.

This satisfies the first 3 desiderata, in addition to applying the defined styles correctly. Whether it satisfies the fourth of the desiderata I cannot say. Since I don't understand that one, if it satisfies it, it does so purely by accident. If not, that is entirely to be expected.

\documentclass[border=10pt,multi,tikz]{standalone}
\usepackage[edges]{forest}
\usetikzlibrary{arrows.meta}% arrows is deprecated
\begin{document}
\tikzset{% \tikzstyle is deprecated
  block/.style={rectangle, draw, fill=white!20, text width=10em, text centered, rounded corners, minimum height=4em},
  line/.style={draw, very thick, color=black!50, -Stealth},
}
\begin{forest}
  arrow to/.style n args=2{%
    delay={%
      tikz+={%
        \draw [every edge, line] () -- (!#1) node [above, midway] {#2};
      },
    },
    !u.s sep+=30pt,
  },
  before typesetting nodes={%
    where n=1{%
      edge label/.wrap value={%
        node [left,pos=.75, anchor=mid east] {#1}
      },
    }{%
      edge label/.wrap value={%
        node [right,pos=.75, anchor=mid west] {#1}
      },
    },
  },
  for tree={%
    parent anchor=children,
    child anchor=parent,
    block,
    edge={line},
    l sep+=10pt,
  },
  forked edges
  [Both HF and LF waves important?
    [Phase averaged, edge label=no
      [nonlinearity?
        [SWE + groups, edge label=yes
        ]
        [energies, edge label=no
        ]
      ]
    ]
    [Phase resolving, edge label=yes
      [Is it complex?, arrow to={us1}{no*}
        [Spectral?, edge label={yes/no}
          [Large gradient?, edge label=no
            [, phantom
            ]
            [mild slope, edge label=no
            ]
          ]
          [{NH, Boussinesq}, edge label=yes, arrow to={s}{yes}
          ]
        ]
      ]
    ]
  ]
\end{forest}    
\end{document}

forest solution

3

enter image description here

Image actually present tree (as it also stated with selected tags of question), but was drawn as general graph. Wit use of tree way of drawing, code become very simple and concise:

\documentclass[tikz, border=3mm]{standalone}
\usetikzlibrary{arrows, quotes, %trees
               }

\usepackage[latin1]{inputenc}

\begin{document}
    \begin{tikzpicture}[
    box/.style = {rectangle, draw, align=center},
level distance = 18mm,
level 1/.style = {sibling distance=66mm},
level 2/.style = {sibling distance=32mm},
level 4/.style = {sibling distance=44mm},
edge from parent/.style = {draw, -latex'},
%edge from parent fork down
                        ]
\node [block] {Both HF and LF\\ waves important?}
    child{ node [block] {Phase averaged}
        child{ node (C1) [block] {nonlinearity?}
            child{  node [block] {SWE + groups}   edge from parent node[left] {yes} }
            child{  node [block] {energies}       edge from parent node[right] {no} }
            }
        edge from parent node[left] {no} }
%
    child{ node [block] {Phase resolving}
        child{ node (C2) [block] {Is it complex?}
            child{ node  [block] {Spectral?}
                child{ node (D1) [block] {Large gradient?}
                    child [missing] { node {} }
                    child{  node [block] {mild slope}
                        edge from parent node[right] {no} }
                edge from parent node[left] {no} }
                 child{ node (D2) [block] {NH, Bossiness}
                    edge from parent node[right] {yes} }
            edge from parent node[right] {yes/no} }
            }
        edge from parent node[right] {yes}
        };
\draw[line] (C2) to ["no"] (C1);
\draw[line] (D2) to ["yes"] (D1);
\end{tikzpicture}
\end{document}

Edit: @cfr encourage me to make some style changes to above code. Now I make nodes with rounded corners, use name block for node shapes as exist (but not used) in OP's MWE, also is added definition for line and used in definition of style for edge from parent.

if you like to have lines between blocks in style as are in @cfr answer, than you need to add on the end of tikzpicture option

edge from parent fork down

and to TikZ libraries add library trees (bot are in above code, but commented by %). In this case you will get:

enter image description here

  • @cfr, than you for encouraging me to improve my answer :-). I have a problem wit understanding question too, so I decided to follow image in question. I will change the names and accommodate style of lines to OP wises. – Zarko Jul 25 '16 at 13:17
  • Great! And something. – cfr Jul 25 '16 at 17:42

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.