Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

binary tree implementation javascript

class Node {
    constructor(value){
        this.value = value
        this.left = null
        this.right = null
    
    }

}
class BinarySearchTree {

    constructor(){
      this.root = null
    }
 
  find(value){
      if(!this.root) return false
      
      let current = this.root
      let found = false
      while(current && !found){
            if(value < current.value){
              current = current.left
             } else if(value > current.value){
                current = current.right
             } else {
                  found = current
             } 
            
            }
    
        if(!found) return undefined;
        return found
      
  
  }
   insert(value){
        var newNode = new Node(value);
        if(this.root === null){
            this.root = newNode;
            return this;
        }
        let current = this.root;
        while(current){
            if(value === current.value) return undefined;
            if(value < current.value){
                if(current.left === null){
                    current.left = newNode;
                    return this;
                }
                current = current.left;
            } else {
                if(current.right === null){
                    current.right = newNode;
                    return this;
                } 
                current = current.right;
            }
        }
    }
}
Comment

JavaScript binary tree

// invert binary tree : (right node >> left node && left node >> right node )
 const invertTree =(root)=>{
if(root===null) return root;
  let queue =[root] ; 
  while(queue.length>0){

let node =queue.shift() ; 
if(node !=null){
  let hold = node.left ;
  node.left =node.right; 
  node.right =hold ; 
  if(node.left) queue.push(node.left) ;

  if(node.right) queue.push(node.right) ;
}
  }
  
return root;

 }
Comment

JavaScript binary tree

// calculate the size of binary tree (number of nodes) 
const sizeTree = (root) => {
  if (!root) return 0;
  let queue = [root];
  let size = 0;

  while (queue.length > 0) {
    let current = queue.shift();

    if (current.left) queue.push(current.left);
    if (current.right) queue.push(current.right);
    size += 1;
  }
  return size;
};
Comment

JavaScript binary tree

// calculate the maximum height of binary tree  
const maxHeightTree = (root) => {
  if (!root) return 0;
 let left = maxHeightTree(root.left);
  console.log(left)
 let right = maxHeightTree(root.right);
 console.log(right)

 if(left> right){

  return left +1 ;

 }else { 
  return right+1 ; 
 }
};
Comment

binary tree inOrder method with JavaScript

inOrder() {
        let result = [];

        let recursive = (node) => {
            if (node.left) {
                recursive(node.left);
            }
            result.push(node.value);

            if (node.right) {
                recursive(node.right);
            }
        }

        recursive(this.root);

        return result;
    }
//if you find the answer is useful ,
//upvote for it, so can the others benefit also ༼ つ ◕_◕ ༽つ @mohammad alshraideh ( ͡~ ͜ʖ ͡°)⇑⇑
Comment

JavaScript binary tree

//print Kth path (given K integer and you need to find the path to reach this K as total sum  in binary tree)
  const printKthPath =(root ) =>{
 let k =7 ;
if(root === null) return 
 let sum = 0 ; 
 let stack =[] ;
 sum =sum +root.data ;
 stack.push(root.data)
 if(sum === k){
  return stack
 }else {

   if(root.left)printKthPath(root.left )
  if(root.right)printKthPath(root.right)
 if(!root.right && ! root.left ){
   sum = sum- root.data;
    stack.pop();
 
 }
 }
return stack
  }
Comment

binary search tree js

class Node {
  constructor(value){
      this.value = value
      this.left = null
      this.right = null
  }
}

class BinarySeachTree {
      constructor(){
        this.root = null
      }
}
Comment

JavaScript binary tree


// calculate the average value for binary tree 
const AvgValue = (root) => {
  let result = [];
  let avg = 0;
  let sum = 0;
  let queue = [root];
  while (queue.length) {
    let current = queue.shift();
    result.push(current.data);
    if (current.left) queue.push(current.left);
    if (current.right) queue.push(current.right);
  }
  for (let i = 0; i < result.length; i++) {
    sum = sum + result[i];
  }
  avg = Math.floor(sum / result.length);
  return avg;
};
Comment

JavaScript binary tree

// minimum node in binary tree
const BreadthFirstsearchMin =(root)=>{
let min = Infinity ;
let queue = [root];
while(queue.length){
let current = queue.shift();
if(current.data < min) {
  min=current.data;
}
if(current.left!== null) queue.push(current.left);
if(current.right !==null) queue.push(current.right);
}
return min ;
}
Comment

JavaScript binary tree

// find maixmum node in binary tree 
const Max = (root) => {
  let max = -Infinity;
  let stack = [root];

  while (stack.length) {
    let current = stack.pop();
    if (current.data > max) {
      max = current.data;
    }
    if (current.left !== null) stack.push(current.left);
    if (current.right !== null) stack.push(current.right);
  }

  return max;
};
Comment

JavaScript binary tree

// check if mirror binary tree or not (give two binary trees and you need to find if they are a mirror to each other )
const isMirror =(tree1 ,tree2)=>{
if(tree1 ===null && tree2 ===null) return true ;
if(tree1.data == tree2.data){
  if(isMirror(tree1.left ,tree2.right) && isMirror(tree1.right ,tree2.left)){
    return true 
  }else return false
} else{
  return false
} 
  }
Comment

JavaScript binary tree

// invert binary  tree using recursive function

function invertTree(node) {
  if (node && node.left) {
      let left = node.left;
      node.left = node.right;
      node.right = left;
      invertTree(node.right);
      invertTree(node.left);
  }
  return node;
}
Comment

JavaScript binary tree

//claculate the minimum height of binary tree 
const minHeightTree =(root)=>{
  if (!root) return 0;
  let left = minHeightTree(root.left);
  console.log(left)
  let right = minHeightTree(root.right);
  console.log(right)
 
  if(left< right){
 
   return left +1 ;
 
  }else { 
   return right+1 ; 
  }
 };
Comment

PREVIOUS NEXT
Code Example
Javascript :: javascript default parameters 
Javascript :: js get words first letter 
Javascript :: js new array from new set 
Javascript :: date difference moment js 
Javascript :: sequelize operators 
Javascript :: how to seperate words seperated by commas using javascript 
Javascript :: npm fs 
Javascript :: replace last element of array javascript 
Javascript :: flatmap javascript 
Javascript :: how to remove last index of array in javascript 
Javascript :: remove letter until vowel javascript 
Javascript :: javascript show page 
Javascript :: github actions ssh 
Javascript :: jquery get all value from class 
Javascript :: prime numbers using for loop in Js 
Javascript :: array.from foreach 
Javascript :: i want to redirect to main page from iframe javascript 
Javascript :: from string to number js 
Javascript :: new variable in loop javascript 
Javascript :: javascript if and statement 
Javascript :: js mb to bytes 
Javascript :: commander js 
Javascript :: github pages react route 
Javascript :: js get english alphabet 
Javascript :: .env.development.local 
Javascript :: jquery from object to queryselector 
Javascript :: queryselector javascript 
Javascript :: how to find out most repeated string in an array js 
Javascript :: drupal8 get params from route 
Javascript :: jquery clone row 
ADD CONTENT
Topic
Content
Source link
Name
8+8 =