Workspace Boundary Traversal
0d
Back to questionsDSA Visualizers: 388 Coding Questions

Boundary Traversal visualizer

Learn Boundary Traversal with a step-by-step Binary Trees visualizer, JavaScript source code and a worked example. Practice traversals.

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 / 6Execution step
Reference example · JavaScript
Inspect reference input
[
  1,
  2,
  3,
  4,
  5,
  null,
  6
]

Algorithm source

JavaScript reference implementation · highlighted trace statement
const solve = (a,s)=>{
let o=[a[0]],leaf=i=>exists(a,i)&&!exists(a,2*i+1)&&!exists(a,2*i+2);
for(let i=1;
exists(a,i)&&!leaf(i);
i=exists(a,2*i+1)?2*i+1:2*i+2){
o.push(a[i]);
s("Follow left boundary",{
Tree:nodes(a),Boundary:o,Current:[i]
})
}
function f(i){
if(!exists(a,i))return;
if(leaf(i)&&i){
o.push(a[i]);
s("Collect leaf",{
Tree:nodes(a),Boundary:o,Current:[i]
})
}
f(2*i+1);
f(2*i+2)
}
f(0);
let r=[];
for(let i=2;
exists(a,i)&&!leaf(i);
i=exists(a,2*i+2)?2*i+2:2*i+1)r.push(a[i]);
while(r.length){
o.push(r.pop());
s("Append reversed right boundary",{
Tree:nodes(a),Boundary:o
})
}
return o
};
function exists(a,i){
return i<a.length&&a[i]!=null
}
function nodes(a){
return a.map((v,i)=>v==null?null:[i,v,exists(a,2*i+1)?2*i+1:null,exists(a,2*i+2)?2*i+2:null]).filter(Boolean)
}
function ordered(a,s,mode){
let o=[];
function f(i){
if(!exists(a,i))return;
if(mode==="pre"){
o.push(a[i]);
s("Visit node before children",{
Tree:nodes(a),Visited:o,Current:[i]
})
}
f(2*i+1);
if(mode==="in"){
o.push(a[i]);
s("Visit node between children",{
Tree:nodes(a),Visited:o,Current:[i]
})
}
f(2*i+2);
if(mode==="post"){
o.push(a[i]);
s("Visit node after children",{
Tree:nodes(a),Visited:o,Current:[i]
})
}
}
f(0);
return o
}

Execution canvas

Active Changed
STEP 1

Follow left boundary

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

Tree6 rows
123456Select a node to inspect its connections.
Boundary2 items
Current1 items
Keyboard: Space to play · Left / Right to stepExplore next problem

Worked example and algorithm walkthrough

Topic: Binary Trees · Traversals

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

[
  1,
  2,
  3,
  4,
  5,
  null,
  6
]

Execution steps

  1. Follow left boundary
  2. Collect leaf
  3. Append reversed right boundary

Computed output

[
  1,
  2,
  4,
  5,
  6,
  3
]
JavaScript reference implementation
const solve = (a,s)=>{
  let o=[a[0]],leaf=i=>exists(a,i)&&!exists(a,2*i+1)&&!exists(a,2*i+2);
  for(let i=1;
  exists(a,i)&&!leaf(i);
  i=exists(a,2*i+1)?2*i+1:2*i+2){
    o.push(a[i]);
    s("Follow left boundary",{
      Tree:nodes(a),Boundary:o,Current:[i]
    })
  }
  function f(i){
    if(!exists(a,i))return;
    if(leaf(i)&&i){
      o.push(a[i]);
      s("Collect leaf",{
        Tree:nodes(a),Boundary:o,Current:[i]
      })
    }
    f(2*i+1);
    f(2*i+2)
  }
  f(0);
  let r=[];
  for(let i=2;
  exists(a,i)&&!leaf(i);
  i=exists(a,2*i+2)?2*i+2:2*i+1)r.push(a[i]);
  while(r.length){
    o.push(r.pop());
    s("Append reversed right boundary",{
      Tree:nodes(a),Boundary:o
    })
  }
  return o
};
function exists(a,i){
  return i<a.length&&a[i]!=null
}
function nodes(a){
  return a.map((v,i)=>v==null?null:[i,v,exists(a,2*i+1)?2*i+1:null,exists(a,2*i+2)?2*i+2:null]).filter(Boolean)
}
function ordered(a,s,mode){
  let o=[];
  function f(i){
    if(!exists(a,i))return;
    if(mode==="pre"){
      o.push(a[i]);
      s("Visit node before children",{
        Tree:nodes(a),Visited:o,Current:[i]
      })
    }
    f(2*i+1);
    if(mode==="in"){
      o.push(a[i]);
      s("Visit node between children",{
        Tree:nodes(a),Visited:o,Current:[i]
      })
    }
    f(2*i+2);
    if(mode==="post"){
      o.push(a[i]);
      s("Visit node after children",{
        Tree:nodes(a),Visited:o,Current:[i]
      })
    }
  }
  f(0);
  return o
}

Continue learning

Practice related DSA questions in the same topic.