Java AVLTree例外
我的班级在做AVLTree's,我有一个作业,我遇到了麻烦。我已经完成了实现,但代码运行不正常。我的老师给我们上了一堂测试课,检查我们是否做对了。有几个测试我的实现没有通过,但我现在想重点关注第一个失败的测试 这是第一个测试的代码Java AVLTree例外,java,exception-handling,avl-tree,Java,Exception Handling,Avl Tree,我的班级在做AVLTree's,我有一个作业,我遇到了麻烦。我已经完成了实现,但代码运行不正常。我的老师给我们上了一堂测试课,检查我们是否做对了。有几个测试我的实现没有通过,但我现在想重点关注第一个失败的测试 这是第一个测试的代码 'case 1: // Test 1: add and find a few entries try { AVLTree<String, Integer> tree = new AVLT
'case 1: // Test 1: add and find a few entries
try {
AVLTree<String, Integer> tree = new AVLTree<String, Integer>(
stringComp);
String st;
st = new String("hello");
tree.insert(st, new Integer(0));
st = new String("hello6");
tree.insert(st, new Integer(1));
if (tree.find(new String("hello")) == null
|| tree.find(new String("hello6")) == null) {
System.out.println("*****Test 1: Failed");
} else
System.out.println("Test 1: Passed");
} catch (AVLTreeException e) {
System.out.println("*****Test 1: Failed"); // getting this one
}`
'案例1://测试1:添加并查找一些条目
试一试{
AVLTree树=新的AVLTree树(
stringComp);
字符串st;
st=新字符串(“你好”);
插入(st,新整数(0));
st=新字符串(“hello6”);
插入(st,新整数(1));
if(tree.find(新字符串(“hello”))==null
||tree.find(新字符串(“hello6”)==null){
System.out.println(“******测试1:失败”);
}否则
System.out.println(“测试1:通过”);
}捕获(avltreee异常){
System.out.println(“******测试1:失败”);//获取这个
}`
我添加了一些print语句来检查代码在哪里被破坏,它似乎捕获了AVLException,我相信这意味着我的insert或find方法出现了错误
public void insert(K key, V value) { //inserts at leaf node
AVLnode<K, V> hold = TreeSearch(key, root); // find where to insert
AVLnode<K, V> z = hold;
DictEntry<K, V> temp = new DictEntry<K, V>(key, value); //creates the dictionary entry with key and value
z.setEntry(temp); // sets the node being inserted to the correct key and value
while (z != null) {
z.resetHeight(); // incase height is changed
if (java.lang.Math
.abs(z.left().getHeight() - z.right().getHeight()) > 1) {
z = triNodeRestructure(z.parent().parent(), z.parent(), z); //check if imbalanced
z.left().resetHeight();
z.right().resetHeight();
z.resetHeight();
break;
}
size++;
z = z.parent();
}
}
public DictEntry<K, V> find(K key) {
AVLnode<K, V> tempEntry = TreeSearch(key, root);
if (key.equals(tempEntry.element().key())) {
return tempEntry.getEntry();
} else
return null;
}
public void insert(K键,V值){//在叶节点处插入
AVLnode hold=TreeSearch(键,根);//查找插入位置
AVLnode z=保持;
DictEntry temp=newdictentry(key,value);//使用key和value创建字典条目
z、 setEntry(temp);//将要插入的节点设置为正确的键和值
while(z!=null){
z、 resetHeight();//如果高度已更改
if(java.lang.Math)
.abs(z.left().getHeight()-z.right().getHeight())>1){
z=triNodeRestructure(z.parent().parent(),z.parent(),z);//检查是否不平衡
z、 左();
z、 右();
z、 重置高度();
打破
}
大小++;
z=z.parent();
}
}
公共条目查找(K键){
AVLnode tempEntry=树搜索(键,根);
if(key.equals(tempEntry.element().key())){
返回tempEntry.getEntry();
}否则
返回null;
}
我也有一个私人的方法,我做了插入,并发现两者都使用
private AVLnode<K, V> TreeSearch(K key, AVLnode<K, V> w) { //finds the node or returns leaf node
if (external(w)) {
return w;
}
if (treeComparator.compare(key, (K) w.element().key()) < 0) {
return TreeSearch(key, w.left());
}
else if (key.equals(w.element().key())) {
return w;
} else
return TreeSearch(key, w.right());
}
private AVLnode TreeSearch(K键,AVLnode w){//查找节点或返回叶节点
if(外部(w)){
返回w;
}
if(treecocomparator.compare(key,(K)w.element().key())<0){
返回树搜索(键,w.left());
}
else if(key.equals(w.element().key())){
返回w;
}否则
返回树搜索(键,w.right());
}
任何建议或提示都将不胜感激
谢谢。自定义异常不会自动抛出。那么,
AVLTreeException
抛出在哪里呢?事实上,异常将告诉抛出它的确切行。只需执行e.printStackTrace()代码>。您正在做的,System.out.println(“****测试1:失败”)代码>,几乎完全没用。谢谢你的建议,它帮助我缩小了我的错误范围。