Version 2 (strategy of overwriting the node labels)
I've added probabilities above/below the arrows; p0
can be changed via the \malp
command. Next to it, I've written horizontal level plus number of d
s (vertical level) information into the nodes. We can turn it off via the \nodeformat
command. If we want to add our own node text, it's located in the last block of code and we overwrite the present node labels, e.g.
3/2/G/yellow
3
- is the third horizontal level,
2
- is the number of d
s (vertical level, starting from 0),
G
- is the text we wish to have there,
yellow
- is the fill color of that particular node.
I'm adding a new version of the source code and a preview of a simple example. Let me hope this will help a bit, because it wasn't easy to understand the requested format of a graph.
% *latex mal-tree.tex
\documentclass[a4paper]{article}
\pagestyle{empty} % no page numbers, please
\usepackage{tikz} % graphics engine
\usetikzlibrary{arrows.meta} %
\begin{document}
\def\mallevel{5} % a number of levels we wish to get
\def\malp{0.25} % probability p
\def\nodeformat{\x-\numberofd} % text written in the node, {} or {\numberofs-\numberofd}
\def\ratiox{2.5} % longer arrows (horizontal direction)
\def\ratioy{1.5} % longer arrows (vertical direction)
% Defining styles...
\tikzset{
inner sep=0pt, outer sep=2pt, % some node reserve
malarrow/.style={->, shorten >=0pt, shorten <=-2pt, -{Stealth[length=5pt, width=3pt, inset=1pt]},
}, % a style for arrows, -2 is a shift back (an experiment)
malnode/.style={draw=black, fill=none, minimum width=5mm, circle, inner sep=1pt}, % a style for nodes
prob/.style={pos=0.3, sloped, font=\footnotesize},
}
% the picture itself
\begin{tikzpicture}
\foreach \x in {0,...,\mallevel} { % horizontal direction of a tree
\foreach \y in {0,...,\x} { % vertical direction of a tree
\pgfmathparse{-\x/2+\y} % move up by a half of a tree (vertical direction)
\let\movey=\pgfmathresult % save the result
% position of nodes
%\ifnum\y=0 \def\whereto{below} \else \def\whereto{above} \fi
%\ifnum\x=0 \ifnum\y=0 \def\whereto{left} \fi \fi
% d, number of ds and its output format
\pgfmathparse{int(\x-\y)}
\let\numberofd=\pgfmathresult
%\ifnum\numberofd=0 \def\mald{} \fi % print nothing if there is no d
%\ifnum\numberofd=1 \def\mald{d} \fi % print d without superscript
%\ifnum\numberofd>1 \def\mald{d^{\numberofd}} \fi % regular d with superscript
% u, number of us and its output format
\pgfmathparse{int(\y)}
\let\numberofs=\pgfmathresult
%\ifnum\y=0 \def\malu{} \fi % print nothing if there is no u
%\ifnum\y=1 \def\malu{u} \fi % print u without superscript
%\ifnum\y>1 \def\malu{u^{\y}} \fi % regular u with superscript
\node[malnode, font=\tiny] %label=\whereto:$S_0\mald\malu$
(\x-\y) at (\ratiox*\x, \ratioy*\movey) {\nodeformat}; % draw a node + its label
\ifnum\x>0 % draw the arrows
\pgfmathparse{int(\x-1)}
\let\previousx=\pgfmathresult % previous level (horizontal direction)
\pgfmathparse{int(\y-1)}
\let\previousy=\pgfmathresult % previous level (vertical direction)
\pgfmathparse{\malp^\y * (1-\malp)^\numberofd}
\let\maltype=\pgfmathresult
\ifnum\y<\x
\draw[malarrow] (\previousx-\y)--(\x-\y) node [prob, below] {\maltype}; \fi % arrows from the left top node to the right bottom node, if previous node is defined
\ifnum\y>0
\draw[malarrow] (\previousx-\previousy)--(\x-\y) node [prob, above] {\maltype}; \fi % arrows from the left bottom node to the right top node, if the previous node is defined
\fi % end of \ifnum\x>0, otherwise we cannot draw an arrow
} % \y, vertical direction upto \x level
} % \x, horizontal direction upto \mallevel.
% Overwriting mode...
\foreach \bonusx/\bonusy/\maltext/\malcolor in {1/0/A/yellow, 3/2/G/yellow, 4/2/T/yellow, 5/2/E/orange, 5/3/F/orange} {
\pgfmathparse{-\bonusx/2+(\bonusx-\bonusy)} % move up by a half of a tree (vertical direction)
\let\movey=\pgfmathresult % save the result
\node[malnode, font=\footnotesize, fill=\malcolor]
(\bonusx-\bonusy) at (\ratiox*\bonusx, \ratioy*\movey) {\maltext};
}
\end{tikzpicture}
\end{document}

Version 1 (old version)
This might be a starting point for you done in TikZ. I used notation from this question. I've added some comments to the source code. We can run any LaTeX engine.
% *latex mal-a-tree.tex
\documentclass[a4paper]{article}
\pagestyle{empty} % no page numbers, please
\usepackage{tikz} % graphics engine
\usetikzlibrary{arrows.meta} %
\begin{document}
\def\mallevel{7} % a number of levels we wish to get
\def\ratiox{1.7} % longer arrows (horizontal direction)
\def\ratioy{0.9} % longer arrows (vertical direction)
% Defining styles...
\tikzset{
inner sep=0pt, outer sep=2pt, % some node reserve
malarrow/.style={->, shorten >=0pt, shorten <=-2pt, -{Stealth[length=5pt, width=3pt, inset=1pt]}}, % a style for arrows, -2 is a shift back (an experiment)
malnode/.style={draw=none, fill=black, minimum width=5pt, circle}, % a style for nodes
}
% the picture itself
\begin{tikzpicture}
\foreach \x in {0,...,\mallevel} { % horizontal direction of a tree
\foreach \y in {0,...,\x} { % vertical direction of a tree
\pgfmathparse{-\x/2+\y} % move up by a half of a tree (vertical direction)
\let\movey=\pgfmathresult % save the result
% position of nodes
\ifnum\y=0 \def\whereto{below} \else \def\whereto{above} \fi
\ifnum\x=0 \ifnum\y=0 \def\whereto{left} \fi \fi
% d, number of ds and its output format
\pgfmathparse{int(\x-\y)}
\let\numberofd=\pgfmathresult
\ifnum\numberofd=0 \def\mald{} \fi % print nothing if there is no d
\ifnum\numberofd=1 \def\mald{d} \fi % print d without superscript
\ifnum\numberofd>1 \def\mald{d^{\numberofd}} \fi % regular d with superscript
% u, number of us and its output format
\ifnum\y=0 \def\malu{} \fi % print nothing if there is no u
\ifnum\y=1 \def\malu{u} \fi % print u without superscript
\ifnum\y>1 \def\malu{u^{\y}} \fi % regular u with superscript
\node[malnode, label=\whereto:$S_0\mald\malu$] (\x-\y) at (\ratiox*\x, \ratioy*\movey) {}; % draw a node + its label
\ifnum\x>0 % draw the arrows
\pgfmathparse{int(\x-1)}
\let\previousx=\pgfmathresult % previous level (horizontal direction)
\pgfmathparse{int(\y-1)}
\let\previousy=\pgfmathresult % previous level (vertical direction)
\ifnum\y<\x \draw[malarrow] (\previousx-\y)--(\x-\y); \fi % arrows from the left top node to the right bottom node, if previous node is defined
\ifnum\y>0 \draw[malarrow] (\previousx-\previousy)--(\x-\y); \fi % arrows from the left bottom node to the right top node, if the previous node is defined
\fi % end of \ifnum\x>0, otherwise we cannot draw an arrow
} % \y, vertical direction upto \x level
} % \x, horizontal direction upto \mallevel.
\end{tikzpicture}
\end{document}

p
and1-p
labels. I didn't use the one from wiki to make it clear that I don't necessarily need that specific look. Very simple text nodes connected by very simple lines are sufficient.p
, horizontal level, that's clear, andS0
next to it? According to notation, after we enterS0
all cells/nodes should be known in value. Or are you really entering differentS
value per cell (it is known from game theory field)?