我在Java中有一个很好的二叉树.但是我想增强节点中的数据内容.目前,我可以为其添加值,例如:
for( int i = 1; i <=10; i++ )
t.insert( new Integer( i ) );
它将添加如下内容:
public void insert( Comparable item ) {
current = parent = grand = header;
nullNode.element = item;
...
}
这是树的格式:
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
}
为了显示树,我这样做:
private void printTree( RedBlackNode t ) {
if( t != nullNode ) {
printTree( t.left );
System.out.println(t.element);
printTree( t.right );
}
}
当使用许多其他语言进行编程时,该元素将声明为struct,对于Java中的sample code,该元素将声明为Comparable,当前仅将一个元素作为整数.我的问题是,如何才能将其类似地用作结构,以便能够像在此伪代码中那样操作它:
System.out.println(t.element.valueInt);
System.out.println(t.element.firstNameString);
System.out.println(t.element.lastNameString);
我已经根据先前的posts尝试了不同的语法组合,但是到目前为止,都没有奏效.
有关带有注释的当前代码版本,请检查Gist.
所有建议深表感谢.