2

Here is the code:

\documentclass {article}
\usepackage {tikz}
\usetikzlibrary {positioning}
\definecolor {turboviole}{cmyk}{0.70,0,0,0}

\begin {document}
\begin {center}
\begin {tikzpicture}[-latex ,auto ,node distance =4 cm and 5cm ,on grid ,
semithick ,
state/.style ={ circle ,top color =white , bottom color = turboviole!20 ,
draw,turboviole , text=blue , minimum width =1 cm}]

% Left branch
\node[state] (root) {$[5,4,6,2,9,1,7,3]$};
\node[state] (A) [below left=of root] {$[4,1,2,3]$};
\node[state] (B) [below left=of A] {$[2,1,3]$};
\node[state] (C) [below left=of B] {$[1]$};
\node[state] (D) [below right=of B] {$[3]$};

% Call paths
\path (root) edge [bend right =25] node [below left=0.15cm] 
{$\textcolor{red}{Call 1},Pivot < 5$} (A);
\path (A) edge [bend right =25] node [below left =0.15 cm] {$\textcolor{red}
{Call 2}, Pivot <4$} (B);
\path (B) edge [bend right =25] node [below left =0.15 cm] {$\textcolor{red}
{Call 3}, Pivot <2$} (C);
\path (B) edge [bend left =25] node [below right=0.15 cm] {$\textcolor{red}
{Call 4}, Pivot >2$} (D);

% Returning paths
\path (C) edge [bend right =25] node [above right =0.15 cm {$\textcolor{green}{Return 1}$} (B);
\path (D) edge [bend left =25] node [above left =0.15 cm] {$\textcolor{green}{Return 2}$} (B);
\path (B) edge [bend right =25] node [above right =0.15 cm] {$\textcolor{green}{Return 3}$} (A);
\path (A) edge [bend right =25] node [above right =0.15 cm] {$\textcolor{green}{Return 4}$} (root);

% Right branch
\node[state] (E) [below right=of root] {$[6,9,7]$};
\node[state] (F) [below right=of E] {$[9,7]$};
\node[state] (G) [below right=of F] {$[7]$};

% Call Paths
\path (root) edge [bend left =25] node [below right=0.15cm] {$\textcolor{red}{Call 5}, Pivot > 5$} (E);
\path (E) edge [bend left =25] node [below right=0.15 cm] {$\textcolor{red}{Call 6}, Pivot >6$} (F);
\path (F) edge [bend left =25] node [below right=0.15 cm] {$\textcolor{red}{Call 7}, Pivot >9$} (G);

% Returning paths
% intentionally not included

\end{tikzpicture}
\end{center}
\end{document}

Result:

enter image description here

I would like your suggestions and recommendations regarding:

  • the missing right part of the graph. What am I doing wrong?

  • changing the colour of the edges to match text colour the in description nodesThe ones containing: Call and Return.. How to change colour of the edges?

  • how to place the text of the description nodes above the edges, so that it is visible?

Any additional constructive comment will be also appreciated!


Note: idea for the code borrowed from here.

Ignasi
  • 136,588
Ziezi
  • 455
  • 2
    The right part isn't really missing, your diagram is just so large that it extends outside the page. Try changing the node distance to for example node distance=3cm and 1.2cm. – Torbjørn T. Sep 06 '17 at 13:33
  • @TorbjørnT. Thank your for the comment, you are right, but the whole graph is, for some reason, also shifted to the right. – Ziezi Sep 06 '17 at 13:45
  • 2
    1cm then, instead of 1.2cm. It was a little bit wider than the text block (note the overfull hbox warning in the log, or add \usepackage{showframe} to see it) with 1.2cm, so it went into the right margin a bit. – Torbjørn T. Sep 06 '17 at 13:48
  • 2
    Add e.g. fill=white to the nodes you want 'above' the edges. Add e.g. [draw=blue] so you have edge [draw=blue] rather than just edge to add colour, or \draw [blue] if it is not an edge in TikZ's sense. – cfr Sep 07 '17 at 00:09

1 Answers1

2

To answer the question in the title: the right part isn't missing, it's just that your diagram is very wide, so you see the same as if you inserted an image that was 25cm wide -- it sticks into the right margin and out of the page. An easy fix is to set node distance=3cm and 1cm, instead of the 4cm and 5cm you have.

In the code below I made several manual adjustments to this though, to make better use of the space.

Regarding the colors, you could use edge[draw=red] to make an edge red, as cfr remarks in a comment. Alternatively, as you're adding both arrow and node in the same path, you could do \path [red] .... I do a variation of this below, where I use a scope environment and every path/.append style={}. Making the entire node red, of course means that Pivot N > 5 will be red as well, that can be fixed with a \color{black}.

There are other changes to the code as well, ask if anything is unclear.

(Note: the frame in the image is from the showframe package, and is merely there to show that the diagram is narrower than the text width. You'll want to remove the showframe package.)

output of code

\documentclass{article}
\usepackage{amsmath,showframe}
\usepackage{tikz}
\usetikzlibrary{positioning}
\definecolor{turboviole}{cmyk}{0.70,0,0,0}
\newcommand\callpivot[2]{Call #1,\\\color{black}$\text{Pivot #2} > 5$}
\begin{document}
\begin{center}
\begin{tikzpicture}[
   -latex,
   auto,
   node distance=3cm and 1cm,
   on grid,
   semithick,
   state/.style={
     circle,
     top color=white,
     bottom color=turboviole!20,
     draw,
     turboviole,
     text=blue,
     minimum width=1cm
   },
   call/.style={
     red,
     every node/.append style={align=center}
   },
   return/.style={green}
]

% Left branch
\node[state] (root) {$[5,4,6,2,9,1,7,3]$};
\node[state] (A) [below left=2cm and 3cm of root] {$[4,1,2,3]$};
\node[state] (B) [below left=3cm and 0cm of A] {$[2,1,3]$};
\node[state] (C) [below left=3cm and 2cm of B] {$[1]$};
\node[state] (D) [below right=3and 2cm of B] {$[3]$};

% Call paths
\begin{scope}[every path/.append style={call}]
\path (root) edge [bend right=25] node [above left]  {\callpivot{1}{5}} (A);
\path (A) edge [bend right=25] node [left] {\callpivot{2}{4}} (B);
\path (B) edge [bend right=25] node [above left] {\callpivot{3}{2}} (C);
\path (B) edge [bend left=25] node [above right] {\callpivot{4},{2}} (D);
\end{scope}

% Returning paths
\begin{scope}[every path/.append style={return}]
\path (C) edge [bend right=15] node [right,pos=0.1] {Return 1} (B);
\path (D) edge [bend left=15] node [left,pos=0.4] {Return 2} (B);
\path (B) edge [bend right=25] node [right] {Return 3} (A);
\path (A) edge [bend right=25] node [below right] {Return 4} (root);
\end{scope}

% Right branch
\node[state] (E) [below right=2cm and 3cm of root] {$[6,9,7]$};
\node[state] (F) [below=of E] {$[9,7]$};
\node[state] (G) [below=of F] {$[7]$};

% Call Paths
\begin{scope}[every path/.append style={call}]
\path (root) edge [bend left=25] node [above right] {\callpivot{5}{5}} (E);
\path (E) edge [bend left=25] node [right] {\callpivot{6}{6}} (F);
\path (F) edge [bend left=25] node [right] {\callpivot{7}{9}} (G);
\end{scope}

% Returning paths
% intentionally not included

\end{tikzpicture}
\end{center}
\end{document}
Torbjørn T.
  • 206,688