/** * Writes a string representation of this tree structure into a * {@link java.lang.StringBuffer}. * * @param sb The string buffer to write to. */ private void toString(StringBuffer sb) { sb.append(getId()); sb.append(" ["); AVLNode tmp; if ((tmp = getLeftChildNode()) == null) { sb.append("-"); } else { tmp.toString(sb); } sb.append(", "); if ((tmp = getRightChildNode()) == null) { sb.append("-"); } else { tmp.toString(sb); } sb.append("]"); }
/** * Gets the NextNode attribute of the AVLNode object. The current node is not released. * * @return The NextNode value */ public AVLNode getNextNode() { AVLNode node; if ((node = getRightChildNode()) != null) { return node.getMinNode_R(); } node = this; while (node.childIndex == IDX_RIGHT) { if ((node = node.parentNode) == null) { return null; } } node = node.parentNode; if (node != null) { node.incRefCount(); } return node; }
/** * The rotate which is performed when the node is heavy on the right * sub-branch. * * @return the change in height of the node. * @throws IOException If an I/O error occurs. */ private int rotateR() throws IOException { AVLNode nodeR = getRightChildNode(); int deltaHeight = (nodeR.block.getByte(IDX_BALANCE_B) <= 0) ? rotateRR(nodeR) : rotateRL(nodeR); nodeR.release(); return deltaHeight; }
/** * Gets the Height attribute of the AVLNode object * * @return The Height value */ public int getHeight() { AVLNode lChildNode = getLeftChildNode(); int leftHeight = (lChildNode != null) ? lChildNode.getHeight() : 0; if (lChildNode != null) { lChildNode.release(); } AVLNode rChildNode = getRightChildNode(); int rightHeight = (rChildNode != null) ? rChildNode.getHeight() : 0; if (rChildNode != null) { rChildNode.release(); } // Check the balance value. int balance = leftHeight - rightHeight; if (balance != block.getByte(IDX_BALANCE_B)) { throw new RuntimeException( "Incorrect balance for node " + getId() + ". Expected: " + balance + " but was: " + block.getByte(IDX_BALANCE_B) ); } return Math.max(leftHeight, rightHeight) + 1; }
/** * The rotate which is performed when the node is heavy on the left child's * right sub-branch. * * @param nodeL a reference to the left child. * @return the change in height of the node. * @throws IOException If an I/O error occurs. */ private int rotateLR(AVLNode nodeL) throws IOException { // First perform rotateRR on nodeL. AVLNode nodeLR = nodeL.getRightChildNode(); int deltaHeightL = nodeL.rotateRR(nodeLR); // Adjust the balance of this node. if (deltaHeightL != 0) { modify(); block.putByte( IDX_BALANCE_B, (byte)(block.getByte(IDX_BALANCE_B) + deltaHeightL) ); } // Finally do a rotateLL on this node. int deltaHeight = deltaHeightL + rotateLL(nodeLR); nodeLR.release(); return deltaHeight; }
ci = IDX_LEFT; } else { childNode = getRightChildNode(); adjacentNode = childNode.getMinNode_R(); ci = IDX_RIGHT;