Ternary representation of Cantor set | The Linked List Structure for the Cantor Set ; Function to initialize the Cantor Set List ; Function to propogate the list by adding new nodes for the next levels ; Modifying the start and end values for the next level ; Changing the pointers to the next node ; Recursively call the function to generate the Cantor Set for the entire level ; Function to print a level of the Set ; Function to build and display the Cantor Set for each level ; Driver code
class Cantor { constructor ( ) { this . start = 0 ; this . end = 0 ; this . next = null ; } } ; var cantor = null ; function startList ( head , start_num , end_num ) { if ( head == null ) { head = new Cantor ( ) ; head . start = start_num ; head . end = end_num ; head . next = null ; } return head ; } function propagate ( head ) { var temp = head ; if ( temp != null ) { var newNode = new Cantor ( ) ; var diff = ( ( ( temp . end ) - ( temp . start ) ) / 3 ) ; newNode . end = temp . end ; temp . end = ( ( temp . start ) + diff ) ; newNode . start = ( newNode . end ) - diff ; newNode . next = temp . next ; temp . next = newNode ; propagate ( temp . next . next ) ; } return head ; } function print ( temp ) { while ( temp != null ) { document . write ( " " + temp . start . toFixed ( 6 ) + " " + temp . end . toFixed ( 6 ) + " " ) ; temp = temp . next ; } document . write ( " " ) ; } function buildCantorSet ( A , B , L ) { var head = null ; head = startList ( head , A , B ) ; for ( var i = 0 ; i < L ; i ++ ) { document . write ( " " + i + " " ) ; print ( head ) ; propagate ( head ) ; } document . write ( " " + L + " " ) ; print ( head ) ; } var A = 0 ; var B = 9 ; var L = 2 ; buildCantorSet ( A , B , L ) ;