1

I'm trying to illustrate a process of an algorithm over a Binary Search Tree, for which I need to make various figures where each time, different nodes are highlighted.

I wish to highlight these nodes using a vertical arrow that points to them, with a description text above that arrow, as well as color (gray-scale, since the document is intended for printing) highlight of the given node.

This is the code I use for my basic Binary Search Tree(*):

\begin{tikzpicture}[level/.style={sibling distance=30mm/#1}]
\node [circle,draw] (a){$26$}
    child {node [circle,draw] (b){$15$}
        child {node [circle,draw] (c){$9$}}
        child {node [circle,draw] (d){$20$}
            child {node [circle,draw] (e){$19$}}
            child {node [circle,draw] (f){$24$}}
        }
    }
    child {node [circle,draw] (g){$31$}
        child[missing]
        child {node [circle,draw] (i){$35$}
            child[missing]
            child {node [circle,draw] (k){$40$}}
        }
    };
\end{tikzpicture}

This code, when compiled (in context) will produce the following result:

My original BST

What I want to achieve is something like this:

The BST I'd like to get

I've found this question, which seeks a similar solution, however I don't wish to adjust the style of an existing graph edge, but to add a label that points to a vertex. Is there a way to do it without creating this label as a Tree vertex?

Thanks in advance!

(*) It is only a small part of the entire document. Based on an example by Manuel Kirsch, which can be found here.

Khaloymes
  • 267

2 Answers2

2

The code

child {node [circle,draw] (i){$35$}

will define the node labelled i

So you have just to complete your tikzpicture like this, for example:

\node[above=1cm of i] (new one) {Some text};
\draw[->,>=angle 60,ultra thick] (new one) -- (i);

This may require to load the positioning library, see Tikz manual

Edit: to make a colored node, you just have to add the relevant keys in the definition of the node (node[...](i){}). I'll let you search a little :-)

  • I've managed to compile your answer with few changes: \node[above of=i] (new one) {Some text}; \draw[->,thick] (new one) -- (i);, which is good enough for me. Thanks a lot! – Khaloymes Nov 23 '12 at 14:14
1

After reading the answer provided by Lionel MANSUY, I've gone to read into the TikZ/PGF manual that can be found here (Warning: 3.75MB PDF!). On page 87 of this manual, there is an example on specifying node locations on a grid.

After working around the example, I've managed to create a solution to my original question above, using the following code:

\begin{tikzpicture}[fill=gray!20]

    \draw[style=help lines,color=white] (0,0) grid (7,3);

    \path   (2,3) node(a) [circle,draw] {$26$}
            (1,2) node(b) [circle,draw] {$15$}
            (0,1) node(c) [circle,draw] {$9$}
            (2,1) node(d) [circle,draw] {$20$}
            (1,0) node(e) [circle,draw] {$19$}
            (3,0) node(f) [circle,draw] {$24$}
            (3,2) node(g) [circle,draw,fill] {$31$}
            (4,1) node(h) [circle,draw,fill] {$35$}
            (5,0) node(i) [circle,draw] {$40$}

            (7,2) node(j) [rectangle,draw] {u}
            (7,1) node(k) [rectangle,draw] {Successor(u)};

    \draw (a) -- (b);
    \draw (b) -- (c);
    \draw (b) -- (d);
    \draw (d) -- (e);
    \draw (d) -- (f);
    \draw (a) -- (g);
    \draw (g) -- (h);
    \draw (h) -- (i);

    \draw[->,thick] (j) -- (g);
    \draw[->,thick] (k) -- (h);

\end{tikzpicture}

This code, when compiled (in context) will produce the following result:

Grid version of my BST

This result is good enough for what I was trying to achieve, but on the other hand, the parent-child dependency of the Binary Search Tree is lost in the solution, therefore, editing the Binary Search Tree this way will be less con\mfortable had I used the code in my question.

EDIT: Please note that in my original question, I've asked for a solution to draw an arrow atop of a node, using the grid based solution I've provided in this answer, I've put these arrows to the right of the nodes I wanted to be pointed at. If I'll put nodes (j) and (k) atop of the nodes, the arrows will point down instead of pointing left.

Khaloymes
  • 267