// finds the TreeNode with the smallest key more than nextKey public ListItemStats <KeyType, ValueType> FindNextItem(KeyType nextKey, bool strictlyGreater) { TreeNode <KeyType, ValueType> bestNode = null; TreeNode <KeyType, ValueType> currentNode = this.rootNode; while (currentNode != null) { // choose the child to move to if (this.ChooseLeftChild(nextKey, currentNode, !strictlyGreater)) { // keep track of the rightmost node found so far that is to the left of this one bestNode = currentNode; // move to the correct child currentNode = currentNode.LeftChild; } else { // move to the correct child currentNode = currentNode.RightChild; } } ListItemStats <KeyType, ValueType> result = null; if (bestNode != null) { result = bestNode.Stats; } return(result); }
// finds the TreeNode with the largest key less than nextKey private ListItemStats <KeyType, ValueType> FindPreviousItem(KeyType nextKey, bool strictlyLess, TreeNode <KeyType, ValueType> startingNode) { TreeNode <KeyType, ValueType> bestNode = null; TreeNode <KeyType, ValueType> currentNode = startingNode; while (currentNode != null) { // remember the last node we visited this.latestNode = currentNode; // choose the child to move to if (this.ChooseLeftChild(nextKey, currentNode, strictlyLess)) { // move to the correct child currentNode = currentNode.LeftChild; } else { // keep track of the number of items before this one, including itself //lowerCount += currentNode.GetNumLeftChildren() + 1; // keep track of the rightmost node found so far that is to the left of this one bestNode = currentNode; // move to the correct child currentNode = currentNode.RightChild; } } ListItemStats <KeyType, ValueType> result = null; if (bestNode != null) { result = bestNode.Stats; } return(result); }