Last active
August 29, 2015 14:09
-
-
Save kartikkukreja/6078cb742ab9cbb4f936 to your computer and use it in GitHub Desktop.
BRCKTS segment tree node
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
struct SegmentTreeNode { | |
int unmatchedOpenParans, unmatchedClosedParans; | |
void assignLeaf(char paranthesis) { | |
if (paranthesis == '(') | |
unmatchedOpenParans = 1, unmatchedClosedParans = 0; | |
else | |
unmatchedOpenParans = 0, unmatchedClosedParans = 1; | |
} | |
void merge(SegmentTreeNode& left, SegmentTreeNode& right) { | |
int newMatches = min(left.unmatchedOpenParans, right.unmatchedClosedParans); | |
unmatchedOpenParans = right.unmatchedOpenParans + left.unmatchedOpenParans - newMatches; | |
unmatchedClosedParans = left.unmatchedClosedParans + right.unmatchedClosedParans - newMatches; | |
} | |
bool getValue() { | |
return unmatchedOpenParans == 0 && unmatchedClosedParans == 0; | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment