How to Draw Trees, Graphs, and State Machines in TikZ
Trees, graphs, and state machines are fundamental structures in computer science, linguistics, and mathematics. TikZ provides specialized libraries for each: the trees library for hierarchical structures, graphs for general graph drawing, and automata for finite state machines. This guide covers practical examples of all three. Bibby AI's instant TikZ rendering makes iterating on complex graph layouts dramatically faster than on Overleaf.
Draw Tree Diagrams
Use TikZ's built-in tree syntax or the forest package for complex hierarchical trees:
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{arrows.meta}
\begin{document}
% Basic TikZ tree
\begin{tikzpicture}[
level distance=1.5cm,
sibling distance=2.5cm,
every node/.style={draw, circle, minimum size=0.8cm},
edge from parent/.style={draw, -{Stealth[length=2mm]}},
level 1/.style={sibling distance=3cm},
level 2/.style={sibling distance=1.5cm}
]
\node {1}
child { node {2}
child { node {4} }
child { node {5} }
}
child { node {3}
child { node {6} }
child { node {7} }
};
\end{tikzpicture}
\end{document}Draw Graphs with Custom Layouts
Create graph structures with manual or automatic layouts for networks, dependency diagrams, and more:
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{arrows.meta, positioning}
\begin{document}
\begin{tikzpicture}[
vertex/.style={draw, circle, fill=blue!20, minimum size=0.7cm, font=\small},
edge/.style={thick},
>={Stealth[length=2.5mm]}
]
% Pentagon graph layout
\foreach \i/\label in {1/A, 2/B, 3/C, 4/D, 5/E} {
\node[vertex] (\label) at ({90+72*(\i-1)}:2cm) {\label};
}
% Edges (undirected graph)
\draw[edge] (A) -- (B);
\draw[edge] (B) -- (C);
\draw[edge] (C) -- (D);
\draw[edge] (D) -- (E);
\draw[edge] (E) -- (A);
% Diagonal edges
\draw[edge, dashed, red] (A) -- (C);
\draw[edge, dashed, red] (B) -- (D);
% Weighted edge label
\node[font=\scriptsize, above] at ($(A)!0.5!(B)$) {5};
\end{tikzpicture}
\end{document}Draw Finite State Machines (Automata)
Use the automata library to draw DFAs, NFAs, and other state machines with standard notation:
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{automata, arrows.meta, positioning}
\begin{document}
\begin{tikzpicture}[
shorten >=1pt,
node distance=3cm,
on grid,
>={Stealth[length=2.5mm]},
every state/.style={draw, minimum size=1cm},
accepting/.style={double, double distance=1.5pt}
]
% States
\node[state, initial] (q0) {$q_0$};
\node[state] (q1) [right=of q0] {$q_1$};
\node[state, accepting] (q2) [right=of q1] {$q_2$};
\node[state] (q3) [below=2cm of q1] {$q_3$};
% Transitions
\path[->]
(q0) edge node[above] {a} (q1)
(q0) edge[bend right] node[below left] {b} (q3)
(q1) edge node[above] {a} (q2)
(q1) edge node[right] {b} (q3)
(q2) edge[loop above] node {a,b} ()
(q3) edge[bend right] node[below right] {a} (q2)
(q3) edge[loop below] node {b} ();
\end{tikzpicture}
\end{document}💡 Tips
- •For complex trees, consider the forest package — it offers automatic layout and is more flexible than TikZ's built-in trees.
- •Use 'on grid' for state machines to ensure consistent spacing between states.
- •The initial and accepting styles are built into the automata library — no need to define them manually.
- •Use \foreach loops to generate repetitive graph structures programmatically, as shown in the pentagon example.
Try This in Bibby AI
Write LaTeX faster with AI auto-complete and instant compilation.
Start Writing Free