C#在二叉搜索树中查找特定节点

C#在二叉搜索树中查找特定节点,c#,search,binary-search-tree,nodes,C#,Search,Binary Search Tree,Nodes,我一直在和BST鬼混,我对它们有很好的了解,但我希望能够在BST中搜索特定节点,并让它告诉我它是否存在。我在BST中使用字符串,一切似乎都很好,但我无法理解这个Find方法。如果有人能告诉我我做错了什么,以及如何修复它,我将不胜感激 class Node { public string number; public string data; public Node left; public Node ri

我一直在和BST鬼混,我对它们有很好的了解,但我希望能够在BST中搜索特定节点,并让它告诉我它是否存在。我在BST中使用字符串,一切似乎都很好,但我无法理解这个Find方法。如果有人能告诉我我做错了什么,以及如何修复它,我将不胜感激

class Node
    {          
        public string number;
        public string data;
        public Node left;
        public Node right;
        public string Content;
        public Node(string data)
        {
            this.data = data;
        }
    }
    class BinarySearchTree
    { 
        public Node root, current;
        public BinarySearchTree()
        {
            this.root = null;
        }

        public void AddNode(string a) // code to insert nodes to the binary search tree
        {
            Node newNode = new Node(a); //create a new node
            if (root == null) // if the tree is empty new node will be the root node
                root = newNode;
            else
            {
                Node previous;
                current = root;

                while (current != null)
                {
                    previous = current;

                    if (a.CompareTo(current.data) < 1) //if the new node is less than the            current node
                    {
                        current = current.left;
                        if (current == null)
                            previous.left = newNode;
                    }                         
                    else //if the new node is greater than the current node
                    {
                        current = current.right;

                        if (current == null)
                            previous.right = newNode;
                    }
                }
            }
        }

        public string FindNode(Node node, string s)
        {
            if (root == null)
                return Output = "not found";
            else if (s.CompareTo(root.data) < 1)
                return FindNode(root.left, s);
            else if (s.CompareTo(root.data) > 1)
                return FindNode(root.right, s);

            return Output = "found";
        }

        string SearchResult = "";
        static string Output = "";
        public string Display(Node rootNode)
        {
            if (rootNode != null)
            {
                Display(rootNode.left);
                Output += rootNode.data;
                Display(rootNode.right);
            }
            return Output;
        }           
    }

    private void btnExecute_Click(object sender, EventArgs e)
    {
        BinarySearchTree btree = new BinarySearchTree();
        btree.AddNode("D");
        btree.AddNode("B");
        btree.AddNode("F");
        btree.AddNode("E");
        btree.AddNode("A");
        btree.AddNode("G");
        btree.AddNode("C");
        string target;
        txtOutput.Text += "The sorted values of the Binary Search Tree are: \r\n \r\n";
        txtOutput.Text += btree.Display(btree.root);
        txtOutput.Text += btree.FindNode(btree.root, "A");

    }
类节点
{          
公共字符串号;
公共字符串数据;
公共节点左;
公共节点权;
公共字符串内容;
公共节点(字符串数据)
{
这个数据=数据;
}
}
类二进制搜索树
{ 
公共节点根,当前;
公共二进制搜索树()
{
this.root=null;
}
public void AddNode(string a)//将节点插入到二进制搜索树的代码
{
Node newNode=新节点(a);//创建一个新节点
if(root==null)//如果树为空,则新节点将是根节点
根=新节点;
其他的
{
节点前向;
电流=根;
while(当前!=null)
{
先前=当前;
if(a.CompareTo(current.data)<1)//如果新节点小于当前节点
{
current=current.left;
如果(当前==null)
previous.left=newNode;
}                         
else//如果新节点大于当前节点
{
current=current.right;
如果(当前==null)
previous.right=newNode;
}
}
}
}
公共字符串FindNode(节点,字符串s)
{
if(root==null)
返回输出=“未找到”;
否则如果(s.CompareTo(root.data)<1)
返回FindNode(root.left,s);
如果(s.CompareTo(root.data)>1,则为else)
返回FindNode(root.right,s);
返回Output=“found”;
}
字符串SearchResult=“”;
静态字符串输出=”;
公共字符串显示(节点根节点)
{
if(rootNode!=null)
{
显示(rootNode.left);
输出+=rootNode.data;
显示(rootNode.right);
}
返回输出;
}           
}
私有void btnExecute\u单击(对象发送者,事件参数e)
{
BinarySearchTree btree=新的BinarySearchTree();
b.添加节点(“D”);
B.添加节点(“B”);
b.添加节点(“F”);
b.添加节点(“E”);
b.添加节点(“A”);
b.添加节点(“G”);
b.添加节点(“C”);
字符串目标;
txtOutput.Text+=“二进制搜索树的排序值为:\r\n\r\n”;
txtOutput.Text+=btree.Display(btree.root);
txtOutput.Text+=btree.FindNode(btree.root,“A”);
}

尝试更改以下内容:
1.将
CompareTo
方法与0而不是1一起使用,因此
a.CompareTo(current.data)<1
应该是
a.CompareTo(current.data)<0

请参阅文档
2.由于您的
FindNode
是递归调用,请将
root
更改为
node
用法

public string FindNode(Node node, string s)
{
    if (node == null)
        return Output = "not found";
    else if (s.CompareTo(node.data) < 0)
        return FindNode(node.left, s);
    else if (s.CompareTo(node.data) > 0)
        return FindNode(node.right, s);

    return Output = "found";
}
公共字符串FindNode(节点节点,字符串s)
{
if(node==null)
返回输出=“未找到”;
如果(s.CompareTo(node.data)<0),则为else
返回FindNode(node.left,s);
如果(s.CompareTo(node.data)>0,则为else
返回FindNode(node.right,s);
返回Output=“found”;
}

祝你好运

你知道,调试器让生活变得容易多了。也就是说,FindNode()应该有node.left,而不是root.left。右边也是。此外,您的AddNode无法插入现有节点。这是完美的,找到了我的解决方案。谢谢