package lattice // TreeDistance is the hop count between two keys within one branch, measured // through their lowest common ancestor node. 0 means both keys sit in the same // node. Returns -1 if either key is absent. func (t *Tree) TreeDistance(branch Branch, keyA, keyB Key) (int, error) { if keyA == keyB { return 0, nil } pathA, err := t.WalkPath(branch, keyA) if err != nil { return 0, err } pathB, err := t.WalkPath(branch, keyB) if err != nil { return 0, err } if pathA == nil || pathB == nil { return -1, nil } lcaDepth := 0 minLen := len(pathA) if len(pathB) < minLen { minLen = len(pathB) } for i := 0; i < minLen; i++ { if pathA[i] != pathB[i] { break } lcaDepth = i } return (len(pathA) - 1 - lcaDepth) + (len(pathB) - 1 - lcaDepth), nil }