/** * Gets a byte from the AVLNode payload * * @param offset The offset into the payload (in bytes). * @return The requested byte value */ public int getPayloadByte(int offset) { assert offset > 0; return block.getByte(IDX_PAYLOAD_B + offset); }
/** * Gets the Balance of the AVLNode * * @return The Balance value */ int getBalance() { return block.getByte(IDX_BALANCE_B); }
/** * 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 * 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; }
/** * 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; }
/** * 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; }
/** * 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; }
balance = (node.block.getByte(IDX_BALANCE_B) + 1) - (ci * 2); node.modify(); node.block.putByte(IDX_BALANCE_B, (byte) balance);
balance = node.block.getByte(IDX_BALANCE_B) - (1 - (ci * 2)); node.modify(); node.block.putByte(IDX_BALANCE_B, (byte) balance);
int ci; if (block.getByte(IDX_BALANCE_B) > 0) { childNode = getLeftChildNode(); adjacentNode = childNode.getMaxNode_R(); adjacentNode.block.putLong(IDX_LEFT, block.getLong(IDX_LEFT)); adjacentNode.block.putLong(IDX_RIGHT, block.getLong(IDX_RIGHT)); adjacentNode.block.putByte(IDX_BALANCE_B, block.getByte(IDX_BALANCE_B));