Skip to content

Commit

Permalink
Update Trie.cpp
Browse files Browse the repository at this point in the history
  • Loading branch information
Ashishgup1 authored Mar 1, 2019
1 parent 15f4e1d commit 8abe53f
Showing 1 changed file with 18 additions and 18 deletions.
36 changes: 18 additions & 18 deletions Trie.cpp
Original file line number Diff line number Diff line change
@@ -1,49 +1,49 @@
typedef struct data
{
data* bit[2];
int cnt=0;
int cnt = 0;
}trie;

trie* head;

void insert(int x)
{
trie* node = head;
trie* cur = head;
for(int i=30;i>=0;i--)
{
int curbit=(x>>i)&1;
if(node->bit[curbit]==NULL)
node->bit[curbit]=new trie();
node=node->bit[curbit];
node->cnt++;
int b = (x>>i) & 1;
if(!cur->bit[b])
cur->bit[b] = new trie();
cur = cur->bit[b];
cur->cnt++;
}
}

void remove(int x)
{
trie* node = head;
trie* cur = head;
for(int i=30;i>=0;i--)
{
int curbit=(x>>i)&1;
node=node->bit[curbit];
node->cnt--;
int b = (x>>i) & 1;
cur = cur->bit[b];
cur->cnt--;
}
}

int maxxor(int x)
{
trie* node = head;
int ans=0;
trie* cur = head;
int ans = 0;
for(int i=30;i>=0;i--)
{
int curbit=(x>>i)&1;
if(node->bit[curbit^1]!=NULL && node->bit[curbit^1]->cnt>0)
int b = (x>>i)&1;
if(cur->bit[!b] && cur->bit[!b]->cnt>0)
{
ans+=(1LL<<i);
node=node->bit[curbit^1];
ans += (1LL<<i);
cur = cur->bit[!b];
}
else
node=node->bit[curbit];
cur = cur->bit[b];
}
return ans;
}
Expand Down

0 comments on commit 8abe53f

Please sign in to comment.