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

// 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

// 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 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 :: compare two date objects 
Javascript :: jquery if element has multiple classes 
Javascript :: get class name of object javascript 
Javascript :: change your favicon in javascript 
Javascript :: for loop javascript array of objects 
Javascript :: json stringify without quotes 
Javascript :: && condition in javascript 
Javascript :: filesaver.js cdn 
Javascript :: password validation in javascript 
Javascript :: js replace last occurrence of string 
Javascript :: looping object 
Javascript :: javascript buffer to file 
Javascript :: instance of javascript 
Javascript :: emberjs cdn 
Javascript :: strip whitespace from shopify liquid output 
Javascript :: EACCES: permission denied /delete express.js 
Javascript :: javascript Implicit Boolean Conversion to Number 
Javascript :: javascript Access String Characters 
Javascript :: javascript Removing Elements 
Javascript :: javascript Working of multiple yield Statements 
Javascript :: nuxt login with google strategie 
Javascript :: pizza form validation jquery 
Javascript :: switch javascript to java 
Javascript :: maximum product of word lengths leetcode solution 
Javascript :: phaser create animation from canvas texture 
Javascript :: Node.js technical interview samples 
Javascript :: js undici fetch data with agent 
Javascript :: condition rendering using if-else 
Javascript :: how to run javascript in terminal 
Javascript :: js foreach syntax 
ADD CONTENT
Topic
Content
Source link
Name
7+6 =