LaTeX

TikZ: Huffman Tree

ForceCore 2009. 4. 22. 15:36

How cute :D

\begin{tikzpicture}[
level distance=15mm,
level 1/.style={sibling distance=60mm},
level 2/.style={sibling distance=30mm},
level 3/.style={sibling distance=15mm},
level 4/.style={sibling distance=15mm}
]

\usetikzlibrary{shapes}
\tikzstyle{c} = [draw, shape=circle]
\tikzstyle{r} = [draw, shape=rectangle,minimum width=10mm]
\tikzstyle{tr} = [draw,isosceles triangle, shape border rotate=90, anchor=north]

\node[c]{100}[edge from parent]
    child {node[r] {a:45}
        edge from parent coordinate (ea);
    }
    child {node[c]{55}
        child {node[c]{25}
            child{node[r]{c:12}
                edge from parent coordinate (ec);
            }
            child{node[r]{b:13}
                edge from parent coordinate (eb);
            }
            edge from parent coordinate (e25);
        }
        child {node[c]{30}
            child{node[c]{14}
                child{node[r]{f:5}
                    edge from parent coordinate (ef);
                }
                child{node[r]{e:9}
                    edge from parent coordinate (ee);
                }
                edge from parent coordinate (e14);
            }
            child{node[r]{d:16}
                edge from parent coordinate (e16);
            }
            edge from parent coordinate (e30);
        }
        edge from parent coordinate (e55);
    };
% code labels
\node at([yshift=2mm]ea)  {0};
\node at([yshift=2mm]e55) {1};
\node at([xshift=-2mm,yshift=2mm]e25) {0};
\node at([xshift=2mm,yshift=2mm]e30) {1};
\node at([xshift=-2mm,yshift=1mm]ec) {0};
\node at([xshift=2mm,yshift=1mm]eb) {1};
\node at([xshift=-2mm,yshift=1mm]e14) {0};
\node at([xshift=2mm,yshift=1mm]e16) {1};
\node at([xshift=-2mm,yshift=1mm]ef) {0};
\node at([xshift=2mm,yshift=1mm]ee) {1};
\end{tikzpicture}

Edge에 code label 적는게 좀 귀찮고 헷갈림.