How to manually delete keys

Have you ever found yourself with an item identifier in your B-tree that has no corresponding item in your data file? Although this never happens in a properly coded program that carefully updates the B-tree each time the data file is updated, we've heard from users who have experienced this problem because their application was not yet debugged, or because someone manually deleted a data record with the Editor. They've asked us if it's possible to fix the B-tree by deleting the leftover key, without having to completely rebuild the B-tree.

Yes, it's easy to manually delete a key from a B-tree, if the node containing the key is a leaf and if the number of keys in the node is not equal to the node size parameter (which is usually the case). For example, assume customer 399 has been manually deleted from a data file and so item identifier 399 must now be manually deleted from the B-tree named ZIP. First use a command like LIST B-TREE WITH AMC2 = "399" to find the B-tree node containing the key to be deleted. If the B-TREE file contains more than one B-tree, be sure to pick the node for the B-tree you want. Attribute 4 of each node contains the number of the node's parent, so you can backtrack node numbers until you reach the root node pointed to by the item named ZIP (in this example) to verify you're editing the correct B-tree.

Examine the node containing the key you want to delete. If attribute 3 is empty and if the count in attribute 1 is not equal to the node size parameter (that's the number used as the second argument in BTPINS and BTPDEL calls), then simply reduce the count in attribute 1 by one and delete 399 from attribute 2 along with its trailing subvalue mark, if any. But if attribute 3 contains node numbers, or if the count in attribute 1 equals the node size parameter, then this and other B-tree nodes will have to be adjusted in ways too complicated to do manually.