protected N buildTree ( int [ ] msg , int cur , int parent , ArrayList < N > nodes ) { // Count the number of children: int c = 0 ; for ( int i = 1 ; i < msg . length ; i += 2 ) { if ( ( msg [ i - 1 ] == cur && msg [ i ] != parent ) || ( msg [ i ] == cur && msg [ i - 1 ] != parent ) ) { c ++ ; } } // Build children: List < N > children = Collections . emptyList ( ) ; if ( c > 0 ) { children = new ArrayList <> ( c ) ; for ( int i = 1 ; i < msg . length ; i += 2 ) { if ( msg [ i - 1 ] == cur && msg [ i ] != parent ) { children . add ( buildTree ( msg , msg [ i ] , cur , nodes ) ) ; } else if ( msg [ i ] == cur && msg [ i - 1 ] != parent ) { children . add ( buildTree ( msg , msg [ i - 1 ] , cur , nodes ) ) ; } } } N node = makeNode ( cur , children ) ; nodes . set ( cur , node ) ; return node ; }
Recursive tree build method .