CS-151 Labs > Lab 6. Binary Tree
Reminder: Javadoc comments
We have seen in previous labs how to use Java’s built-in documentation format, Javadocs, to comment your code in a way that auto-updates. This section is just a friendly reminder that you must use Javadoc comments on every program you write from here on out (with the exception of JUnit tests). Below, you will find a reminder of how Javadocs work, in case you have forgotten.
/**
* Set the array at index to element
* @param index index into array
* @param element element to insert
* @return old value at index
*/
public AnyType set(int index, AnyType element) {
AnyType oldVal;
// Make sure my index is within bound
if ( (index < 0) || (index > this.size)) {
throw new IndexOutOfBoundsException("Index Out of Bounds! You tried to get " +
index + " but the size is " + size);
}
// Save the old value
oldVal = data[index];
// Set array at index to element
data[index] = element;
// Return the old element
return(oldVal);
}
To generate the documentation, in the Project menu in Eclipse, select
Generate Javadoc
. Make sure that your project is selected, and then click
Finish
.
This will generate an html file.
Some useful tags to include in your documentation:
@author
– The author of the program@param
– Parameters that are used for a function@return
– What a function returns@throws
– Exceptions that your function may throw@see
– Links to other documentation that your function might require. You can include an HTML anchor tag (<a>...</a>
or another.java
file here.
Tree Traversals
One of the traits that comes with a tree is that it does not have a pre-determined logical ordering of the nodes, unless one is imposed from the outside.
For example, if we have the tree
A
/ \
B C
we could state that the nodes, printed in order, should be ABC, BAC, CAB, BCA, etc. All would be valid, based on the sorting criteria that we choose.
It is convenient to describe some of these sorting criteria in a reproducible way. This is similar to how we would describe the traversal of the list from Head to Tail, or from front to back. For trees, we will consider three traversals: inorder, preorder, and postorder.
For each of these traverals, there are essentially three operations:
- Visit the current node
- Traverse the left subtree
- Traverse the right subtree
Visiting the node means performing whatever operation we’re trying to do with each node. For example, if we are printing out the values stored in the nodes of the tree, then when we visit a node, we print out its value.
The order of these operations will determine whether we are doing an inorder, preorder, or postorder traversal.
For the following examples, we will use the following tree:
A
/ \
B C
/ \ / \
D E F G
Preorder
The order of the operations for a preorder traversal is:
- Visit the current node
- Traverse the left subtree
- Traverse the right subtree
The resulting preorder traversal for this tree is ABDECFG.
Inorder
The order of the operations for a preorder traversal is:
- Traverse the left subtree
- Visit the current node
- Traverse the right subtree
The resulting inorder traversal for this tree is DEBAFCG.
Postorder
The order of the operations for a preorder traversal is:
- Traverse the left subtree
- Traverse the right subtree
- Visit the current node
The resulting postorder traversal for this tree is DEBFGCA.
Your Turn
Calculate the preorder, inorder, and postorer traversals of the tree below. Record your answers in your README
file.
A
/ \
B C
/ \ / \
D E F G
/ \ / \ / \ / \
H I J K L M N O
Binary Operators
Consider the mathematical expression (3 + 5) * (1 + 3) - 2
.
Create a binary tree out of this expression, assuming that the expression is an inorder traversal of a tree, with the parentheses being used just for grouping purposes and not needing to be included in a tree. The leaves of the tree will be numbers and the interior nodes of the tree are the mathematical operations. Your lab instructor will show you an example on the board.
In your README
document, write out the preorder and postorder traversals. The preorder traversal
corresponds to Polish
notation whereas the postorder
traversal corresponds to the more well-known reverse Polish
notation.
When you take CSCI 275, you will see these other traversals for mathematical operators in action.