The class "node" could look something like this :
public class Node<K, V> {
public Node left_child, right_child;
public K key;
public ArrayList<V> value; // Now each key holds a list of values
// Make a subclass "NullNode" which indicates it isn't a node.
// So that left/right childs can be ommitted by passing the NullNode (e.g for leaf nodes)
public Node(Node left, Node right, K key, V value) {
this.left_child = left;
this.right_child = right;
this.key = key;
this.value.add(value);
}
// Get the node's values
public ArrayList<V> values() {
this.values;
}
// Are we a leaf node ? --> No
public boolean leaf() {
return false;
}
// Other needed procedures
}
public class NullNode extends Node<int, int> {
public NullNode() {
super(0, 0); // Just because we have to pass values
}
// We are a leaf node
public boolean leaf() {
return true;
}
}
Before doing operations on nodes you then always first have to be sure it is not a leaf node, using leaf()
method.
Notice that this is just to help you a bit further, it can be written cleaner then this.