# Binary Red Black Trees, adding a pointer?

I want to illustrate the algorithm for rotation on red-black trees. How can I draw an external (and temporarly) pointer on the leaf '5'. The arrow should must come from above-left with a label ('y' for example).

My (uncomplete) example

\documentclass{minimal}
\usepackage[utf8]{inputenc}
\usepackage{tikz}
\usetikzlibrary{arrows,automata}
\usetikzlibrary{positioning}

\tikzstyle{arn_n} = [circle, white, font=\bfseries, draw=black, fill=black, align=center, inner sep=0pt,text width=1.5em, text centered]% black node
\tikzstyle{arn_r} = [circle, red, draw=red, align=center, inner sep=0pt,text width=1.5em, text centered, very thick]% red node
\tikzstyle{arn_x} = [rectangle, draw=black, align=center, inner sep=0pt,minimum width=0.5em, minimum height=0.5em]% NIL 'node'

\begin{document}

\begin{tikzpicture}[->,>=stealth',level/.style={sibling distance = 5cm/#1, level distance = 1.5cm}]
\node [arn_n] {33}
child{ node [arn_r] {15}
child{ node [arn_n] {10}
child{ node [arn_r] {5}
edge from parent node[above left] {$x$}} % name of pointer
child{ node [arn_x] {}}
}
child{ node [arn_n] {20}
child{ node [arn_r] {18}}
child{ node [arn_x] {}}
}
}
child{ node [arn_r] {47}
child{ node [arn_n] {38}
child{ node [arn_r] {36}}
child{ node [arn_r] {39}}
}
child{ node [arn_n] {51}
child{ node [arn_r] {49}}
child{ node [arn_x] {}}
}
}
;
\end{tikzpicture}
\end{document}

-
A tip: instead of using the <pre> and </pre> tags, it's preferable to select the code and hit Ctrl-K or click the {} button; in this way you get colorized TeX syntax. – egreg Sep 27 '12 at 13:14
what do you mean with few more lines ? Wich information more ? – madit Sep 27 '12 at 13:21
Thanks to egreg for the tip – madit Sep 27 '12 at 13:22
@madit: It's generally preferred to post a compilable example document that can be compiled as it is. Otherwise, people who want to try to help first of have to do the work of completing the document. – Jake Sep 27 '12 at 13:22
@Jake : done ;-) I do not find how to add an immage of the result. – madit Sep 27 '12 at 13:32

You can name the required node to make it available in a \draw command, and then use something like

\draw [<-, ultra thick] (leaf 5) -- node [auto] {$y$} ++(135:1cm) ;


to add the arrow with the label:

\documentclass{minimal}
\usepackage[utf8]{inputenc}
\usepackage{tikz}
\usetikzlibrary{arrows,automata}
\usetikzlibrary{positioning}

\tikzstyle{arn_n} = [circle, white, font=\bfseries, draw=black, fill=black, align=center, inner sep=0pt,text width=1.5em, text centered]% black node
\tikzstyle{arn_r} = [circle, red, draw=red, align=center, inner sep=0pt,text width=1.5em, text centered, very thick]% red node
\tikzstyle{arn_x} = [rectangle, draw=black, align=center, inner sep=0pt,minimum width=0.5em, minimum height=0.5em]% NIL 'node'

\begin{document}

\begin{tikzpicture}[->,>=stealth',level/.style={sibling distance = 5cm/#1, level distance = 1.5cm}]
\node [arn_n] {33}
child{ node [arn_r] {15}
child{ node [arn_n] {10}
child{ node [arn_r, name=leaf 5] {5}
edge from parent node[above left] {$x$}} % name of pointer
child{ node [arn_x] {}}
}
child{ node [arn_n] {20}
child{ node [arn_r] {18}}
child{ node [arn_x] {}}
}
}
child{ node [arn_r] {47}
child{ node [arn_n] {38}
child{ node [arn_r] {36}}
child{ node [arn_r] {39}}
}
child{ node [arn_n] {51}
child{ node [arn_r] {49}}
child{ node [arn_x] {}}
}
}
;

\draw [<-, ultra thick] (leaf 5) -- node [auto] {$y$} ++(135:1cm) ;
\end{tikzpicture}
\end{document}

-
A VERY THANKS to you. But what mean the value '135' ? – madit Sep 27 '12 at 13:42
@madit: That's the angle of the line. You can specify points either in cartesian coordinates (x,y) or in polar coordinates (angle:distance). – Jake Sep 27 '12 at 13:49
hum ... i have a little problem. I have the same code but not the same result. On your image I see that the arrows doens't start from 'south' to 'north' but are well drawn on angle ! Not on my result :-( Something to configure in the preamble ? A conflict with an another package ? – madit Sep 27 '12 at 14:18
@madit: Did you copy and paste the document from my answer? Then you should get the same result. What version of PGF are you using? – Jake Sep 27 '12 at 14:22
well, in the minimal document ... no problem with the lines ! in the syllabus that I wrote, the result is very different. This is probably due to a conflict with another package or another tikz library. Which will be difficult to find! – madit Sep 27 '12 at 18:34