In this blog post, I will show how to draw a beautiful FP-Tree data structure in a Latex document. The FP-Tree is a tree-like structure that was proposed in the FP-Growth algorithm for itemset mining, and is also used in many other pattern mining algorithms. I will show how to draw an FP-tree in Latex using the TIKZ library. An FP-tree consists of a table and a tree that are linked using some pointers (dashed arrows). The result is like this:
I took me a while to obtain this result, so here is the code:
\documentclass[a4paper, 12pt]{article} \usepackage[utf8]{inputenc} \usepackage{amssymb} \usepackage{amsmath} \usepackage{tikz} \usetikzlibrary{positioning,matrix} \begin{document} \begin{figure} \centering \begin{tikzpicture} %%%%% THE TREE \begin{scope}[->,font=\small,draw,circle, every node/.style={fill=white!10,shape=circle,draw}, edge from parent/.style={black,thick,draw}, level 1/.style={sibling distance=2.5cm}, level 2/.style={sibling distance=2.5cm}] \node (TREE) {root} child {node {$a$:3} child {node {$b$:1} } child {node {$c$:2} child {node{$d$:1}}}} child {node {$c$:2} child {node {$d$:2}} }; % Node links within the tree \draw[->, dashed] (TREE-1-2) -- (TREE-2); \draw[->, dashed] (TREE-1-2-1) -- (TREE-2-1); \end{scope} %%%%% THE TABLE \begin{scope}[xshift=-5cm,yshift=-2cm,every tree node/.style={shape=rectangle,draw}] \matrix (TABLE) [matrix of nodes, row sep=-\pgflinewidth, column sep=-\pgflinewidth, nodes={draw, text height=5mm, align=center, minimum width=15mm, inner sep=0mm, minimum height=7mm}]{ Item & Link\\ $a$ & ~\\ $b$ & ~ \\ $c$ & ~\\ $d$ & ~\\ }; \end{scope} %%%% LINKS FROM THE TABLE TO THE TREE \draw[->,dashed] (TABLE-4-2.center) to (TREE-1-2.west); \draw[->,dashed] (TABLE-5-2.center) to (TREE-1-2-1.west); \end{tikzpicture} \caption{An Example of FP-tree} \label{fig:my_label} \end{figure} \end{document} }
Hope that this will be useful! If you like it or if you want to suggest some improvement, please let me know in the comment section below or by e-mail.
—
Philippe Fournier-Viger is a distinguished professor working in China and founder of the SPMF open source data mining software.
Pingback: An Online Tool to Draw FP-Trees | The Data Blog