Need assistance with recursion in JS -
i'm having great deal of trouble wrapping head around recursion. simple recursion can 1 not easy me. goal here speed search algorithm. i'm guessing recursion help. takes 15 seconds on simple 43 node tree children is. below unrolled recursion fomr of code works.
var nodelist = new array(); var removelist = new array(); var count = 0; var foundinthisnodetree; var find = function ( condition ) { } while ( this.treeidmap.igtree( "nodebypath", count ).data() ) { var foundinthisnodetree = false; var n; n = this.treeidmap.igtree( "nodefromelement", this.treeidmap.igtree( "nodebypath", count ) ) if ( n.data.item.indexof( filter ) > -1 ) { foundinthisnodetree = true; } else {//look deeper var = 0; while ( this.treeidmap.igtree( "nodebypath", count + "_" + ).data() ) { n = this.treeidmap.igtree( "nodefromelement", this.treeidmap.igtree( "nodebypath", count + "_" + ) ); if ( n.data.item.indexof( filter ) > -1 ) { foundinthisnodetree = true; break; } else {//look deeper var j = 0; while ( this.treeidmap.igtree( "nodebypath", count + "_" + + "_" + j ).data() ) { n = this.treeidmap.igtree( "nodefromelement", this.treeidmap.igtree( "nodebypath", count + "_" + + "_" + j ) ); if ( n.data.item.indexof( filter ) > -1 ) { foundinthisnodetree = true; break; } else {//look deeper var k = 0; while ( this.treeidmap.igtree( "nodebypath", count + "_" + + "_" + j + "_" + k ).data() ) { n = this.treeidmap.igtree( "nodefromelement", this.treeidmap.igtree( "nodebypath", count + "_" + + "_" + j + "_" + k ) ); if ( n.data.item.indexof( filter ) > -1 ) { foundinthisnodetree = true; break; } k++; } } j++; } } i++; } } if ( !foundinthisnodetree ) this.treeidmap.igtree("removeat", ""+count ) else count++; }
*** second revision suggested mirco ellmann *****
var nodelist = new array(); var removelist = new array(); var count = 0; var foundinthisnodetree; filter = filter.tolowercase(); while ( this.treeidmap.igtree( "nodebypath", count ).data() ) { var foundinthisnodetree = false; var n; n = this.treeidmap.igtree( "nodefromelement", this.treeidmap.igtree( "nodebypath", count ) ) if ( n.data.item.tolowercase().indexof( filter ) > -1 ) { foundinthisnodetree = true; } else {//look deeper var = 0; n = this.treeidmap.igtree( "childrenbypath", count ); while ( n[i] ) { if ( n[i].data.item.indexof( filter ) > -1 ) { foundinthisnodetree = true; break; } var j = 0; n = this.treeidmap.igtree( "childrenbypath", count + "_" + ); while ( n[j] ) { if ( n[j].data.item.indexof( filter ) > -1 ) { foundinthisnodetree = true; break; } var k = 0; n = this.treeidmap.igtree( "childrenbypath", count + "_" + + "_" + j); while ( n[k] ) { if ( n[k].data.item.indexof( filter ) > -1 ) { foundinthisnodetree = true; break; } k++; } j++; } i++; } } if ( !foundinthisnodetree ) this.treeidmap.igtree("removeat", ""+count ) else count++; }
****using branchable trees data no need calls tree****
var count = 0; var foundinthisnodetree; filter = filter.tolowercase(); while ( this.treeidmap.igtree( "nodebypath", count ).data() ) { var foundinthisnodetree = false; var n; n = this.treeidmap.igtree( "nodefromelement", this.treeidmap.igtree( "nodebypath", count ) ) if ( n.data.item.tolowercase().indexof( filter ) > -1 ) { foundinthisnodetree = true; } if ( n.data.branch )//look @ childer under root node { var = 0; n = n.data.branch; while ( n[i] )//look @ childer under root node { if ( n[i].item.tolowercase().indexof( filter ) > -1 ) { foundinthisnodetree = true; break; } while ( n[i].branch )//look deeper { var j = 0; n = n[i].branch; if ( n[j].item.tolowercase().indexof( filter ) > -1 ) { foundinthisnodetree = true; break; } while ( n[j].branch )//look deeper { var k = 0; n = n[j].branch; if ( n[k].item.tolowercase().indexof( filter ) > -1 ) { foundinthisnodetree = true; break; } k++; } j++; } i++; } } if ( !foundinthisnodetree ) this.treeidmap.igtree("removeat", ""+count ) else count++; }
instead of use "nodebypath" should use "childrenbypath".
that minimize search calls on igtree.
ps: use not replace ;)
Comments
Post a Comment