简体   繁体   English

C# 中的二叉搜索树 (BST) 查找方法

[英]binary search tree (BST) find method in C#

how can I Write a code with these requirements?如何编写具有这些要求的代码? this is the question: check if there is a node with key K in the tree T, and if so, return a reference to this node.这就是问题:检查树 T 中是否存在键为 K 的节点,如果有,则返回对该节点的引用。 If the tree is empty, report that the node was not found and stop.如果树为空,则报告未找到该节点并停止。 Otherwise, compare K with the key value of the root node X.否则,将 K 与根节点 X 的键值进行比较。

  • If K=X, issue a link to this node and stop.如果 K=X,则发出到该节点的链接并停止。
  • If K>X, recursively search for the key K in the right subtree of T.如果 K>X,则递归搜索 T 的右子树中的键 K。
  • If K<X, recursively search for the key K in the left subtree of T.如果 K<X,则递归搜索 T 的左子树中的键 K。

This is what i found:这是我发现的:

public GenBT<T> Find(GenBT<T> k, T inf){ 

if (k == null) return null; 

else 

switch (inf.CompareTo(k.inf)){ 

case 1: return Find(k.RT, inf); 

case -1: return Find(k.LT, inf); 

case 0: return k; 

default: return null;} 

};

but I also found that if I want to search or find in BST I have to use a code like this:但我也发现,如果我想在 BST 中搜索或查找,我必须使用如下代码:

struct node* search(int data){
   struct node *current = root;
   printf("Visiting elements: ");
    
   while(current->data != data){
    
      if(current != NULL) {
         printf("%d ",current->data);
            
         //go to left tree
         if(current->data > data){
            current = current->leftChild;
         }  //else go to right tree
         else {                
            current = current->rightChild;
         }
            
         //not found
         if(current == NULL){
            return NULL;
         }
      }         
   }
   
   return current;
}

These two look very different and I don't know which way is right and in general what is the right way of solving this question这两个看起来非常不同,我不知道哪种方法是正确的,一般来说解决这个问题的正确方法是什么

To transform the recursive solution to an iterative one we need to insert a loop.要将递归解决方案转换为迭代解决方案,我们需要插入一个循环。 In the recursive case we change the node parameter for each recursion.在递归情况下,我们更改每个递归的节点参数。 To do the same in the iterative case we simply create a variable that is updated instead of doing a recursion.为了在迭代的情况下做同样的事情,我们只需创建一个更新的变量而不是进行递归。

Changed naming to make a clearer and compilable example.更改命名以使示例更清晰和可编译。 Note also that CompareTo can return any number, not just 0, 1, -1.另请注意,CompareTo 可以返回任何数字,而不仅仅是 0、1、-1。 So a switch is insufficient:所以一个开关是不够的:

    public class Node<T>
    {
        public Node<T> Left { get; }
        public Node<T> Right { get; }
        public T Value { get; }

        public Node(Node<T> left, Node<T> right, T value)
            => (Left, Right, Value) = (left, right, value);
    }

    public static Node<T> Find<T>(Node<T> root, T target) where T : IComparable<T>
    {
        var current = root;
        while (current != null)
        {
            var comparison = target.CompareTo(current.Value);
            if (comparison > 0) 
                current = current.Right;
            else if (comparison < 0)
                current = current.Left;
            else
                return current;

        }
        return null;
    }

See also how to transform a recursive method to an iterative one in a more generic way.另请参阅如何以更通用的方式将递归方法转换为迭代方法。 Note that the example uses a stack, In your case a stack is not needed, since you only process one branch of the tree.请注意,该示例使用堆栈,在您的情况下不需要堆栈,因为您只处理树的一个分支。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM