1-Relational Database/1-Relational DB Concepts/B-Tree & B+ Tree/java/BPlusTree.java
/**
     * Searches for the given range of keys in this B+ tree, and returns the
     * associated record addresses if found.
     * @param fromKey key lower bound (inclusive)
     * @param toKey key upper bound (inclusive)
     * @return list of associated record addresses
     */
Searches for the given range of keys in this B+ tree, and returns the
associated record addresses if found.
@param fromKey key lower bound (inclusive)
@param toKey key upper bound (inclusive)
@return list of associated record addresses
Searches for the given range of keys in this B + tree and returns the associated record addresses if found . @param fromKey key lower bound ( inclusive ) @param toKey key upper bound ( inclusive ) @return list of associated record addresses
public List<String> rangeSearch(int fromKey, int toKey) {
        if (fromKey > toKey) {
            throw new IllegalArgumentException("fromKey must be <= toKey");
        }
        if (root == null) {
            return new ArrayList<>();
        }
        locateLeafAndPos(root, fromKey);
        BPlusTreeLeaf leafPtr = leaf;
        int posPtr = leafPos;
        List<String> result = new ArrayList<>();
        while (leafPtr != null) {
            if (posPtr < leafPtr.size()) {
                IndexEntry entry = (IndexEntry) leafPtr.getEntry(posPtr);
                if (entry.getKey() > toKey) {
                    break;
                }
                result.add(entry.getRecordAddress());
                ++posPtr;
            } else {
                leafPtr = leafPtr.next;
                posPtr = 0;
            }
        }
        return result;
    }
public List < String > rangeSearch ( int fromKey , int toKey ) { if ( fromKey > toKey ) { throw new IllegalArgumentException ( "fromKey must be <= toKey" ) ; } if ( root == null ) { return new ArrayList < > ( ) ; } locateLeafAndPos ( root , fromKey ) ; BPlusTreeLeaf leafPtr = leaf ; int posPtr = leafPos ; List < String > result = new ArrayList < > ( ) ; while ( leafPtr != null ) { if ( posPtr < leafPtr . size ( ) ) { IndexEntry entry = ( IndexEntry ) leafPtr . getEntry ( posPtr ) ; if ( entry . getKey ( ) > toKey ) { break ; } result . add ( entry . getRecordAddress ( ) ) ; ++ posPtr ; } else { leafPtr = leafPtr . next ; posPtr = 0 ; } } return result ; }
Searches for the given range of keys in this B+ tree, and returns the
 associated record addresses if found.
Searches for the given range of keys in this B + tree and returns the associated record addresses if found .
// 1. Find the fromKey // 2. Use the fact that all the leaves are connect, simply do a linear scan along the leaves (and thus the //    keys), find all the keys within the given range