/** * 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 PrevNode attribute of the AVLNode object. The current node is not released. * * @return The PrevNode value */ public AVLNode getPrevNode() { AVLNode node; if ((node = getLeftChildNode()) != null) { return node.getMaxNode_R(); } node = this; while (node.childIndex == IDX_LEFT) { 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 left * sub-branch. * * @return the change in height of the node. * @throws IOException If an I/O error occurs. */ private int rotateL() throws IOException { AVLNode nodeL = getLeftChildNode(); int deltaHeight = (nodeL.block.getByte(IDX_BALANCE_B) >= 0) ? rotateLL(nodeL) : rotateLR(nodeL); nodeL.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 right child's * left sub-branch. * * @param nodeR a reference to the right child. * @return the change in height of the node. * @throws IOException If an I/O error occurs. */ private int rotateRL(AVLNode nodeR) throws IOException { // First perform rotateLL on nodeR. AVLNode nodeRL = nodeR.getLeftChildNode(); int deltaHeightR = nodeR.rotateLL(nodeRL); // Adjust the balance of this node. if (deltaHeightR != 0) { modify(); block.putByte( IDX_BALANCE_B, (byte)(block.getByte(IDX_BALANCE_B) - deltaHeightR) ); } // Finally do a rotateRR on this node. int deltaHeight = deltaHeightR + rotateRR(nodeRL); nodeRL.release(); return deltaHeight; }
childNode = getLeftChildNode(); adjacentNode = childNode.getMaxNode_R(); ci = IDX_LEFT;