0
我需要在二叉搜索樹上編寫一個有序集,問題是,我不知道如何使用抽象類型並比較上下文中的這兩個對象,因爲我需要有我自己的比較聲明。通用模板ADT傻瓜
我到目前爲止,但不知何故,我卡住了,因爲可比較類型我認爲是由於我自己的比較方法而不存在的問題。
靜態節點類:
static class BinaryNode<ElementType> {
ElementType element;
BinaryNode <ElementType> right;
BinaryNode <ElementType> left;
public BinaryNode(ElementType elm) {
ElementType element = elm;
right=left=null;
}
}
方法,例如,我會感到困惑:
private BinaryNode find(ElementType x, BinaryNode t) {
while(t != null) {
if(x.compareTo(t.element) < 0)
t = t.left;
else if(x.compareTo(t.element) > 0) // is done with overrite of the comparable method, any Ideas please?
t = t.right;
else
return t; // Match
}
return null; // Not found
}
你的問題是什麼? –
我怎樣才能讓java像ElementType一樣在函數中使用聲明的通用合作伙伴 –
ElementType是什麼聲明?你提到的這個「聲明的比較器」是什麼? –