4

I am drawing a tree in tikz (similar to binary search tree). I use for loops to create the nodes and edges. After I create them, I would like to add description to some nodes and edges. With nodes, I don't have an issue as I can reference them. For edges, how would I reference a specific edge so I can put a label next to it without having to redraw that edge.

enter image description here

\documentclass{minimal}
\usepackage{tikz}
\tikzset{>=stealth}

\begin{document}
\begin{tikzpicture}[darkstyle/.style={circle,fill=black,minimum size=15}]
  \pgfmathsetmacro{\scale}{1.5} % Scale of distance between nodes
  \pgfmathtruncatemacro{\maxDepth}{4} % max depth

  \pgfmathtruncatemacro{\maxDepthLessOne}{\maxDepth-1} % max depth - 1
  \pgfmathtruncatemacro{\maxDepthLessTwo}{\maxDepth-2} % max depth - 2
  \pgfmathtruncatemacro{\halfway}{\maxDepthLessOne/2} % floor((max depth - 1)/2)
  \pgfmathtruncatemacro{\otherhalfway}{\maxDepthLessOne-\halfway} % ceil((max depth - 1)/2)

  % create nodes
  \foreach \curDepth in {0,...,\maxDepthLessOne} % current depth
    \foreach \pos in {0,...,\curDepth} % horizontal
    {
      \pgfmathsetmacro{\verPos}{1-\curDepth}
      \pgfmathsetmacro{\horPos}{\pos-\curDepth/2}
      \node [darkstyle] (node\curDepth\pos) at (\scale*\horPos,\scale*\verPos) {};
    }

  % create edges
  \foreach \curDepth in {0,...,\maxDepthLessTwo} % current depth
    \foreach \pos in {0,...,\curDepth} % horizontal
    {
      \pgfmathtruncatemacro{\nextDepth}{\curDepth+1}
      \pgfmathtruncatemacro{\nextPos}{\pos+1}
      \draw [->,blue,ultra thick] (node\curDepth\pos)--(node\nextDepth\pos);
      \draw [->,red,ultra thick] (node\curDepth\pos)--(node\nextDepth\nextPos);
    }

  \node [left=8] at (node20) {testt}; %testt

\end{tikzpicture}
\end{document}
4

Another option is to use coordinate during creation:

\documentclass{minimal}
\usepackage{tikz}
\usetikzlibrary{positioning}
\tikzset{>=stealth}

\begin{document}
\begin{tikzpicture}[darkstyle/.style={circle,fill=black,minimum size=15}]
  \pgfmathsetmacro{\scale}{1.5} % Scale of distance between nodes
  \pgfmathtruncatemacro{\maxDepth}{4} % max depth

  \pgfmathtruncatemacro{\maxDepthLessOne}{\maxDepth-1} % max depth - 1
  \pgfmathtruncatemacro{\maxDepthLessTwo}{\maxDepth-2} % max depth - 2
  \pgfmathtruncatemacro{\halfway}{\maxDepthLessOne/2} % floor((max depth - 1)/2)
  \pgfmathtruncatemacro{\otherhalfway}{\maxDepthLessOne-\halfway} % ceil((max depth - 1)/2)

  % create nodes
  \foreach \curDepth in {0,...,\maxDepthLessOne} % current depth
    \foreach \pos in {0,...,\curDepth} % horizontal
    {
      \pgfmathsetmacro{\verPos}{1-\curDepth}
      \pgfmathsetmacro{\horPos}{\pos-\curDepth/2}
      \node [darkstyle] (node\curDepth\pos) at (\scale*\horPos,\scale*\verPos) {};
    }

  % create edges
  \foreach \curDepth in {0,...,\maxDepthLessTwo} % current depth
    \foreach \pos in {0,...,\curDepth} % horizontal
    {
      \pgfmathtruncatemacro{\nextDepth}{\curDepth+1}
      \pgfmathtruncatemacro{\nextPos}{\pos+1}
      \draw [->,blue,ultra thick] (node\curDepth\pos)--(node\nextDepth\pos)coordinate[midway] (ArrowA\curDepth_\pos);
      \draw [->,red,ultra thick] (node\curDepth\pos)--(node\nextDepth\nextPos) coordinate[midway] (ArrowB\curDepth_\pos);
    }

  \node[left] at (node20) {testt}; %testt
  \node [left={of ArrowB1_0}] {\begin{minipage}{1.2cm}Text Added!\end{minipage}};
\end{tikzpicture}
\end{document}

enter image description here

  • I am guessing I can't do it without a shift because of how I drew the edges. – Topa Aug 9 at 6:08
  • @Topa ... You can, the problem was with my text width ... But using a minipage will work (see edit). Then just a really small shift could help... But anchor=east or left could work as expected – koleygr Aug 9 at 6:16
6

One possibility is to use calc to determine the middle between nodes. You could also use \path together with, say, midway.

\documentclass{minimal}
\usepackage{tikz}
\usetikzlibrary{calc}
\tikzset{>=stealth}

\begin{document}
\begin{tikzpicture}[darkstyle/.style={circle,fill=black,minimum size=15}]
  \pgfmathsetmacro{\scale}{1.5} % Scale of distance between nodes
  \pgfmathtruncatemacro{\maxDepth}{4} % max depth

  \pgfmathtruncatemacro{\maxDepthLessOne}{\maxDepth-1} % max depth - 1
  \pgfmathtruncatemacro{\maxDepthLessTwo}{\maxDepth-2} % max depth - 2
  \pgfmathtruncatemacro{\halfway}{\maxDepthLessOne/2} % floor((max depth - 1)/2)
  \pgfmathtruncatemacro{\otherhalfway}{\maxDepthLessOne-\halfway} % ceil((max depth - 1)/2)

  % create nodes
  \foreach \curDepth in {0,...,\maxDepthLessOne} % current depth
    \foreach \pos in {0,...,\curDepth} % horizontal
    {
      \pgfmathsetmacro{\verPos}{1-\curDepth}
      \pgfmathsetmacro{\horPos}{\pos-\curDepth/2}
      \node [darkstyle] (node\curDepth\pos) at (\scale*\horPos,\scale*\verPos) {};
    }

  % create edges
  \foreach \curDepth in {0,...,\maxDepthLessTwo} % current depth
    \foreach \pos in {0,...,\curDepth} % horizontal
    {
      \pgfmathtruncatemacro{\nextDepth}{\curDepth+1}
      \pgfmathtruncatemacro{\nextPos}{\pos+1}
      \draw [->,blue,ultra thick] (node\curDepth\pos)--(node\nextDepth\pos);
      \draw [->,red,ultra thick] (node\curDepth\pos)--(node\nextDepth\nextPos);
    }

  \node [left=8] at (node20) {testt}; %testt
  \node [left=5] at ($(node20)!0.5!(node10)$) {text}; %testt
  \path (node11) -- (node22) node[midway,right=5] {text}; %testt
\end{tikzpicture}
\end{document}

enter image description here

  • 1
    Great Suggestion. However, the position is not that accurate and would be difficult for non-trivial paths. There was another answer by @koleygr which is exactly what I needed (even better). Hopefully, he can resubmit his answer or I'll post it later. – Topa Aug 9 at 5:37
  • @Topa try \path (node10) -- (node20) node[midway,anchor=east] {text}; %test it is the same... Undeleting my answer for 5 minutes too... – koleygr Aug 9 at 5:40
  • @koleygr Yeah that would be a solution for my trivial example but your other solution gives me a reference to the paths for more complex cases. – Topa Aug 9 at 5:53
  • 1
    Ok @Topa ... I improve that for your case ... But sure someone else could make it without shifts too. – koleygr Aug 9 at 5:58
0

I see that we can do it naturally with some usual TikZ's commands. Using xscale=.75 to make figure narrower. There is no edge in the below code. We can use foreach to shorten the code a bit.

enter image description here

\documentclass[tikz,border=5mm]{standalone}
\begin{document}
\begin{tikzpicture}[%xscale=.75,
n/.style={circle,fill,minimum size=5mm},
ar/.style={line width=2pt,-stealth}]
\def\a{2}
\path
(0,0)       node[n] (1)  {}
++(-120:\a) node[n] (2a) {}
+(0:\a)     node[n] (2b) {}
++(-120:\a) node[n] (3a) {}
+(0:\a)     node[n] (3b) {}
+(0:2*\a)   node[n] (3c) {}
++(-120:\a) node[n] (4a) {}
+(0:\a)     node[n] (4b) {}
+(0:2*\a)   node[n] (4c) {}
+(0:3*\a)   node[n] (4d) {};

\draw[ar,blue] (1)--(2a); 
\draw[ar,blue] (2a)--(3a); 
\draw[ar,blue] (2b)--(3b); 
\draw[ar,blue] (3a)--(4a);
\draw[ar,blue] (3b)--(4b);
\draw[ar,blue] (3c)--(4c);

\draw[ar,red] (1)--(2b);
\draw[ar,red] (2a)--(3b);
\draw[ar,red] (2b)--(3c);
\draw[ar,red] (3a)--(4b);
\draw[ar,red] (3b)--(4c);
\draw[ar,red] (3c)--(4d);

\draw[ar,blue] (2a)--(3a) 
node[left=2mm,black]{The left of 3rd row!} 
node[pos=.3,align=right,red,left=2mm]{YOU WANT\\TEXT HERE};
\end{tikzpicture}
\end{document}

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.