如何在 tikz 中的节点下绘制一个条形图?

如何在 tikz 中的节点下绘制一个条形图?

我有以下 tikz 模板(在文件中template.tex),其中我定义了一个 tikzpicture

\def\mysearchtree{
\begin{tikzpicture}[-, >=stealth', level/.style={sibling distance = 4cm/##1, level distance = 2cm}] 

\node [circle_node] (A) {$1$}
child { node [circle_node] (B) {$2$}    
    child { node [circle_node] (D) {$3$} }
    child { node [double_circle_node] (E) {$5$} }
}
child { node [circle_node] (C) {$3$}    
    child { node [circle_node] (F) {$1$} }
    child { node [circle_node] (G) {$4$} }
};


% Add labels to nodes
\node [blue, above] at (A.north) {$[1]$};
\node [blue, above left] at (B.north) {$[2]$};
\node [blue, above right] at (C.north) {$[3]$};
\node [blue, above left] at (D.north) {$[4]$};
\node [blue, above right] at (E.north) {$[5]$};

\end{tikzpicture}
}

在我的主文件中,main.tex有以下代码

\documentclass{article}

\usepackage{tikz}
\usetikzlibrary{arrows}
\usepackage[margin=1.5cm]{geometry}

% Define a few styles for the nodes
\tikzset{
  tree_node/.style = {align=center, inner sep=0pt, text centered, font=\sffamily},
  circle_node/.style = {tree_node, circle, black, draw=black, text width=1.5em, very thick},
  double_circle_node/.style = {circle_node, double}
}

\begin{document}

\input{template}
\mysearchtree

\clearpage

\end{document}

现在,我想在某些节点下(例如节点 D 下)添加横线。原因是这棵树应该表示 BFS 算法的搜索树,当我们访问节点 D 时,搜索树中相应的节点(即 3)无法扩展,因为它已经扩展了。我需要用节点 D 下的横线来表示这一点,而不是数字 3 下的横线。我该怎么做?

答案1

只需记住图片并通过覆盖添加栏即可。

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{arrows}
\usepackage[margin=1.5cm]{geometry}
\usepackage{filecontents}
\begin{filecontents*}{template.tex}
\def\mysearchtree{
\begin{tikzpicture}[-, >=stealth', level/.style={sibling distance = 4cm/##1,
level distance = 2cm},remember picture] 

\node [circle_node] (A) {$1$}
child { node [circle_node] (B) {$2$}    
    child { node [circle_node] (D) {$3$} }
    child { node [double_circle_node] (E) {$5$} }
}
child { node [circle_node] (C) {$3$}    
    child { node [circle_node] (F) {$1$} }
    child { node [circle_node] (G) {$4$} }
};
% Add labels to nodes
\node [blue, above] at (A.north) {$[1]$};
\node [blue, above left] at (B.north) {$[2]$};
\node [blue, above right] at (C.north) {$[3]$};
\node [blue, above left] at (D.north) {$[4]$};
\node [blue, above right] at (E.north) {$[5]$};

\end{tikzpicture}
}
\end{filecontents*}

% Define a few styles for the nodes
\tikzset{
  tree_node/.style = {align=center, inner sep=0pt, text centered, font=\sffamily},
  circle_node/.style = {tree_node, circle, black, draw=black, text width=1.5em, very thick},
  double_circle_node/.style = {circle_node, double}
}

\begin{document}

\input{template}
\mysearchtree

\tikz[overlay,remember picture]{\draw[red] ([yshift=-2mm]D.south west) -- ([yshift=-2mm]D.south east);}

\clearpage

\end{document}

在此处输入图片描述

相关内容