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

时间:2015-11-25 21:38:30

标签: c# search binary-search-tree nodes

一直在与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");

    }

1 个答案:

答案 0 :(得分:1)

尝试更改以下内容:
1.使用CompareTo方法,0而不是1,因此a.CompareTo(current.data) < 1应为a.CompareTo(current.data) < 0
请参阅文档IComparable.CompareTo Method
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";
}
祝你好运!