Skip to main content

Posts

Level Order Tree Traversal (Level order traversal of a tree is breadth first traversal for the tree.)

https://www.geeksforgeeks.org/level-order-tree-traversal/ level order tree Traversal or breadth first traversal for the tree with full example. please note below tree is binary search tree working link=> https://stackblitz.com/edit/typescript-gvv3qk //bfs Level Order Tree Traversal //node class which holds data and left and right child if exits class   TreeNode < T > {     private  data : T ;     private  leftChild : TreeNode < T >= null ;     private  rightChild : TreeNode < T >= null ;     constructor ( _data : T )     {       this . data = _data ;     }    getData ()     {       return   this . data ;     }    setLeft ...

write a program to print all permutations of a given vector

/******************************************************************************                               Online C++ Compiler.                Code, Compile, Run and Debug C++ program online. Write your code in this editor and press "Run" button to compile and execute it. *******************************************************************************/ #include <iostream> #include <algorithm> #include <vector> using namespace std; void displayData(vector<string> &list,int startIndex,int endIndex) {     if(startIndex==endIndex)     {           string str="";          for(int i=0;i<=endIndex;i++)          {              str+=list[i];          }       ...

Longest Common Substring using recursion, memorization and dynamic programming

working example https://stackblitz.com/edit/js-isyptv?file=index.js running code //Longest Common Substring using recursion 1 let  r1 = 0 ; let  m1 = 0 ; String . prototype . subStringR = function ( str2 , r = this . length , c = this . length , count = 0 ) {      r1 += 1 ;      if ( r === 0   ||  c === 0 )      {        return  count ;      }        if ( this [ r - 1 ]=== str2 [ c - 1 ])      {       count = this . subStringR ( str2 , r - 1 , c - 1 , count + 1 );      }            count = Math . max ( count , this . subStringR ( str2 , r - 1 , c , 0 ), this . subStringR ( str2 , r , c - 1 , 0 ));        retur...

Longest Common Subsequence | Introduction & LCS Length

Longest Common Subsequence | Introduction & LCS Length all three solution. 1. first one is recursion 2. second one is  Memorization using top down approach     3. dynamic programming using a 2d matrix filling from top using bottom up approach  complete example is here https://stackblitz.com/edit/js-3rmcpu really nice video https://www.youtube.com/watch?v=sSno9rV8Rhg //longest comman subsequence  //watch this video for more detail //https://www.youtube.com/watch?v=sSno9rV8Rhg let  r = 0 ; //check how many call happen using Recursion let  m = 0 ; //check how many call happen using Memoization top down appoarch  let  d = 0 ; //check how many call happen using dynamic programming bottom up appoarch //using Recursion St...

Using the Effect Hook complete example how effect hook works in js

working link ======== https://stackblitz.com/edit/useeffect-hook?file=index.js Similar to componentDidMount and componentDidUpdate (class component) //code  const getUser=async()=>{         let response = await fetch(           props.url);                 let user = await response.json();              drawView(user)           }   //just one call like  componentDidMount     useEffect(()=>{             getUser();                },[])  //update on every render      useEffect(()=>{  ...

randomize all elements in a array inplace

randomize all elements in an array (in-place algorithms) take no space and its fastest among all random algorithms. //fastest algo to randomize array values without any space inplace   Array . prototype . swap = function ( startIndex , endIndex = this . length - 1 ) {     let  temp = this [ startIndex ];     this [ startIndex ]= this [ endIndex ];     this [ endIndex ]= temp ;     return   this ; }; Array . prototype . random = function () {      const  randomFunction =( min , max )=>{          return   Math . round ( Math . random ()*( max - min ))+ min ;      }      for ( let  i = 0 ; i < this . length ; i ++)      {         this . swap ( i , randomFuncti...

some inplace algorithms which can be used to swap two values based on index either based on values or based on index and value

1. swap two values based on the index i think this is the fastest algorithms but if you have something else in your book please share with me. 2. second algorithms swap two values in an array its quite different than first its swap two values. 3. third one is quite highly used its swaps a value and a index. check out the running work https://stackblitz.com/edit/js-fn31dt //swap two values in array based on index in place algo Array . prototype . swap = function ( startIndex , endIndex = this . length - 1 ) {     let  temp = this [ startIndex ];     this [ startIndex ]= this [ endIndex ];     this [ endIndex ]= temp ;     return   this ; }; console . log ([ 1 , 2 , 3 , 4 , 5 ]. swap ( 2 , 3 )) //swap two values by value required swap function in place algo Array . prototype . s...

Object pooling in game using a small coding example c++

When its comes to gaming either you are using unity or cocos2dx or unreal or godot or any other engine you need a object pool specially when you dealing with mobile and low hardware device because they have memory constraint. lets take a example of endless game where there is 100 of types of object that moves from either left to right or in 3d far to close using camera so one idea is that you can create graphic at runtime and remove its when its out of the screen but every time a new graphic created a new memory allocation happen and creating two many graphics at runtime can reduce frame rate and increase memory usage, so its deal with it 1. create a object pool for example a c++ example where bullet class and boom class can used same pool class and we can create two separate pool and each pool can hold 100 bombs and 100 bullet so when bomb or bullet out of the screen we can send it back to the pool. 2. so you are holding 200 instance at the same time and when they are not in ...