implemented preOrder traversal, along with some refactoring.