* for comments.
* @return The generated array.
*/
int[] toArray(int offset) {
// This will hold the new array.
IntVector v = new IntVector();
// A stack for traversing the tree;
Stack<IntRBTNode> nodeStack = new Stack<IntRBTNode>();
// A stack for keeping addresses associated w/ the nodes on the
// node stack.
IntStack addressStack = new IntStack();
IntRBTNode node = this;
int address;
do {
while (node.left != null || node.right != null) {
// While we have a non-terminal node...
v.add(node.key);
v.add(node.element);
if (node.left != null) {
if (node.right != null) {
v.add(IntRBTArray.TWODTRS);
addressStack.push(v.size());
v.add(-1); // Placeholder.
nodeStack.push(node.right);
} else {
v.add(IntRBTArray.LEFTDTR);
}
node = node.left;
} else {
// Because of the while loop, we know at this point that
// node.right != null
v.add(IntRBTArray.RIGHTDTR);
node = node.right;
}
}
// We have reached a terminal node...
v.add(node.key);
v.add(node.element);
v.add(IntRBTArray.TERMINAL);
if (addressStack.empty()) {
node = null;
} else {
node = (IntRBTNode) nodeStack.pop();
address = addressStack.pop();
v.set(address, v.size() + offset);
}
} while (node != null);
return v.toArray();
}