Homework: Binary Search Trees
This homework is necessary preparation for the lab.
Consider the following definition IS_BST that defines binary search
trees, and answer the questions below.
/**
* @mathdefinitions <pre>
* IS_BST(
* tree: binary tree of T
* ): boolean satisfies
* [tree satisfies the binary search tree properties as described in the
* slides with the ordering reported by compareTo for T, including that
* it has no duplicate labels]
* </pre>
*/
-
Write the body for the following static, generic method that searches a given binary search tree,
t(of typeBinaryTree<T>), for a given label,x(of typeT), and returns true if it finds it, and false otherwise. Note that theBinaryTreemust be restored, i.e., its outgoing value must be the same as its incoming value. Make sure your implementation takes advantage of the fact that the given tree is a binary search tree./** * Returns whether {@code x} is in {@code t}. * * @param <T> * type of {@code BinaryTree} labels * @param t * the {@code BinaryTree} to be searched * @param x * the label to be searched for * @return true if t contains x, false otherwise * @requires IS_BST(t) * @ensures isInTree = (x is in labels(t)) */ public static <T extends Comparable<T>> boolean isInTree(BinaryTree<T> t, T x) {...}The
isInTreemethod has a new and interesting property: the generic parameterTis required to extendComparable<T>. Thejava.lang.Comparable<T>interface defines only one method,int compareTo(T), which allows us to compare twoTs to see if one is less than, equal to, or greater than the other, returning a negative, zero, or positive result, respectively. Make sure you use this method in your solution to allow you to search only one of the subtrees. -
Using the binary search tree algorithms discussed in class and alphabetical order:
-
Draw the binary search tree that would result from inserting the following sequence of items into an initially empty binary search tree:
Matt, Zeke, Pete, Lon, John, Mei, Larry, Bess, Merv, Adam, Kate -
Draw the binary search tree resulting from removing
Petefrom the binary search tree in A. -
Draw the binary search tree resulting from removing
Johnfrom the binary search tree in B. -
Draw the binary search tree resulting from removing
Lonfrom the binary search tree in C. -
Draw the binary search tree resulting from removing
Mattfrom the binary search tree in D.
-
Additional Questions
-
Write the body for the following static, generic method that removes and returns the smallest (left-most) label in the given binary search tree (a
BinaryTree<T>)./** * Removes and returns the smallest (left-most) label in {@code t}. * * @param <T> * type of {@code BinaryTree} labels * @param t * the {@code BinaryTree} from which to remove the label * @return the smallest label in the given {@code BinaryTree} * @updates t * @requires IS_BST(t) and |t| > 0 * @ensures <pre> * IS_BST(t) and removeSmallest = [the smallest label in #t] and * labels(t) = labels(#t) \ {removeSmallest} * </pre> */ public static <T> T removeSmallest(BinaryTree<T> t) {...}