static < A > int swap ( final TreeNode < A > that , final TreeNode < A > other ) { assert that != null ; assert other != null ; final Random random = RandomRegistry . getRandom ( ) ; final ISeq < TreeNode < A > > seq1 = that . breadthFirstStream ( ) . collect ( ISeq . toISeq ( ) ) ; final ISeq < TreeNode < A > > seq2 = other . breadthFirstStream ( ) . collect ( ISeq . toISeq ( ) ) ; final int changed ; if ( seq1 . length ( ) > 1 && seq2 . length ( ) > 1 ) { final TreeNode < A > n1 = seq1 . get ( random . nextInt ( seq1 . length ( ) - 1 ) + 1 ) ; final TreeNode < A > p1 = n1 . getParent ( ) . orElseThrow ( AssertionError :: new ) ; final TreeNode < A > n2 = seq2 . get ( random . nextInt ( seq2 . length ( ) - 1 ) + 1 ) ; final TreeNode < A > p2 = n2 . getParent ( ) . orElseThrow ( AssertionError :: new ) ; final int i1 = p1 . getIndex ( n1 ) ; final int i2 = p2 . getIndex ( n2 ) ; p1 . insert ( i1 , n2 . detach ( ) ) ; p2 . insert ( i2 , n1 . detach ( ) ) ; changed = 2 ; } else { changed = 0 ; } return changed ; }
The static method makes it easier to test .