Hi there, I'm new to tree maps and I am trying to write my own version of the pre-defined "remove" function for Tree Maps. I know I need to have two extra functions that find the minimum and maximum values in the map that will be called within the remove function, I just don't know if I'm going about this correctly as I'm struggling to fully understand Tree Maps.
Here's what I currently have (only posting the relevant code for now):
Main (creating two arrays and inserting them into the map):
public static void main(String[] args) {
//Map<String, Integer> people = new TreeMap<String, Integer>();
Map<String, Integer> people = new SearchTreeMap<String, Integer>();
String names[] = {
"jerry", "paul", "erin", "marty", "kevin",
"sam", "bill", "john", "bob", "rick", "tim",
};
Integer ages[] = { 37, 22, 17, 52, 87, 47, 25, 34, 49, 35, 33, };
for(int i = 0; i < names.length; ++i) {
people.put(names[i], ages[i]);
}
System.out.println(people);
System.out.println("size = " + people.size());
if(people instanceof SearchTreeMap) {
System.out.println("--------------");
((SearchTreeMap<String, Integer>) people).reverseInorderOutput();
System.out.println("--------------");
}
System.out.println();
System.out.println("remove erin: " + people.remove("erin"));
System.out.println("remove erin: " + people.remove("erin"));
System.out.println("remove jerry: " + people.remove("jerry"));
System.out.println("remove paul: " + people.remove("paul"));
System.out.println(people);
System.out.println("size = " + people.size());
if(people instanceof SearchTreeMap) {
System.out.println("--------------");
((SearchTreeMap<String, Integer>) people).reverseInorderOutput();
System.out.println("--------------");
}
}
}
Remove function (so far):
private Node removeMax(Node n, Mutable<K> key, Mutable<V> val) {
if(n.right == null) {
val.set(n.value);
key.set(n.key);
return n.left;
}
else {
n.right = removeMax(n.right, key, val);
return n;
}
}
private Node removeMin(Node n, Mutable<K> key, Mutable<V> val) {
if(n.left == null) {
val.set(n.value);
key.set(n.key);
return n.right;
}
else {
n.left = removeMin(n.left, key, val);
return n;
}
}
@Override
public V remove(Object obj) {
Mutable<K> save = new Mutable<K>();
Mutable<V> val = new Mutable<V>();
//TESTING
Node n = root;
System.out.println(n.key + " " + n.value);
//TESTING MIN/MAX FUNCTION RESULTS
n = removeMin(n, save, val);
System.out.println(n.key);
System.out.println(n.value);
n = removeMax(n, save, val);
System.out.println(n.key);
System.out.println(n.value);
return null;
}
As you can see, the actual remove function is bare. I haven't yet worked out the logic for it yet because I want to have the MIN/MAX functions correct first.
Here's a sample output of the above code:
run:
[bill=25, bob=49, erin=17, jerry=37, john=34, kevin=87, marty=52, paul=22, rick=35, sam=47, tim=33]
size = 11
--------------
tim=33
sam=47
rick=35
paul=22
marty=52
kevin=87
john=34
jerry=37
erin=17
bob=49
bill=25
--------------
jerry 37
jerry
37
jerry
37
remove erin: null
jerry 37
jerry
37
jerry
37
remove erin: null
jerry 37
jerry
37
jerry
37
remove jerry: null
jerry 37
paul
22
marty
52
remove paul: null
[jerry=37, john=34, kevin=87, marty=52, paul=22, null, null, null, null, null, null]
size = 11
--------------
paul=22
marty=52
kevin=87
john=34
jerry=37
--------------
So as you can see it returns the correct minimum and maximum keys/values ONLY on that one function call (lines 37-40). Also, why is it setting things to null? I'm a little confused by this, any suggestions?
I know this may be a lot to take in for one post, so if you need more of an explanation or code, please let me know! Thank you!