added function to get next valid tree node