我实现了一个二进制搜索树,并想创建一个自平衡(AVL)的子类。我得到的结果很奇怪,所以我决定隔离问题,我精确复制了父类MyTreeMap,并将其命名为dumbchild extends MyTreeMap。同样,它完全相同并且可以正常工作。然后,我删除dumbchild中的一种方法,希望它可以从MyTreeMap继承它,这改变了类的行为。

这似乎是继承的非常简单的应用程序,但是它不起作用。我认为可能与递归的数据结构有关。

编辑:要求我包括所有代码

import java.util.Iterator;

public class MyTreeMap<K extends Comparable<? super K>, V> implements Iterable<K>{

    public K key;
    public V value;
    public int height = 0;
    public MyTreeMap<K, V> left, right;

    protected void setHeight(){ // if key is not null, left and right should not be null
        if(key == null){
            height = 0;
        }
        else{
            height = 1 + Math.max(left.height, right.height);
        }
        System.out.println("set of " + key + " height to " + height);
    }


    public V put(K key, V value){
        if(key == null){
            throw new NullPointerException();
        }
        V ret;
        if(this.key == null){ // empty leaf, place found
            this.key = key;
            this.value = value;
            left = new MyTreeMap<>();
            right = new MyTreeMap<>();
            ret =  null;
        }
        else{
            int compare = key.compareTo(this.key);
            if(compare == 0){ //replace
                this.value = value;
                ret =  value;
            }
            else if(compare < 0){ // put to left
                ret =  left.put(key, value);
            }
            else{
                ret =  right.put(key, value);
            }
        }
        setHeight();
        return ret;
    }

    public Iterator<K> iterator(){
        return new Iterator<K>(){
            Iterator<K> l, r;
            K current = MyTreeMap.this.key;
            {
                if(left != null) l = left.iterator();
                if(right != null) r = right.iterator();
            }

            public boolean hasNext(){
                return current != null;
            }

            public K next(){
                K ret = current;
                if(l!= null && l.hasNext()){
                    current = l.next();
                }
                else if(r!= null && r.hasNext()){
                    current = r.next();
                }
                else{
                    current = null;
                }
                return ret;
            }
        };
    }

    public static void main(String[] args){
        MyTreeMap<Integer, String> t = new MyTreeMap<>();
        for(int i = 0; i<64; i++){
            t.put(i, null);
        }
        Iterator<Integer> it = t.iterator();
        while(it.hasNext()){
            System.out.println(it.next());
        }
        System.out.println("height: " + t.height);

    }


}


这是dumbchild的声明:

public class dumbchild<K extends Comparable<? super K>, V> extends MyTreeMap<K, V> implements Iterable<K>{


dumbchild没有setHeight,但是在其他所有方面都完全相同(复制和粘贴,将“ MyTreeMap”文本替换为“ dumbchild”)。他们甚至具有相同的主要测试方法。

测试是添加一堆东西,然后以预定顺序迭代它,打印出值,然后打印高度。

MyHashMap打印正确的高度,dumbchild打印0。如果我从dumbchild删除其他方法,其他事情也会出错。

我想念什么?

谢谢

最佳答案

我使用以下代码进行了测试,并且dumbchild正确地将高度打印为64。最初是否存在任何问题?我所做的就是在返回remove()匿名实例的代码中添加Iterator<T>的定义。

import java.util.Iterator;

class dumbchild<K extends Comparable<? super K>, V> extends MyTreeMap<K, V> implements Iterable<K>{
}

public class MyTreeMap<K extends Comparable<? super K>, V> implements Iterable<K>{

    public K key;
    public V value;
    public int height = 0;
    public MyTreeMap<K, V> left, right;

    protected void setHeight(){ // if key is not null, left and right should not be null
        if(key == null){
            height = 0;
        }
        else{
            height = 1 + Math.max(left.height, right.height);
        }
        System.out.println("set of " + key + " height to " + height);
    }


    public V put(K key, V value){
        if(key == null){
            throw new NullPointerException();
        }
        V ret;
        if(this.key == null){ // empty leaf, place found
            this.key = key;
            this.value = value;
            left = new MyTreeMap<>();
            right = new MyTreeMap<>();
            ret =  null;
        }
        else{
            int compare = key.compareTo(this.key);
            if(compare == 0){ //replace
                this.value = value;
                ret =  value;
            }
            else if(compare < 0){ // put to left
                ret =  left.put(key, value);
            }
            else{
                ret =  right.put(key, value);
            }
        }
        setHeight();
        return ret;
    }

    public Iterator<K> iterator(){
        return new Iterator<K>() {
            Iterator<K> l, r;
            K current = MyTreeMap.this.key;
            {
                if(left != null) l = left.iterator();
                if(right != null) r = right.iterator();
            }

            public boolean hasNext(){
                return current != null;
            }

            public K next(){
                K ret = current;
                if(l!= null && l.hasNext()){
                    current = l.next();
                }
                else if(r!= null && r.hasNext()){
                    current = r.next();
                }
                else{
                    current = null;
                }
                return ret;
            }

            @Override
            public void remove() {
                // ?
            }
        };
    }

    public static void main(String[] args){
        /*
        MyTreeMap<Integer, String> t = new MyTreeMap<>();
        for(int i = 0; i<64; i++){
            t.put(i, null);
        }
        Iterator<Integer> it = t.iterator();
        while(it.hasNext()){
            System.out.println(it.next());
        }
        System.out.println("height: " + t.height);
        */
        dumbchild<Integer, String> c = new dumbchild<>();
        for(int i = 0; i<64; i++){
            c.put(i, null);
        }
        Iterator<Integer> ct = c.iterator();
        while(ct.hasNext()){
            System.out.println(ct.next());
        }
        System.out.println("height: " + c.height);
    }
}

08-19 01:32