-
-
Notifications
You must be signed in to change notification settings - Fork 51.1k
Create binary_trie.py #11918
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Create binary_trie.py #11918
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,71 @@ | ||
| class BinaryTrie: | ||
| def __init__(self, max_bit_len=31): | ||
| self.inf = 1 << 63 | ||
| self.cc = [0] | ||
| self.to = [[-1], [-1]] | ||
| self.mb = max_bit_len | ||
|
|
||
| def add(self, a): | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As there is no test file in this pull request nor any test function or class in the file Please provide return type hint for the function: Please provide type hint for the parameter: Please provide descriptive name for the parameter: |
||
| u = 0 | ||
| self.cc[u] += 1 | ||
| for i in range(self.mb - 1, -1, -1): | ||
| d = a >> i & 1 | ||
| if self.to[d][u] == -1: | ||
| self.to[d][u] = len(self.cc) | ||
| self.to[0].append(-1) | ||
| self.to[1].append(-1) | ||
| self.cc.append(0) | ||
| u = self.to[d][u] | ||
| self.cc[u] += 1 | ||
|
|
||
| def remove(self, a): | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As there is no test file in this pull request nor any test function or class in the file Please provide return type hint for the function: Please provide type hint for the parameter: Please provide descriptive name for the parameter: |
||
| if self.cc[0] == 0: | ||
| return False | ||
| uu = [0] | ||
| u = 0 | ||
| for i in range(self.mb - 1, -1, -1): | ||
| d = a >> i & 1 | ||
| u = self.to[d][u] | ||
| if u == -1 or self.cc[u] == 0: | ||
| return False | ||
| uu.append(u) | ||
| for u in uu: | ||
| self.cc[u] -= 1 | ||
| return True | ||
|
|
||
| def cnt(self, a): | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As there is no test file in this pull request nor any test function or class in the file Please provide return type hint for the function: Please provide type hint for the parameter: Please provide descriptive name for the parameter: |
||
| u = 0 | ||
| for i in range(self.mb - 1, -1, -1): | ||
| d = a >> i & 1 | ||
| u = self.to[d][u] | ||
| if u == -1 or self.cc[u] == 0: | ||
| return 0 | ||
| return self.cc[u] | ||
|
|
||
| def min_xor(self, a): | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As there is no test file in this pull request nor any test function or class in the file Please provide return type hint for the function: Please provide type hint for the parameter: Please provide descriptive name for the parameter: |
||
| if self.cc[0] == 0: | ||
| return self.inf | ||
| u, res = 0, 0 | ||
| for i in range(self.mb - 1, -1, -1): | ||
| d = a >> i & 1 | ||
| v = self.to[d][u] | ||
| if v == -1 or self.cc[v] == 0: | ||
| res |= 1 << i | ||
| u = self.to[d ^ 1][u] | ||
| else: | ||
| u = v | ||
| return res | ||
|
|
||
| def max_xor(self, a): | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As there is no test file in this pull request nor any test function or class in the file Please provide return type hint for the function: Please provide type hint for the parameter: Please provide descriptive name for the parameter: |
||
| if self.cc[0] == 0: | ||
| return -self.inf | ||
| u, res = 0, 0 | ||
| for i in range(self.mb - 1, -1, -1): | ||
| d = a >> i & 1 | ||
| v = self.to[d ^ 1][u] | ||
| if v == -1 or self.cc[v] == 0: | ||
| u = self.to[d][u] | ||
| else: | ||
| u = v | ||
| res |= 1 << i | ||
| return res | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please provide return type hint for the function:
__init__. If the function does not return a value, please provide the type hint as:def function() -> None:Please provide type hint for the parameter:
max_bit_len