/** Source code example for "A Practical Introduction to Data Structures and Algorithm Analysis, 3rd Edition (Java)" by Clifford A. Shaffer Copyright 2008-2011 by Clifford A. Shaffer */ /** Dictionary implemented using a B+ tree. */ class BPTree, E> implements Dictionary { private BPNode root; // Root of B+ tree private int nodecount; // # of records now in 2-3 tree private static final int NUMRECS = 10; /** Stub for a binary search on the key array */ private int binaryle(Key keys[], int cnt, Key k) { return 0; } BPTree() { nodecount = 0; root = new BPLeaf(NUMRECS); } public void clear() { /** Reinitialize */ nodecount = 0; root = null; } public void insert(Key k, E e) { /** Insert an element */ root = inserthelp(root, k, e); nodecount++; } /** Remove an element */ public E remove(Key k) { E temp = findhelp(root, k); if (temp != null) { if (removehelp(root, k) && (root.numrecs() == 1)) // Collapse root if (!root.isLeaf()) root = ((BPInternal)root).pointers(0); nodecount--; } return temp; } /** Remove some element. */ public E removeAny() { System.out.println("removeany not implemented"); return null; } /** Find a record with key value "k" */ public E find(Key k) { return findhelp(root, k); } /** Return number of values in the tree */ public int size() { return nodecount; } private BPNode inserthelp(BPNode rt, Key k, E e) { BPNode retval; if (rt.isLeaf()) // At leaf node: insert here return ((BPLeaf)rt).add(k, e); // Add to internal node int currec = binaryle(rt.keys(), rt.numrecs(), k); BPNode temp = inserthelp( ((BPInternal)root).pointers(currec), k, e); if (temp != ((BPInternal)rt).pointers(currec)) return ((BPInternal)rt). add((BPInternal)temp); else return rt; } private E findhelp(BPNode rt, Key k) { int currec = binaryle(rt.keys(), rt.numrecs(), k); if (rt.isLeaf()) if ((((BPLeaf)rt).keys())[currec] == k) return ((BPLeaf)rt).recs(currec); else return null; else return findhelp(((BPInternal)rt). pointers(currec), k); } /** Delete a record with the given key value, and return true if the root underflows */ private boolean removehelp(BPNode rt, Key k) { int currec = binaryle(rt.keys(), rt.numrecs(), k); if (rt.isLeaf()) if (((BPLeaf)rt).keys()[currec] == k) return ((BPLeaf)rt).delete(currec); else return false; else // Process internal node if (removehelp(((BPInternal)rt).pointers(currec), k)) // Child will merge if necessary return ((BPInternal)rt).underflow(currec); else return false; } }