Bay*_*ayu 5 c# generics recursion icomparable binary-search-tree
为了检查二叉搜索树的有效性,我使用了一种方法。但是当针对有效的二叉搜索树进行测试时,该方法总是返回false。
编码
public class Program
{
public static void Main()
{
BinarySearchTree<int> tree = new BinarySearchTree<int>();
tree.Insert(2);
tree.Insert(1);
tree.Insert(3);
Console.WriteLine(tree.IsValidBinarySearchTreeRecursive(tree.root).ToString()); // this is supposed to return true when tested against the simple tree above
}
}
public class Node<T> where T : IComparable
{
public Node<T> left;
public Node<T> right;
public T data;
public Node(T data)
{
this.left = null;
this.right = null;
this.data = data;
}
}
public class BinarySearchTree<T> where T : IComparable
{
public Node<T> root;
public BinarySearchTree()
{
this.root = null;
}
public bool Insert(T data)
{
Node<T> before = null;
Node<T> after = this.root;
while(after != null)
{
before = after;
if(data.CompareTo(after.data) < 0)
after = after.left;
else if(data.CompareTo(after.data) > 0)
after = after.right;
else
return false;
}
Node<T> newNode = new Node<T>(data);
if (this.root == null)
{
this.root = newNode;
}
else
{
if(data.CompareTo(before.data) < 0)
before.left = newNode;
else
before.right = newNode;
}
return true;
}
private bool _HelperForIsValidBinarySearchTreeRecursive(Node<T> node, T lower, T upper)
{
if (node == null)
return true;
T val = node.data;
Type nodeType = typeof(T);
if(nodeType.IsNumeric())
{
if(val.CompareTo(lower) <= 0)
return false;
if(val.CompareTo(upper) >= 0)
return false;
}
else
{
if(lower != null && val.CompareTo(lower) <= 0)
return false;
if(upper != null && val.CompareTo(upper) >= 0)
return false;
}
if(!_HelperForIsValidBinarySearchTreeRecursive(node.right, val, upper))
return false;
if(!_HelperForIsValidBinarySearchTreeRecursive(node.left, lower, val))
return false;
return true;
}
public bool IsValidBinarySearchTreeRecursive(Node<T> root)
{
Type nodeType = typeof(T);
if(nodeType.IsNumeric())
return _HelperForIsValidBinarySearchTreeRecursive(root, root.data, root.data);
return _HelperForIsValidBinarySearchTreeRecursive(root, default(T), default(T));
}
}
public static class StaticUtilities
{
private static readonly HashSet<Type> NumericTypes = new HashSet<Type>
{
typeof(int), typeof(double), typeof(decimal),
typeof(long), typeof(short), typeof(sbyte),
typeof(byte), typeof(ulong), typeof(ushort),
typeof(uint), typeof(float)
};
public static bool IsNumeric(this Type myType)
{
return NumericTypes.Contains(Nullable.GetUnderlyingType(myType) ?? myType);
}
}
Run Code Online (Sandbox Code Playgroud)
你的问题在这里:
if(val.CompareTo(lower) <= 0)
return false; // you are always hitting this line
if(val.CompareTo(upper) >= 0)
return false;
Run Code Online (Sandbox Code Playgroud)
因为你T val = node.data;和你的下层,甚至你的上层都是node.data来自你的第一次通话。
所以最有可能修复的行是
return _HelperForIsValidBinarySearchTreeRecursive(root, root.data, root.data);
Run Code Online (Sandbox Code Playgroud)
我猜你的初衷是比较左右与根?
基于此: https: //www.geeksforgeeks.org/a-program-to-check-if-a-binary-tree-is-bst-or-not/
对于您的数字方法,您的解决方案应该是:
/* false if this node violates the min/max constraints */
if (node.data < min || node.data > max)
{
return false;
}
Run Code Online (Sandbox Code Playgroud)
所以你的代码变成:
if(val.CompareTo(lower) < 0)
return false; // you are always hitting this line
if(val.CompareTo(upper) > 0)
return false;
Run Code Online (Sandbox Code Playgroud)
然后你将遇到下一个障碍。我对解决方案的建议是改变这一点:
private bool _HelperForIsValidBinarySearchTreeRecursive(Node<T> node, T lower, T upper)
Run Code Online (Sandbox Code Playgroud)
到
private bool _HelperForIsValidBinarySearchTreeRecursive(Node<T> node, Node<T> leftNode<T> right)
Run Code Online (Sandbox Code Playgroud)
并像这样调用:
_HelperForIsValidBinarySearchTreeRecursive(root, root.Left, root.Right)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
407 次 |
| 最近记录: |