Skip to main content

Posts

implement a binary search tree in js -------------------------------------

https://stackblitz.com/edit/typescript-7qiw9v //implement a binary search tree in js ------------------------------------- class TreeNode { data : any left : TreeNode right : TreeNode constructor ( __data : any ) { this . data = __data ; } } class Tree { //add a node to the tree root : TreeNode = null //public---------------------------------------------------- public insert ( data : any ) { if ( this . root != null ) { this . insertWithLeaf ( data , this . root ) } else { this . root = new TreeNode ( data ); this . root . left = null ; this . root . right = null ; } } //private---------------------------------------------------- private insertWithLeaf ( data : any , leaf : TreeNode ) { //add node to left if ( data < leaf . data ) { if ( leaf . left != null )...

saddleback-search-algorithm using divide a conquer

saddleback-search-algorithm using divide a conquer //https://www.geeksforgeeks.org/saddleback-search-algorithm-in-a-2d-array/ class search { static Saddleback ( arry : Array < any >, row : number , col : number , value : any ) { let bottomRowIndex : number =( row - 1 ); let bottomLeftCol : number = 0 ; while ( bottomRowIndex >= 0 && bottomLeftCol < col ) { if ( arry [ bottomRowIndex ][ bottomLeftCol ]=== value ) { return { rowindex : bottomRowIndex , colIndex : bottomLeftCol }; } else { if ( arry [ bottomRowIndex ][ bottomLeftCol ]> value ) { bottomRowIndex -= 1 ; } else { bottomLeftCol += 1 ; } } } } } let values =[[ 10 , 20 , 30 , 40 ], ...

why to choose cocos2dx c++ when its comes to 2d mobile gaming

  why i choose cocos2dx to create 2d games 1. cross-platform yes you are right while finding gaming solution you can't ignore either android or ios so that's why my choice is cocos2dx . 2. easily build for both android and ios 3. 60 fps performance on both Android and  ios even on a little slow device 4. all required things which required for a 2d game like animation using action's inbuilt tween engine easy way to configure box2d using fixed time steps 5. automatic memory management even you can use c++ 11(smart pointer's) which help you to overcome with memory management. 7. parent-child relationship where you create complex ui using grouping sprites. 8.shaders and particle support 9. easy way to implement touch events and other events 10. callback using lambda 11. inbuilt list button and other ui which normally required 12. you can mix 2d and 3d same scene 13. full tile mapping support 14. inbuilt camera which can be attached t...

a simple example of linked list

working sample code-> https://stackblitz.com/edit/typescript-mzgnay operation done:- add item is a linked list->at last add item at starting remove item by value remove item by index insert item before insert item after reverse linked list code:- //Linked list --------------------------------------------------- //create a class which holds data and address of next class //newNode class newNode { //data which can store any type of data but right now i am not handling array and object data type only string or number data : any ; next : newNode ; } class LinkedList { //Head store first node in linked list Head : newNode = null ; //last node in the linked list Tail : newNode = null ; //length of linked list len : number = 0 ; /* push a node at last of linked list */ public push ( data : any ) //push data { //create a new node---step1 var node = new newNode ();...

draw a 4*4 matrix in typescript and exchange two column values based on row index

working link https://stackblitz.com/edit/typescript-omoaq4 code => //draw a matrix and exchange two column values in the matrix based on given row index class matrix { static rm =[]; static rc =[]; static drawMatrix ( r : number , c : number ) { //n2 var k = 0 ; for ( let i = 0 , k = 0 ; i < r ; i ++) { this . rc =[]; for ( let j = 0 ; j < c ; j ++, k ++) { this . rc . push ( String . fromCharCode ( 64 +( k + 1 ))); } this . rm . push ( this . rc ); } } static exchangeRowData ( rowIndex , col1 , col2 ) { let currentRow = this . rm [ rowIndex ]; let temp = currentRow [ this . getIndex ( currentRow , col1 )]; let index = this . getIndex ( currentRow , col2 ); currentRow [ this . getIndex ( currentRow , col1 )]= currentRow [ index ]; currentRow [ index ]= t...

how to get min and max from array and return sum of all elements except min and max in typescript

working link https://stackblitz.com/edit/typescript-ewrnkd code=> //get minimum and maximum and then class Algo { static solve ( arr : Array < number >) { let max = 0 ; let min = 0 ; max = arr [ 0 ]; min = arr [ 0 ]; for ( var i = 0 ; i < arr . length ; i ++) { if ( arr [ i + 1 ]> max ) { max = arr [ i + 1 ] } if ( arr [ i + 1 ]< min ) { min = arr [ i + 1 ] } } return { "minSum" : this . sumWithMaxMin ( arr , max ), "maxSum" : this . sumWithMaxMin ( arr , min )} } static sumWithMaxMin ( arr , n ): number { let sum = 0 ; for ( let i = 0 ; i < arr . length ; i ++) { if ( arr [ i ]!= n ) { sum += arr [ i ]; } } return sum ; } } var o = Algo . solve ([ 1 , 2 , 3 , 4 , 5 ]); console . log ( o . max...

classic merge short in typescript

working example https://stackblitz.com/edit/typescript-lchwhz Please remember this is not the best way to implement merge short in javascript but its a classic way using without any inbuilt API method. more detail about merge sort is=> https://www.youtube.com/watch?v=TzeBrDU-JaY //merge sort in typescript using classic way you can easliy reduce this method to sorter one depends on your requirement class sortAlgo { static sort ( myArray : Array < number >) { //left bound let left = 0 ; //right bound just one less than total array length let right = myArray . length - 1 ; //divide array in to small part this . divideArray ( myArray , left , right ); } static divideArray ( myArray : Array < number >, left : number , right : number ) { //recursive call util left < right if ( left < right ) { //find mid to preventing stack overflow right-1 ...