Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

search node in tree javascript

var stack = [], node, ii;
stack.push(root);

while (stack.length > 0) {
    node = stack.pop();
    if (node.title == 'randomNode_1') {
        // Found it!
        return node;
    } else if (node.children && node.children.length) {
        for (ii = 0; ii < node.children.length; ii += 1) {
            stack.push(node.children[ii]);
        }
    }
}

// Didn't find it. Return null.
return null;
Comment

javascript tree search


const isPresent = (target , root)=>{
  if (root === null) return null;
  const result = [];
  const queue = [root];


  while (queue.length > 0) {
    const current = queue.shift();
    if(target===current.data){
      return true
    }
    result.push(current.data);
    if (current.left) queue.push(current.left);
    if (current.right) queue.push(current.right);
  }
  return false;

}
Comment

PREVIOUS NEXT
Code Example
Javascript :: javascript nested loop 
Javascript :: array last element 
Javascript :: react usestate hook 
Javascript :: express json middleware 
Javascript :: how to install react fullpage using npm 
Javascript :: splice remove 0 elements before index and insert new element 
Javascript :: parseint() javascript 
Javascript :: js ?. 
Javascript :: react select remove the loading indicator 
Javascript :: angular formatting numbers with -commas 
Javascript :: ajax call to load a page on scrolling 
Javascript :: what hostings can run react js 
Javascript :: turn off js console 
Javascript :: spam system discord.js 
Javascript :: how to use axios 
Javascript :: Install PHP debugbar 
Javascript :: how to start react project on atom 
Javascript :: Get title assert 
Javascript :: clear dict javascript 
Javascript :: object loop 
Javascript :: vuejs on route scrollbehavior 
Javascript :: carousel in material ui react 
Javascript :: stripe delete product 
Javascript :: changement image js sur click 
Javascript :: percentage with react 
Javascript :: listen to localstorage changes 
Javascript :: multiple elements with same id jquery 
Javascript :: react native add react native vector icons not working 
Javascript :: row auto textarea 
Javascript :: react router dom v6 navigate replace 
ADD CONTENT
Topic
Content
Source link
Name
5+2 =