private void updateNode(ViterbiNode[] viterbiNodes, ViterbiNode node) {
    int backwardConnectionId = node.getLeftId();
    int wordCost = node.getWordCost();
    int leastPathCost = DEFAULT_COST;

    for (ViterbiNode leftNode : viterbiNodes) {
      // If array doesn't contain any more ViterbiNodes, continue to next index
      if (leftNode == null) {
        return;
      } else {
        // cost = [total cost from BOS to previous node] + [connection cost between previous node
        // and current node] + [word cost]
        int pathCost =
            leftNode.getPathCost()
                + costs.get(leftNode.getRightId(), backwardConnectionId)
                + wordCost;

        // Add extra cost for long nodes in "Search mode".
        if (mode == TokenizerBase.Mode.SEARCH || mode == TokenizerBase.Mode.EXTENDED) {
          pathCost += getPenaltyCost(node);
        }

        // If total cost is lower than before, set current previous node as best left node (previous
        // means left).
        if (pathCost < leastPathCost) {
          leastPathCost = pathCost;
          node.setPathCost(leastPathCost);
          node.setLeftNode(leftNode);
        }
      }
    }
  }