Skip to content

Commit 55cd88b

Browse files
Bin trie nits (#86)
* type nits * another nit * added docs * change type here too --------- Co-authored-by: Luke Videckis <lukevideckis@gmail.com> Co-authored-by: Cameron Custer <73217097+cameroncuster@users.noreply.github.com>
1 parent 06b7980 commit 55cd88b

File tree

3 files changed

+20
-16
lines changed

3 files changed

+20
-16
lines changed

examples/data_structures/binary_trie.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ fn main() {
1212
for _ in 0..q {
1313
input! {
1414
t: u8,
15-
x: usize,
15+
x: u32,
1616
}
1717

1818
match t {

src/data_structures/binary_trie.rs

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
//! # Binary Trie which can be used as a multiset of integers
22
3+
/// u32 or u64
4+
pub type T = u32;
5+
36
#[derive(Default)]
47
struct Node {
58
next: [Option<usize>; 2],
6-
sub_sz: isize,
9+
sub_sz: i32,
710
}
811

912
/// # Example
@@ -44,10 +47,10 @@ impl BinaryTrie {
4447
/// # Complexity
4548
/// - Time: O(log(max_num))
4649
/// - Space: O(log(max_num))
47-
pub fn update(&mut self, num: usize, delta: isize) {
50+
pub fn update(&mut self, num: T, delta: i32) {
4851
let mut v = 0;
49-
for i in (0..usize::BITS).rev() {
50-
let bit = (num >> i) & 1;
52+
for i in (0..T::BITS).rev() {
53+
let bit = ((num >> i) & 1) as usize;
5154
if self.t[v].next[bit].is_none() {
5255
self.t[v].next[bit] = Some(self.t.len());
5356
self.t.push(Node::default());
@@ -63,14 +66,15 @@ impl BinaryTrie {
6366
/// # Complexity
6467
/// - Time: O(log(max_num))
6568
/// - Space: O(1)
66-
pub fn count(&self, num: usize) -> isize {
69+
pub fn count(&self, num: T) -> i32 {
6770
let mut v = 0;
68-
for i in (0..usize::BITS).rev() {
69-
let bit = (num >> i) & 1;
70-
if self.t[v].next[bit].is_none() {
71+
for i in (0..T::BITS).rev() {
72+
let bit = ((num >> i) & 1) as usize;
73+
if let Some(u) = self.t[v].next[bit] {
74+
v = u;
75+
} else {
7176
return 0;
7277
}
73-
v = self.t[v].next[bit].unwrap();
7478
}
7579
self.t[v].sub_sz
7680
}
@@ -80,12 +84,12 @@ impl BinaryTrie {
8084
/// # Complexity
8185
/// - Time: O(log(max_num))
8286
/// - Space: O(1)
83-
pub fn min_xor(&self, num: usize) -> usize {
84-
assert!(self.t.len() > 1);
87+
pub fn min_xor(&self, num: T) -> T {
88+
assert!(self.t[0].sub_sz > 0);
8589
let mut v = 0;
8690
let mut ans = 0;
87-
for i in (0..usize::BITS).rev() {
88-
let bit = (num >> i) & 1;
91+
for i in (0..T::BITS).rev() {
92+
let bit = ((num >> i) & 1) as usize;
8993
if self.t[v].next[bit].is_some() && self.t[self.t[v].next[bit].unwrap()].sub_sz > 0 {
9094
v = self.t[v].next[bit].unwrap();
9195
} else {

src/data_structures/trie.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ const FIRST_CHAR: usize = 'A' as usize;
66
#[derive(Default)]
77
struct Node {
88
next: [Option<usize>; ALPHABET_SIZE],
9-
cnt_words: usize,
9+
cnt_words: i32,
1010
}
1111

1212
/// # Example
@@ -60,7 +60,7 @@ impl Trie {
6060
/// # Complexity
6161
/// - Time: O(|s|)
6262
/// - Space: O(1)
63-
pub fn find(&self, s: &[char]) -> usize {
63+
pub fn find(&self, s: &[char]) -> i32 {
6464
let mut v = 0;
6565
for &ch in s {
6666
let idx = ch as usize - FIRST_CHAR;

0 commit comments

Comments
 (0)