implemented preorder traversal