Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions DIRECTORY.md
Original file line number Diff line number Diff line change
Expand Up @@ -402,6 +402,7 @@
* Tests
* [Test Suffix Tree](data_structures/suffix_tree/tests/test_suffix_tree.py)
* Trie
* [Binary Trie](data_structures/trie/binary_trie.py)
* [Radix Tree](data_structures/trie/radix_tree.py)
* [Trie](data_structures/trie/trie.py)

Expand Down
71 changes: 71 additions & 0 deletions data_structures/trie/binary_trie.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
class BinaryTrie:
def __init__(self, max_bit_len=31):

Copy link
Copy Markdown

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

self.inf = 1 << 63
self.cc = [0]
self.to = [[-1], [-1]]
self.mb = max_bit_len

def add(self, a):

Copy link
Copy Markdown

Choose a reason for hiding this comment

The 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 data_structures/trie/binary_trie.py, please provide doctest for the function add

Please provide return type hint for the function: add. If the function does not return a value, please provide the type hint as: def function() -> None:

Please provide type hint for the parameter: a

Please provide descriptive name for the parameter: a

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):

Copy link
Copy Markdown

Choose a reason for hiding this comment

The 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 data_structures/trie/binary_trie.py, please provide doctest for the function remove

Please provide return type hint for the function: remove. If the function does not return a value, please provide the type hint as: def function() -> None:

Please provide type hint for the parameter: a

Please provide descriptive name for the parameter: a

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):

Copy link
Copy Markdown

Choose a reason for hiding this comment

The 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 data_structures/trie/binary_trie.py, please provide doctest for the function cnt

Please provide return type hint for the function: cnt. If the function does not return a value, please provide the type hint as: def function() -> None:

Please provide type hint for the parameter: a

Please provide descriptive name for the parameter: a

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):

Copy link
Copy Markdown

Choose a reason for hiding this comment

The 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 data_structures/trie/binary_trie.py, please provide doctest for the function min_xor

Please provide return type hint for the function: min_xor. If the function does not return a value, please provide the type hint as: def function() -> None:

Please provide type hint for the parameter: a

Please provide descriptive name for the parameter: a

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):

Copy link
Copy Markdown

Choose a reason for hiding this comment

The 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 data_structures/trie/binary_trie.py, please provide doctest for the function max_xor

Please provide return type hint for the function: max_xor. If the function does not return a value, please provide the type hint as: def function() -> None:

Please provide type hint for the parameter: a

Please provide descriptive name for the parameter: a

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
Loading