boolean isIdenticalTree ( MathNode aTree , MathNode bTree ) { // first check if they have the same number of children if ( aTree . equals ( bTree ) && aTree . getChildren ( ) . size ( ) == bTree . getChildren ( ) . size ( ) ) { if ( aTree . isOrderSensitive ( ) ) { // all children order sensitive for ( int i = 0 ; i < aTree . getChildren ( ) . size ( ) ; i ++ ) { if ( ! isIdenticalTree ( aTree . getChildren ( ) . get ( i ) , bTree . getChildren ( ) . get ( i ) ) ) { return false ; } } } else { // order insensitive List < MathNode > bChildren = new ArrayList <> ( bTree . getChildren ( ) ) ; OUTER : for ( MathNode aChild : aTree . getChildren ( ) ) { for ( MathNode bChild : filterSameChildren ( aChild , bChildren ) ) { if ( isIdenticalTree ( aChild , bChild ) ) { // found an identical child bChildren . remove ( bChild ) ; continue OUTER ; } } // aChild is missing in bChildren return false ; } } return true ; } return false ; }
Are aTree and bTree identical subtrees? If the root node is equal all subsequent children will be compared .