Observation 7.7.1.
The name of the order is based on where the root is visited; in preorder, we visit it first. For inorder, we visit it in the middle, and for postorder, we visit it last.

preorder on the left child, in this case Chapter1. We again recursively call preorder on the left child to get to Section 1.1. Since Section 1.1 has no children, we do not make any additional recursive calls. When we are finished with Section 1.1, we move up the tree to Chapter 1. At this point we still need to visit the right subtree of Chapter 1, which is Section 1.2. As before we visit the left subtree, which brings us to Section 1.2.1, then we visit the node for Section 1.2.2. With Section 1.2 finished, we return to Chapter 1. Then we return to the Book node and follow the same procedure for Chapter 2.
BinaryTree class, or should it be a method of the tree class itself? Listing 7.7.3 shows a version of the preorder traversal written as a stand-alone function that takes a binary tree as a parameter. The function is particularly elegant because our base case is simply to check if the tree exists. If the tree parameter is null, then the function returns without taking any action.
fun preorder(tree: BinaryTree<String>?) {
if (tree != null) {
println(tree.key)
preorder(tree.leftChild)
preorder(tree.rightChild)
}
}
preorder as a method of the BinaryTree class. The code for implementing preorder as an internal method is shown in Listing 7.7.4. Notice what happens when we move the code from external to internal. In general, we just change the syntax of the recursive call. A key difference is that we need to modify the base case. The internal method must check for the existence of the left and the right children before making the recursive call to preorder. Here, we do that conveniently with the Kotlin null-safety operator ?. For example, in line 3, if leftChild is null, then Kotlin does not evaluate the call to preorder.
fun preorder() {
println(this.key)
leftChild?.preorder()
rightChild?.preorder()
}
preorder is best? The answer is that implementing preorder as an external function is probably better in this case. The reason is that you very rarely want to just traverse the tree and print its values. In most cases, you will want to accomplish something else while using one of the basic traversal patterns. In fact, we will see in the next example that the postorder traversal pattern follows very closely with the code we wrote earlier to evaluate a parse tree. Therefore, we will write the rest of the traversals as external function.
postorder traversal, shown in Listing 7.7.5, is nearly identical to preorder except that we move the call to print to the end of the function.
fun postorder(tree: BinaryTree<String>?) {
if (tree != null) {
postorder(tree.leftChild)
postorder(tree.rightChild)
println(tree.key)
}
}
fun postorderEvaluate(tree: BinaryTree<String>?): Double? {
var leftValue: Double? = null
var rightValue: Double? = null
val result: Double
if (tree != null) {
leftValue = postorderEvaluate(tree.leftChild)
rightValue = postorderEvaluate(tree.rightChild)
if (leftValue != null && rightValue != null) {
val operator: String = tree.key
if (operator == "+") {
result = leftValue + rightValue
} else if (operator == "-") {
result = leftValue - rightValue
} else if (operator == "*") {
result = leftValue * rightValue
} else {
result = leftValue / rightValue
}
return result
}
return tree.key.toDouble()
} else {
return null
}
}
System.out.println call with respect to the two recursive function calls.
fun inorder(tree: BinaryTree<String>?) {
if (tree != null) {
inorder(tree.leftChild)
println(tree.key)
inorder(tree.rightChild)
}
}
fun expressionToString(tree: BinaryTree<String>?): String {
var result = ""
if (tree != null) {
result += "(" + expressionToString(tree.leftChild)
result += tree.key
result += expressionToString(tree.rightChild) + ")"
}
return result
}
expressionToString method as we have implemented it puts parentheses around each number. While not incorrect, the parentheses are clearly not needed. In the exercises at the end of this chapter you are asked to modify the expressionToString function to remove this set of parentheses.