After declaring them, we need to define the functions in file BinaryTreeNode.cpp .
The essence of all three functions are recursion and they are all same. So I’ll just pick up one and explains thoroughly.
Take post-order for example, we first need to find the bottom left of the tree. If the function writes that: “Go to it’s left child.”, we just get one layer deeper but still not the last layer, so we need to go another layer deeper again.
Since the function has only one parameter that indicate the current node, we can call the function itself inside it and change the parameter to the child of this node. That’s the way we keep looking for the left child until the current node has nothing.
If the current node has nothing, that means we’ve got to the bottom of the tree.
After finding the left bottom of the tree, we return to it’s parent node and find it’s brother—the right child. And it’s the same way as finding the left child.
In this program I use preorder to input the tree, it’s creation is just like preorder traversal. First Point is parent, then input it’s left child and right child.
intmain(){ cout << "Input the binary tree(# stands for null node AB#C##D##): "; BinaryTreeNode::BinaryTree tr; BinaryTreeNode::CreateBinaryTree(&tr); cout << "The preorder of the tree is: "; BinaryTreeNode::PreOrderTraverse(tr); cout << endl; cout << "The inorder of the tree is: "; BinaryTreeNode::inOrderTraverse(tr); cout << endl; cout << "The postorder of the tree is: "; BinaryTreeNode::PostOrderTraverse(tr); cout << endl; return0; }