2015-11-25 81 views
1

已經被BST所困擾,我對它們有一個很好的想法,但我希望能夠在BST中搜索特定的節點並告訴我它是否存在。我在BST中使用字符串,一切似乎都工作正常,但我無法弄清楚這個Find方法。如果有人可以告訴我我做錯了什麼,以及如何解決這個問題,這將不勝感激。C#查找二進制搜索樹中的特定節點

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"); 

    } 
+0

你知道,調試器讓生活變得更容易。也就是說,FindNode()應該有node.left,而不是root.left。相同的權利。您的AddNode也無法插入現有節點。 –

回答

0

試圖改變如下:0而不是1
1.使用CompareTo方法,所以a.CompareTo(current.data) < 1應該a.CompareTo(current.data) < 0
查看文檔IComparable.CompareTo Method
2.您FindNode是遞歸調用,改變rootnode用法

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"; 
} 

祝你好運!

+0

這很完美,找到了我的解決方案。謝謝 –