Lew wrote:
> Jeff Higgins wrote:
>> Lew wrote:
>>> Jeff Higgins wrote:
>>>> @SuppressWarnings("unchecked")
>>>> public class Node
>>>> implements Cloneable, Serializable, Comparable {
>>> It would be fun to genericize this such that the "unchecked" warnings
>>> need not be suppressed.
>>>
>>
>> How do you do that?
>
> public class Node implements Cloneable, Serializable,
> Comparable <Node>
> {
> ...
Oh! OK that's kinda neat, thanks.
import java.io.Serializable;
public class Node<T extends Comparable<T>>
implements Cloneable, Serializable, Comparable<Node<T>> {
private static final long serialVersionUID = 1L;
private final T data;
public Node(T data) {
this.data = data; }
public int height() {
// TODO
return -1; }
public int depth() {
// TODO
return -1; }
public T getData() {
return data; }
@Override
protected Object clone()
throws CloneNotSupportedException {
return super.clone(); }
@Override
public String toString() {
return data.toString(); }
@Override
public int compareTo(Node<T> that) {
return this.data.compareTo(that.data); }
}
import java.io.Serializable;
import java.util.Iterator;
@SuppressWarnings("unused")
public class BinaryTree<T extends Comparable<T>>
implements Cloneable, Serializable {
private static final long serialVersionUID = 1L;
private Node<T> root;
private BinaryTree<T> right;
private BinaryTree<T> left;
public BinaryTree() {};
public BinaryTree(Node<T> root) {
this.root = root; }
public Node<T> getRoot() {
return root; }
public int height() {
// TODO
return -1; }
private class PreOrderIterator
implements Iterator<Node<T>> {
@Override
public boolean hasNext() {
// TODO
return false; }
@Override
public Node<T> next() {
// TODO
return null; }
@Override
public void remove() {
// TODO
}
}
}
public class TestTree {
@SuppressWarnings("boxing")
public static void main(String[] args) {
Node<Integer> root =
new Node<Integer>(Integer.MAX_VALUE);
BinaryTree<Integer> tree =
new BinaryTree<Integer>(root);
System.out.println(tree.getRoot());
}
}
|