tree integrity check function, with tests.