2017-03-18 58 views
0

我想實現一個基於有序字典的AVL樹,這些類是我給的,但是我遇到了構造函數的問題。這是AVL類:Java AVL樹構造器實現

public class AVLTree implements AVLTreeInterface { 
private Comparator comp; 
private AVLnode avlentry; 
private AVLnode root; 
private int size; 

/* 
* Constructor which initializes AVL tree to hold 0 entries 
* Sets comparator object to one provided in input 
* "Empty" tree consists of 1 external node, as the root, that does not hold any entries 
*/ 
public AVLTree(Comparator inputComparator){ 

DictEntry entry = new DictEntry(0,0); 

    root = new AVLnode(entry,avlentry,avlentry,avlentry); 
    comp = inputComparator; 

} 

而且它是基於這個類AVLnode

//package a3; 
import java.lang.*; 
public class AVLnode implements Position{ 

private AVLnode parent;  // reference to the parent node 
private AVLnode left;  // reference to the left child 
private AVLnode right;  // reference to the right child 
private DictEntry entry; // reference to the entry stored at the node 
private int height;   // height of the node for checking balance-height property 

public AVLnode(DictEntry inputEntry, AVLnode inputParent, AVLnode inputLeft, AVLnode inputRight) 
{ 
    entry = inputEntry; 
    parent = inputParent; 
    left = inputLeft; 
    right = inputRight; 
    height = 0; 
    if (left != null) height = Math.max(height,1+left.getHeight()); 
    if (right != null) height = Math.max(height,1+right.getHeight()); 
} 

public AVLnode parent(){ return parent;} 
public AVLnode left() {return left;} 
public AVLnode right() {return right;} 
public int getHeight() { return height; } 
public DictEntry getEntry() { return entry; } 
public void setParent(AVLnode newParent){ parent = newParent; } 
public void setLeft(AVLnode newLeft) {left = newLeft;} 
public void setRight(AVLnode newRight) { right = newRight; } 
public void setEntry(DictEntry newEntry) { entry = newEntry; } 
public Object element(){return entry;} 

public void resetHeight() throws AVLtreeException{ 
if (left == null || right == null) throw new AVLtreeException("Attempt to    update height for external node "); 
height = 1+Math.max(left.getHeight(),right.getHeight()); 
    } 

}

的主要問題是,在AVLnode resetHeight例外的例外不工作,和我不知道這是因爲我如何添加根

編輯: 1錯誤發現: 文件:C:\ xxxxxxxxxxxx x \ AVLnode.java [line:33] 錯誤:類AVLtreeException中的構造函數AVLtreeException不能應用於給定的類型; 要求:無參數 發現:java.lang.String中 原因:實際的和正式的參數列表長度

不同的是錯誤即時得到

/** 
* Auto Generated Java Class. 
*/ 
import java.lang.*; 
public class AVLtreeException extends Throwable { 

    /* ADD YOUR CODE HERE */ 

} 

謝謝!

回答

0

您聲明AVLtreeException沒有任何自定義構造函數,這意味着會自動生成默認構造函數(不帶參數)。但是,您嘗試將異常類傳遞字符串(錯誤消息)作爲參數進行實例化。請參閱this answer瞭解如何正確劃分Exception類。