File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1- class Root {
2- constructor ( val ) {
1+ class Node {
2+ constructor ( val , index ) {
33 this . number = val || null ;
44 this . left = null ;
55 this . right = null ;
6+ this . index = index || null ;
67 }
7- addValue ( val ) {
8- if ( this . number == null ) this . number = val ;
9- if ( val < this . number ) {
10- this . left = new Root ( val ) ;
11- } else if ( val > this . number ) {
12- this . right = new Root ( val ) ;
8+ }
9+
10+ class Tree {
11+ constructor ( ) {
12+ this . tree = new Node ( ) ;
13+ }
14+ addValue ( { value, index} ) {
15+ if ( index == 0 ) {
16+ this . tree . index = index ;
17+ this . tree . number = value ;
18+ }
19+ else {
20+ if ( value < this . tree . number ) this . tree . left = new Node ( value , index ) ;
21+ if ( value > this . tree . number ) this . tree . right = new Node ( value , index ) ;
1322 }
1423 }
1524}
1625
17- let tree = new Root ( ) ;
26+ let tree = new Tree ( ) ;
1827
1928for ( let i = 0 ; i < 15 ; i ++ ) {
20- tree . addValue ( Math . floor ( Math . random ( ) * 101 ) ) ;
29+ tree . addValue ( {
30+ "value" : Math . floor ( Math . random ( ) * 101 ) ,
31+ "index" : i
32+ } ) ;
2133}
2234
23- console . log ( tree ) ;
35+ console . log ( tree . tree ) ;
You can’t perform that action at this time.
0 commit comments