Add type checking to TreeNode. Reformatted all the code.