I have a binary tree in Java that works nicely. But I want to enhance the data content in the node. Currently I can add values on it doing such as:
for( int i = 1; i <=10; i++ )
t.insert( new Integer( i ) );
Which will add the item like this:
public void insert( Comparable item ) {
current = parent = grand = header;
nullNode.element = item;
...
}
Here is the format of the tree:
private static class RedBlackNode {
// Constructors
RedBlackNode( Comparable theElement ) {
this( theElement, null, null );
}
RedBlackNode( Comparable theElement, RedBlackNode lt, RedBlackNode rt ) {
element = theElement;
left = lt;
right = rt;
color = RedBlackTree.BLACK;
}
Comparable element; // The data in the node
RedBlackNode left; // Left child
RedBlackNode right; // Right child
int color; // Color
}
For showing the tree, I do like that:
private void printTree( RedBlackNode t ) {
if( t != nullNode ) {
printTree( t.left );
System.out.println(t.element);
printTree( t.right );
}
}
While when programming in many other languages, the element would be declared as a struct, for this sample code in Java it is declared as Comparable, and currently is only taking one element as integer. My question is, how can I use it similarly as a struct, in order to be able to also manipulate it such as doing in this pseudo code:
System.out.println(t.element.valueInt);
System.out.println(t.element.firstNameString);
System.out.println(t.element.lastNameString);
I have tried different syntax combinations based on some previous posts, but none has worked so far.
For the current code version with added comments, check Gist.
All suggestions are deeply appreciated.