如何比較我的數據結構中Integer類型的2個Tree節點?如果我使用==它給NullPointerException異常誤差值大於128 ...比較Integer類型的BinarySearchTree節點
public int brElNaPutanji(BNode<Integer> tekuci, BNode<Integer> kraj){
if (tekuci == kraj)
return 0;
int trt = kraj.info;
int prt = tekuci.info;
if (trt < prt)
return 1 + brElNaPutanji(tekuci.left, kraj);
else
return 1 + brElNaPutanji(tekuci.right, kraj);
}
這裏是B節點類
class BNode<E extends Comparable<E>> {
public E info;
public BNode<E> left;
public BNode<E> right;
public BNode(E info) {
this.info = info;
left = null;
right = null;
}
public BNode(E info, BNode<E> left, BNode<E> right) {
this.info = info;
this.left = left;
this.right = right;
}
}
'return 1 + brElNaPutanji(tekuci.left,kraj);'如果'left'爲空會發生什麼? – Compass 2014-12-03 15:36:38
實際上,它永遠不能爲空,此方法計算從根節點到我們想要的節點的數量。 – mouseepaad 2014-12-03 15:41:07