[Tex/LaTex] How to put labels on a tree diagram in tikz

tikz-pgftikz-treestrees

I am requesting help with a tree diagram in latex.
I am preparing a handout on a mathematics course. In the handout I have a tree diagram which I modified from
here: How can I reproduce this simple tree diagram?

While I am familiar with flow charts in tikz, I am not familiar with tree diagrams. What I am not able to put on the tree diagram are:

  1. partial derivative as labels between variables, e.g.

    $\frac{\partial z}{\partial x}$ 
    

    between z and x, and similar partial derivatives between z and y, x and s, x and t, y and s and y and t.

  2. the length of each branch

I found some material on labeling on a tree diagram here: How to label nodes on a tree diagram using tikz.
But I am not able to modify the code to my needs.
Besides separation between variables is not good, e.g. s and t are over written at the last level
Assistance will be appreciated. I hope the document is readable, because I have problems inserting text which is mathematical equations.
Below is my MWE.

\documentclass[12pt]{report}
\usepackage{tikz}
\begin{document}

$\textbf{The Chain Rule (Case 2)}$\\
Suppose that `$z=f(x,y)$` is a differentiable function of x and y,\\
where $x=g(s,t)$ and $y=h(s,t)$ are both differentiable functions of s and t.\\
Then
$\frac{\partial z}{\partial s}=\frac{\partial z}{\partial x}\frac{\partial x}{s}+\frac{\partial z}{\partial y}\frac{\partial y}{\partial s}$\qquad $\frac{\partial z}{\partial t}=\frac{\partial z}{\partial x}\frac{\partial x}{\partial t}+\frac{\partial z}{\partial y}\frac{\partial y}{\partial t}$\\

\begin{minipage}{.8\linewidth}
The Chain Rule (Case 2) contains three types of variables: x and y and are independent
variables, s and t are called intermediate variables, z is the dependent variable.\\ Notice that this has one term for each intermediate variable and each of these terms resembles the one-dimensional Chain Rule above.\\
To remember the Chain Rule, refer to the tree diagram (right). Branches from the dependent variable z to the intermediate variables x and y indicate that z is a function of x and y. Then there are branches from x and y to the independent variables s and t. On each branch is the corresponding partial derivative.\\
To find $\frac{\partial z}{\partial s}$ find the product of the partial derivatives along each path from z to s and then add these products,\\

To find $\frac{\partial z}{\partial s}$ find the product of the partial derivatives along each path from z to s and then add these products,\\

i.e $\frac{\partial z}{\partial s}=\frac{\partial z}{\partial x}\frac{\partial x}{s}+\frac{\partial z}{\partial y}\frac{\partial y}{\partial s}$.\\
Similarly, find $\frac{\partial z}{\partial t}$ by using the paths from z to t,\\

i.e $\frac{\partial z}{\partial t}=\frac{\partial z}{\partial x}\frac{\partial x}{\partial t}+\frac{\partial z}{\partial y}\frac{\partial y}{\partial t}$\\
\end{minipage}
\begin{minipage}{.2\linewidth}
\begin{tikzpicture}[tn/.style={circle,inner sep=1.5pt,draw,fill}% tree node
]

\node (10) {z}
    child { node (11) {x}
      child { node (111) {s}}
      child { node (112) {t}}}
    child { node (12) {y}
      child { node (121) {s}}
      child { node (122) {t}}}
;
%    \node[left =of 121] {};
%    \node      at (10   -| 11) {$\frac{\partial z}{\partial x}$};
%    \node      at (10   -| 12) {$\frac{\partial z}{\partial y}$};
%    \node      at (11   -| 111) {$\frac{\partial x}{\partial s}$};
%    \node      at (11   -| 112) {$\frac{\partial x}{\partial t}$};
%    \node      at (12   -| 121) {$\frac{\partial y}{\partial s}$};
%    \node      at (12   -| 122) {$\frac{\partial y}{\partial t}$};
\end{tikzpicture}
\end{minipage}%
\end{document}

Best Answer

Forest can automatically lay out the tree, avoiding most clashes. (Although labels on branches can potentially conflict, most conflicts between node content and edges etc. within the tree are automatically avoided.) It can also use the content of the tree's nodes to create the labels.

For example,

\documentclass[border=12pt,12pt]{standalone}
\usepackage{forest}
\begin{document}
\begin{forest}
  for tree={
    math content,
    calign=fixed edge angles,
  },
  before typesetting nodes={
    for descendants={
      delay={content/.wrap value={\strut #1}},
      inner sep=1.5pt,
      where={
        >OOw+n< {n}{!u.n children}{(#1+1)/2}
      }{
        edge label/.process={
          OOw2 {content}{!u.content} {node [midway, left, anchor=base east] {$\frac{\partial #2}{\partial #1}$}}
        }
      }{
        edge label/.process={
          OOw2 {content}{!u.content} {node [midway, right, anchor=base west] {$\frac{\partial #2}{\partial #1}$}}
        }
      },
    },
  },
  [z
    [x
      [s]
      [t]
    ]
    [y
      [s]
      [t]
    ]
  ]
\end{forest}
\end{document}

Forest solution