public BinaryNode <T> Seek(T Value, BinarySearchOption Option) { return(BinaryNode <T> .Search(this._Root, Value, this._Comparer, Option)); }
/// <summary> /// Searches a node for a given value; returns null if the value is not found /// </summary> /// <param name="Radix"></param> /// <param name="AWValue"></param> /// <param name="Comparer"></param> /// <param name="Option"></param> /// <returns></returns> public static BinaryNode <T> Search(BinaryNode <T> Radix, T Value, IComparer <T> Comparer, BinarySearchOption Option) { // Check first if we're searching a null node // if (Radix == null) { return(Radix); } int c = Comparer.Compare(Value, Radix.Value); // We found a match // if (c == 0 && Option == BinarySearchOption.Any) { return(Radix); } // Handle the cases where Upper/Lower are null // if (c < 0 && Radix.Lower == null) { return(null); } else if (c > 0 && Radix.Upper == null) { return(null); } // If the lower node is null, then AWValue > Radix.Upper.AWValue // int clower = (Radix.Lower == null ? 1 : Comparer.Compare(Value, Radix.Lower.Value)); // If the upper node is null, then AWValue < Radix.Lower.AWValue // int cupper = (Radix.Upper == null ? -1 : Comparer.Compare(Value, Radix.Upper.Value)); // Check again for equality // if (c == 0) { // If we equal the lower node and we're looking for the first node, search the lower node // if (clower == 0 && Option == BinarySearchOption.First) { return(Search(Radix.Lower, Value, Comparer, Option)); } // If we are equal to the upper node and we're looking for the last node, search the upper node // if (cupper == 0 && Option == BinarySearchOption.Last) { return(Search(Radix.Upper, Value, Comparer, Option)); } // Otherwise, this is the only node, so return it // return(Radix); } // Search // BinaryNode <T> Element = Radix; while (Element != null) { c = Comparer.Compare(Value, Element.Value); if (c < 0) { Element = BinaryNode <T> .Search(Element.Lower, Value, Comparer, Option); } else if (c > 0) { Element = BinaryNode <T> .Search(Element.Upper, Value, Comparer, Option); } else { return(Element); } } // Return null // return(null); }