/// <summary> /// Helper method for TailMap() and HeadMap(). Adds elements to the specified map /// </summary> /// <param name="key">comparison key</param> /// <param name="node">node to start from</param> /// <param name="map">map to add to</param> /// <param name="inclusive">is the key inclusive</param> /// <param name="lesser">should it add lesser elements than the specified key</param> private void AddNodesRecursive(K key, Node <K, V> node, SortedMap <K, V> map, bool inclusive, bool lesser) { if (ReferenceEquals(node, null)) { return; } AddNodesRecursive(key, node.Left, map, inclusive, lesser); int compare = Comparator.Compare(key, node.Key); if (compare < 0 && !lesser) { map.Put(node.Key, node.Value); } else if (compare > 0 && lesser) { map.Put(node.Key, node.Value); } else if (compare == 0 && inclusive) { map.Put(node.Key, node.Value); } AddNodesRecursive(key, node.Right, map, inclusive, lesser); }
/// <summary> /// Helper method for SubMap(). Recursively adds elements to the specified map /// </summary> /// <param name="from">starting key</param> /// <param name="to">ending key</param> /// <param name="node">node to start from</param> /// <param name="map">map to add to</param> private void AddNodesRecursiveSubMap(K from, K to, Node <K, V> node, SortedMap <K, V> map) { if (ReferenceEquals(node, null)) { return; } AddNodesRecursiveSubMap(from, to, node.Left, map); int compare1 = Comparator.Compare(from, node.Key); int compare2 = Comparator.Compare(to, node.Key); if (compare1 <= 0 && compare2 > 0) { map.Put(node.Key, node.Value); } AddNodesRecursiveSubMap(from, to, node.Right, map); }