I implemented a binary search tree and would like to make a subclass that is self-balancing (AVL). I was getting strange results, and so I decided, to isolate the problem, I made an exact copy of the parent class MyTreeMap
, and called it dumbchild extends MyTreeMap
. Again, it's exactly the same and works correctly. Then I delete one of the methods in dumbchild
expecting it to inherit it from MyTreeMap
, and that changed the behavior of the class.
This seems like a very straightforward application of inheritance, but it's not working. I thought maybe it could have to do with the data structure being recursive.
EDIT: it was requested that I include all of the code
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);
}
}
and here is dumbchild
's declaration:
public class dumbchild<K extends Comparable<? super K>, V> extends MyTreeMap<K, V> implements Iterable<K>{
dumbchild
does not have setHeight
, but it is exactly the same in every other way (copied and pasted, replace "MyTreeMap" text with "dumbchild"). They even have the same main method for testing.
The test is to add a bunch of stuff, and then iterator through it in preorder, printing out the values, and then print the height.
MyHashMap
prints the correct height, dumbchild
prints 0. If I remove other methods from dumbchild
, other things go wrong too.
What am I missing?
Thanks