12

i want to draw the tree in the following picture :

enter image description here

i have write :

\documentclass{article}
\usepackage{tikz}
\usepackage{color}

 \definecolor{red}{rgb}{1,0,0} 
 \begin{document}

  \begin{tikzpicture}
  \tikzstyle{level 1}=[sibling distance=50mm]
  \tikzstyle{level 2}=[sibling distance=30mm]
   \node(a) {\textcolor{red}{2}|2}
        child {node(b) {\textcolor{red}{2}|4}
        child {node(f) {\textcolor{red}{2}|4}}
            child {node(g)[rectangle, draw] {
            \scalebox{0.6}{% size of the array
                 \begin{tabular}{c|c|c|c|c|c}
                            \textcolor{red}{2}|4 & \textcolor{red}{2}|4 & \textcolor{red}{2}|4 & \textcolor{red}{2}|4 & \textcolor{red}{2}|4 & \textcolor{red}{2}|4
                        \end{tabular}
                        }
            }}
            child {node(d) {\textcolor{red}{5} | 2}}
            }
   child {node(c)[rectangle, draw] {
\scalebox{0.8}{% size of the array
 \begin{tabular}{c|c|c|c|c|c}
            \textcolor{red}{2}|4 & \textcolor{red}{2}|4 & \textcolor{red}{2}|4 & \textcolor{red}{2}|4 & \textcolor{red}{2}|4 & \textcolor{red}{2}|4
        \end{tabular}
        }
}}
            child {node(e) {\textcolor{red}{7} | 2}
            child {node(f) {\textcolor{red}{2}|4}}
            child {node(g)[rectangle, draw] {
            \scalebox{0.6}{%
                 \begin{tabular}{c|c|c|c|c|c}
                            \textcolor{red}{2}|4 & \textcolor{red}{2}|4 & \textcolor{red}{2}|4 & \textcolor{red}{2}|4 & \textcolor{red}{2}|4 & \textcolor{red}{2}|4
                        \end{tabular}
                        }
              }}
              child {node(d) {\textcolor{red}{5} | 3}}
               }
  ;

  \draw[dashed] (a) -- (b);

\end{tikzpicture}
\end{document}

I get this result :

enter image description here

How can i proceed to draw the many egde form the array child of the parent to the other child , thank u in advance

5
  • 1
    Have you thought of using the rectangle split node shape? Although the PGF manual only shows the anchors of a vertically split node, there are probably also proper anchors for a horizontally split node (.text south, .two south, .three south, …?). Jan 19, 2013 at 23:57
  • 1
    You have repeated node names: d, f, g
    – hpesoj626
    Jan 19, 2013 at 23:59
  • 2
    If you were thinking about the red dashed lines parallel to the actual edges the /pgf/decoration/raise option may come in handy. Jan 20, 2013 at 0:03
  • @Qrrbrbirlbel i have put for example \draw[decorate,decoration={raise=6pt,amplitude=10pt}, thick] (a)--(b); but it does not work Jan 20, 2013 at 0:21
  • @WassimSboui For once, you have at least to specify a decoration (the manual uses crosses). On the other side decoration={lineto,raise=6pt} does not work either, so more effort will be needed to make that work. The next best thing would be a custom to path, I guess. Jan 20, 2013 at 0:44

1 Answer 1

12

Summary

  • Fixed the node names. For easier reference, I named the node from top to bottom, left to right with a,b,....
  • As suggested by Qrrbrbirlbel, I used the rectangle split node shape available in the library shapes.multipart instead of the tabular environment.
  • Instead of defining a new to path I used the brace decoration from the decorations.pathreplacing library instead and set amplitude=0pt. Hacky, but it works.
  • I removed \scalebox and replaced this with scale=0.8.
  • Update I encolsed each | with $...$. As mentioned by Qrrbrbirbel in comment, to draw the pipes: |, use \usepackage[T1]{fontenc} in your preamble: How to insert pipe symbol in (La)TeX?. For good measure, write also \usepackage[utf8]{inputenc}.
  • Instead of defining the coordinates in terms of the default ones in the rectangle split node shape, I defined them relative to only two coordinates of the node.
  • I replaced \tikzstyle, which is deprecated, with \tikzset.
  • There is no need to define red as color since this is already predefined. Also, there is no need to load the color package since tikz uses xcolor internally.

Updated Code

\documentclass[tikz,border=5]{standalone}
%\documentclass[10pt]{article}
%\usepackage[margin=1in]{geometry}
%\usepackage{tikz}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usetikzlibrary{shapes.multipart,calc,decorations.pathreplacing}

\tikzset{
    my shape 1/.style={circle, draw},
    my shape 2/.style={
        rectangle split, 
        rectangle split parts=#1, 
        draw, 
        anchor = center, 
        rectangle split horizontal},
}

\begin{document}
    \begin{tikzpicture} 
    \tikzset{level 1/.style={sibling distance=50mm},
             level 2/.style={sibling distance=30mm}}
    \node (a) [my shape 1] {\textcolor{red}{2}|2}
    child {node(b) [my shape 1] {\textcolor{red}{2}|4}
        child {node(e) [my shape 1] {\textcolor{red}{2}|4}}
        child {node(f)[my shape 2=6,scale=0.8] {
                \textcolor{red}{2}|4 \nodepart{two} \textcolor{red}{2}|4 \nodepart{three} \textcolor{red}{2}|4 \nodepart{four} \textcolor{red}{2}|4 \nodepart{five} \textcolor{red}{2}|4 \nodepart{six} \textcolor{red}{2}|4
            }}
        child {node(g) [my shape 1] {\textcolor{red}{5}|2}}
        }
    child {node(c)[my shape 2=6,scale=0.8] {
            \textcolor{red}{2}|4 \nodepart{two} \textcolor{red}{2}|4 \nodepart{three} \textcolor{red}{2}|4 \nodepart{four} \textcolor{red}{2}|4 \nodepart{five} \textcolor{red}{2}|4 \nodepart{six} \textcolor{red}{2}|4
            }}
    child {node(d) [my shape 1] {\textcolor{red}{7}|2}
        child {node(h) [my shape 1] {\textcolor{red}{2}|4}}
        child {node(i)[my shape 2=6,scale=0.8] {
                \textcolor{red}{2}|4 \nodepart{two} \textcolor{red}{2}|4 \nodepart{three} \textcolor{red}{2}|4 \nodepart{four} \textcolor{red}{2}|4 \nodepart{five} \textcolor{red}{2}|4 \nodepart{six} \textcolor{red}{2}|4
                    }}
        child {node(j) [my shape 1] {\textcolor{red}{5}$|$3}}
                };

    \foreach \xleft/\leftpos in {1/0.416,3/0.2083,5/.375,7/.542,9/.7083,11/.875} {
        \coordinate (c\xleft) at ($(c.south west)!\leftpos!(c.south east)$);
        \coordinate (f\xleft) at ($(f.north west)!\leftpos!(f.north east)$);}

    \foreach \x/\arrowcol in {1/blue,3/green,5/red,7/yellow,9/orange,11/purple}
        \draw [->,\arrowcol] (c\x)  .. controls ++(-90:0.5) and ++(0:0.5) .. ($(c\x)!.5!(f\x)$)
            ($(c\x)!.5!(f\x)$) .. controls ++(180:0.5) and ++(90:0.5) .. (f\x);

    \foreach \xright/\rightpos in {2/.125,4/.2917,6/.4583,8/.625,10/.7917,12/.9583} {
        \coordinate (c\xright) at ($(c.south west)!\rightpos!(c.south east)$);
        \coordinate (i\xright) at ($(i.north west)!\rightpos!(i.north east)$);}

    \foreach \x/\arrowcol in {2/blue,4/green,6/red,8/yellow,10/orange,12/purple}
        \draw [->,\arrowcol,dashed] (c\x)  .. controls ++(-90:0.5) and ++(0:0.5) .. ($(c\x)!.5!(i\x)$)
            ($(c\x)!.5!(i\x)$) .. controls ++(180:0.5) and ++(90:0.5) .. (i\x);

    \draw [postaction={draw,decorate,decoration={brace,raise=-3pt,amplitude=0pt},dashed,red}] (a) -- (b);
    \end{tikzpicture}

\coordinate (a*) at (c9);
\coordinate (b*) at ([yshift=-0.2cm]a*);
\draw (a*) -- (b*);
\draw ([xshift=-0.3cm]b*) -- ([xshift=0.3cm]b*);
\foreach \x in {-0.8,-0.4,0,.4,0.8}
\draw ([xshift=\x em]b*) -- +(-0.1,-0.2);
\end{document}

Updated Output

enter image description here

6

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .