Workspace Implement Trie Ii
0d
Back to questionsDSA Visualizers: 388 Coding Questions

Implement Trie II visualizer

Learn Implement Trie II with a step-by-step Tries visualizer, JavaScript source code and a worked example. Practice prefix and xor queries.

INTERACTIVE EXECUTION STUDIO
LEARN BY DOING

Predict. Execute. Understand.

Follow the source code, inspect live state changes, and predict the next operation before revealing it.

1 / 9Execution step
Reference example · JavaScript
Inspect reference input
[
  [
    "insert",
    "apple"
  ],
  [
    "insert",
    "apple"
  ],
  [
    "insert",
    "app"
  ],
  [
    "equal",
    "apple"
  ],
  [
    "count",
    "app"
  ],
  [
    "erase",
    "apple"
  ],
  [
    "equal",
    "apple"
  ],
  [
    "count",
    "app"
  ]
]

Algorithm source

JavaScript reference implementation · highlighted trace statement
const solve = (ops,s)=>{
let t=trie(),o=[];
for(let [op,w] of ops){
if(op==="insert")t.insert(w);
else if(op==="erase")t.erase(w);
else{
let i=t.walk(w,false),node=t.nodes[i];
o.push(op==="search"?!!node?.end:op==="prefix"?!!node?.count:op==="equal"?node?.end||0:node?.count||0)
}
s("Execute "+op+" "+w,{
Trie:t.board(),Output:o,Word:[...w]
})
}
return o
};
function trie(){
let nodes=[{
p:-1,c:"",children:new Map,end:0,count:0
}];
function walk(w,create){
let i=0;
for(let c of w){
let next=nodes[i].children.get(c);
if(next===undefined){
if(!create)return -1;
next=nodes.length;
nodes[i].children.set(c,next);
nodes.push({
p:i,c,children:new Map,end:0,count:0
})
}
i=next
}
return i
}
function insert(w){
let i=0;
for(let c of w){
let next=nodes[i].children.get(c);
if(next===undefined){
next=nodes.length;
nodes[i].children.set(c,next);
nodes.push({
p:i,c,children:new Map,end:0,count:0
})
}
i=next;
nodes[i].count++
}
nodes[i].end++
}
function erase(w){
let i=walk(w,false);
if(i<0||!nodes[i].end)return;
nodes[i].end--;
while(i>0){
nodes[i].count--;
i=nodes[i].p
}
}
function board(){
return nodes.map((x,i)=>[i,x.c||"root",x.p,x.end,x.count])
}
return {
nodes,walk,insert,erase,board
}
}
function bitTrie(){
let a=[[-1,-1]];
function insert(x){
let i=0;
for(let k=30;
k>=0;
k--){
let b=(x>>>k)&1;
if(a[i][b]<0){
a[i][b]=a.length;
a.push([-1,-1])
}
i=a[i][b]
}
}
function query(x,emit){
let i=0,v=0;
for(let k=30;
k>=0;
k--){
let b=(x>>>k)&1,choose=a[i][1-b]>=0?1-b:b;
if(choose!==b)v+=2**k;
i=a[i][choose];
if(k<6)emit(k,i,v)
}
return v
}
return {
a,insert,query
}
}

Execution canvas

Active Changed
STEP 1

Execute insert apple

Inspect the highlighted code and the state below. Click individual cells or nodes to see their values.

Trie6 rows
rootappleSelect a node to inspect its connections.
Output0 items

Empty at this step.

Word5 items
Keyboard: Space to play · Left / Right to stepExplore next problem

Worked example and algorithm walkthrough

Topic: Tries · Prefix and XOR queries

Follow the computed execution on the reference input. Advance one step at a time, inspect the changing values and compare your prediction with the final output.

Example input

[
  [
    "insert",
    "apple"
  ],
  [
    "insert",
    "apple"
  ],
  [
    "insert",
    "app"
  ],
  [
    "equal",
    "apple"
  ],
  [
    "count",
    "app"
  ],
  [
    "erase",
    "apple"
  ],
  [
    "equal",
    "apple"
  ],
  [
    "count",
    "app"
  ]
]

Execution steps

  1. Execute insert apple
  2. Execute insert app
  3. Execute equal apple
  4. Execute count app
  5. Execute erase apple

Computed output

[
  2,
  3,
  1,
  2
]
JavaScript reference implementation
const solve = (ops,s)=>{
  let t=trie(),o=[];
  for(let [op,w] of ops){
    if(op==="insert")t.insert(w);
    else if(op==="erase")t.erase(w);
    else{
      let i=t.walk(w,false),node=t.nodes[i];
      o.push(op==="search"?!!node?.end:op==="prefix"?!!node?.count:op==="equal"?node?.end||0:node?.count||0)
    }
    s("Execute "+op+" "+w,{
      Trie:t.board(),Output:o,Word:[...w]
    })
  }
  return o
};
function trie(){
  let nodes=[{
    p:-1,c:"",children:new Map,end:0,count:0
  }];
  function walk(w,create){
    let i=0;
    for(let c of w){
      let next=nodes[i].children.get(c);
      if(next===undefined){
        if(!create)return -1;
        next=nodes.length;
        nodes[i].children.set(c,next);
        nodes.push({
          p:i,c,children:new Map,end:0,count:0
        })
      }
      i=next
    }
    return i
  }
  function insert(w){
    let i=0;
    for(let c of w){
      let next=nodes[i].children.get(c);
      if(next===undefined){
        next=nodes.length;
        nodes[i].children.set(c,next);
        nodes.push({
          p:i,c,children:new Map,end:0,count:0
        })
      }
      i=next;
      nodes[i].count++
    }
    nodes[i].end++
  }
  function erase(w){
    let i=walk(w,false);
    if(i<0||!nodes[i].end)return;
    nodes[i].end--;
    while(i>0){
      nodes[i].count--;
      i=nodes[i].p
    }
  }
  function board(){
    return nodes.map((x,i)=>[i,x.c||"root",x.p,x.end,x.count])
  }
  return {
    nodes,walk,insert,erase,board
  }
}
function bitTrie(){
  let a=[[-1,-1]];
  function insert(x){
    let i=0;
    for(let k=30;
    k>=0;
    k--){
      let b=(x>>>k)&1;
      if(a[i][b]<0){
        a[i][b]=a.length;
        a.push([-1,-1])
      }
      i=a[i][b]
    }
  }
  function query(x,emit){
    let i=0,v=0;
    for(let k=30;
    k>=0;
    k--){
      let b=(x>>>k)&1,choose=a[i][1-b]>=0?1-b:b;
      if(choose!==b)v+=2**k;
      i=a[i][choose];
      if(k<6)emit(k,i,v)
    }
    return v
  }
  return {
    a,insert,query
  }
}

Continue learning

Practice related DSA questions in the same topic.