: Given a binary tree, your task is to determine its maximum value. The first step is to decide how to create subinstances for your friends. As said, when the input instance is a binary trees, the most natural subinstances are its left and right subtrees. Your friends must solve the same problem that you do. Hence, assume that they provide you with the maximum value within each of these trees. Luckily, the maximum value within a tree is either the maximum on the left, the maximum on the right, or the value at the root. Our only job then is to determine which of these three is the maximum. As described in the beginning of Chapter 10, the maximum of the empty list is −∞. algorithm Max(tree) pre-cond: tree is a binary tree. post-cond: Returns the maximum of data fields of nodes. begin
: Given a binary tree, your task is to determine its maximum value. The
first step is to decide how to create subinstances for your friends. As said, when the
input instance is a binary trees, the most natural subinstances are its left and right
subtrees. Your friends must solve the same problem that you do. Hence, assume that
they provide you with the maximum value within each of these trees. Luckily, the
maximum value within a tree is either the maximum on the left, the maximum on
the right, or the value at the root. Our only job then is to determine which of these
three is the maximum. As described in the beginning of Chapter 10, the maximum of
the empty list is −∞.
pre-cond: tree is a binary tree.
post-cond: Returns the maximum of data fields of nodes.
begin
Step by step
Solved in 2 steps