How to connect edges to center of the nodes in a binomial tree in fletcher?

I am creating some binomial trees with the help of fletcher. This is the function I am using,

#let binomial_tree(tree, spacing: 3em, label: true) = {
  set align(center)
  block(spacing: 3em)[
    #diagram(
      spacing: spacing,
      for (i, ns) in tree.enumerate() {
        let col = tree.len() - 1 - i
        for (_, n) in ns.enumerate() {
          node((i, col), n, )
          if i < tree.len() - 1 {
            edge("tr", if label {$p$}, "->", label-angle: auto)
            edge("br", if label {$1-p$}, "->", label-side: right, label-angle: auto)
          }
          col = col + 2
        }
      }
    )
  ]
}

#binomial_tree((
  ($S_0$,),
  ($S_0 u$, $S_0 d$),
  ($S_0 u^2$, $S_0 u d$, $S_0 d^2$),
))

Which gives me the following output,

Honestly, I am happy with the results so far. But I want the output look something like the following, notice how the base and tip of the edges connect to the center of the node relative to the sides.

I have gone through the documentation of fletcher, but couldn’t really figure out how to do so.
I am quite new to typst, I would really appreciate your help. Thanks!

You can automatically name each node based on the index and the column. This will allow you to specify the anchors east and west when drawing the edges. Note that this will fail when the one of the nodes is missing, but you can check that before drawing the edges.

#import "@preview/fletcher:0.5.8": diagram, node, edge

#let node-label(i, col, anchor: none) = {
  let name = str(i) + "-" + str(col)
  if anchor != none { name = name + "." + anchor }
  label(name)
}

#let binomial_tree(tree, spacing: (5em, 3em), label: true) = {
  set align(center)
  block(spacing: 3em)[
    #diagram(
      spacing: spacing,
      for (i, ns) in tree.enumerate() {
        let col = tree.len() - 1 - i
        for (_, n) in ns.enumerate() {
          node((i, col), n, name: node-label(i, col))
          if i < tree.len() - 1 {
            edge(node-label(i, col, anchor: "east"), node-label(i + 1, col - 1, anchor: "west"), if label {$p$}, "->", label-angle: auto)
            edge(node-label(i, col, anchor: "east"), node-label(i + 1, col + 1, anchor: "west"), if label {$1-p$}, "->", label-side: right, label-angle: auto)
          }
          col = col + 2
        }
      }
    )
  ]
}

#binomial_tree((
  ($S_0$,),
  ($S_0 u$, $S_0 d$),
  ($S_0 u^2$, $S_0 u d$, $S_0 d^2$),
))

Thanks a lot! It clears up my understanding of the directionality of the edges as well.