代码

代码

我不知道如何将节点放在伯努利试验树第一级的分支上。当我尝试将其画出来时,分支中心的节点标签将成为我的子节点的起点。有人可以提供一些建议吗?

谢谢!

\documentclass{article}
\usepackage[margin = 1in]{geometry}
\usepackage{amsfonts}
\usepackage{amsthm, amsmath, amssymb}
\usepackage{tikz}
\usepackage{pgfplots}

\usetikzlibrary{arrows,decorations.pathmorphing,backgrounds,positioning,fit,petri}
\usepgfplotslibrary{statistics}

\begin{document}
\begin{tikzpicture}
 [font=\footnotesize,
       grow=right, level 1/.style={sibling distance=6em},
level 2/.style={sibling distance=1em}, level distance=5cm] 
\node (Root) {} % root
 child { node {s} edge from parent node [below = .25cm] {$\frac{1}{2}$}
    child { node {s} edge from parent node [above = .25cm] {$\frac{1}{2}$}} 
    child { node {f} edge from parent node [below = .25cm] {$\frac{1}{2}$}}
 }
 child { node {f}
    child { node {s} edge from parent node [above = .25cm] {$\frac{1}{2}$}} 
    child { node {f} edge from parent node [below = .25cm] {$\frac{1}{2}$}}
};
\end{tikzpicture}
\end{document}

在此处输入图片描述

答案1

P在 TikZ 树中,要修改从父节点(例如)到其子节点(例如)的分支(例如C1),您必须将edge from parent键放在C1及其所有子节点和孙节点之后C1

代码

\documentclass{article}
\usepackage[margin = 1in]{geometry}
\usepackage{amsfonts}
\usepackage{amsthm, amsmath, amssymb}
\usepackage{tikz}
\usepackage{pgfplots}

\usetikzlibrary{arrows,decorations.pathmorphing,backgrounds,positioning,fit,petri}
\usepgfplotslibrary{statistics}

\begin{document}
\begin{tikzpicture}
 [font=\footnotesize,
       grow=right, level 1/.style={sibling distance=6em},
level 2/.style={sibling distance=1em}, level distance=5cm] 
\node (Root) {} % root
 child { node {s} 
    child { node {s} edge from parent node [above = .25cm] {$\frac{1}{2}$}} 
    child { node {f} edge from parent node [below = .25cm] {$\frac{1}{2}$}}
    edge from parent node [below = .25cm] {$\frac{1}{2}$} % NEW
 }
 child { node {f}
    child { node {s} edge from parent node [above = .25cm] {$\frac{1}{2}$}} 
    child { node {f} edge from parent node [below = .25cm] {$\frac{1}{2}$}}
    edge from parent node [above = .25cm] {$\frac{1}{2}$} % NEW
};
\end{tikzpicture}
\end{document}

输出

在此处输入图片描述

相关内容