public void Update(BracketBalanceQueryObject updatedLeftChild, BracketBalanceQueryObject updatedRightChild) { RightBracketCount = updatedLeftChild.RightBracketCount; LeftBracketCount = updatedRightChild.LeftBracketCount; if (updatedLeftChild.LeftBracketCount > updatedRightChild.RightBracketCount) { LeftBracketCount += updatedLeftChild.LeftBracketCount - updatedRightChild.RightBracketCount; } else if (updatedLeftChild.LeftBracketCount < updatedRightChild.RightBracketCount) { RightBracketCount += updatedRightChild.RightBracketCount - updatedLeftChild.LeftBracketCount; } }
private void Build(int treeArrayIndex, int segmentStartIndex, int segmentEndIndex) { if (segmentStartIndex == segmentEndIndex) { _treeArray[treeArrayIndex] = new BracketBalanceQueryObject(segmentStartIndex, _sourceBrackets[segmentStartIndex]); return; } int leftChildTreeArrayIndex = 2 * treeArrayIndex + 1; int rightChildTreeArrayIndex = leftChildTreeArrayIndex + 1; int leftChildSegmentEndIndex = (segmentStartIndex + segmentEndIndex) / 2; Build(leftChildTreeArrayIndex, segmentStartIndex, leftChildSegmentEndIndex); Build(rightChildTreeArrayIndex, leftChildSegmentEndIndex + 1, segmentEndIndex); _treeArray[treeArrayIndex] = _treeArray[leftChildTreeArrayIndex].Combine(_treeArray[rightChildTreeArrayIndex]); }
public BracketBalanceQueryObject Combine(BracketBalanceQueryObject rightAdjacentObject) { var queryObject = new BracketBalanceQueryObject(SegmentStartIndex, rightAdjacentObject.SegmentEndIndex) { RightBracketCount = RightBracketCount, LeftBracketCount = rightAdjacentObject.LeftBracketCount }; if (LeftBracketCount > rightAdjacentObject.RightBracketCount) { queryObject.LeftBracketCount += LeftBracketCount - rightAdjacentObject.RightBracketCount; } else if (LeftBracketCount < rightAdjacentObject.RightBracketCount) { queryObject.RightBracketCount += rightAdjacentObject.RightBracketCount - LeftBracketCount; } return(queryObject); }