Recursion 用Java实现的二叉搜索树-递归查找元素

Recursion 用Java实现的二叉搜索树-递归查找元素,recursion,find,binary-search-tree,Recursion,Find,Binary Search Tree,使用Java,是否可以编写一个递归方法来查找二进制搜索树中的元素?我之所以说不,是因为递归重新跟踪的本质,除非我实现得不正确?我一直在网上搜索,我能找到的只是一个迭代版本。以下是我的方法: public boolean findValueRecursively(BSTNode node, int value){ boolean isFound = false; BSTNode currentNode = node; if (value == currentNode.getDa

使用Java,是否可以编写一个递归方法来查找二进制搜索树中的元素?我之所以说不,是因为递归重新跟踪的本质,除非我实现得不正确?我一直在网上搜索,我能找到的只是一个迭代版本。以下是我的方法:

public boolean findValueRecursively(BSTNode node, int value){
   boolean isFound = false;
   BSTNode currentNode = node;

   if (value == currentNode.getData()){
      isFound = true;
      return isFound;
   } else if (value < currentNode.getData()){
      findValueRecursively(currentNode.getLeftNode(), value);
   } else{
      findValueRecursively(currentNode.getRightNode(), value);
   }

  return isFound;
}

// Node data structure
public class BSTNode
{
    private BSTNode leftNode;
    private BSTNode rightNode;
    private int data;
    public BSTNode(int value, BSTNode left, BSTNode right){
       this.leftNode = left;
       this.rightNode = right;
       this.data = value;
    }
}



public static void main(String[] args){
    BST bst = new BST();
    // initialize the root node
    BSTNode bstNode = new BSTNode(4, null, null);
    bst.insert(bstNode, 2);
    bst.insert(bstNode, 5);
    bst.insert(bstNode, 6);
    bst.insert(bstNode, 1);
    bst.insert(bstNode, 3); 
    bst.insert(bstNode, 7);
    if (bst.findValueRecursively(bstNode, 7)){
        System.out.println("element is found! ");
    } else{
        System.out.println("element is not found!");
    }
 }
公共布尔值递归查找(BSTNode节点,int值){
布尔值isFound=false;
BSTNode currentNode=节点;
如果(值==currentNode.getData()){
isFound=true;
发现退货;
}else if(值
我得到打印为“元素未找到”

欢迎提供任何帮助/提示或建议

提前谢谢

递归版本:

public boolean findValueRecursively(Node node, int value){
        if(node == null) return false;
        return 
                node.data == value ||
                findValueRecursively(leftNode, value) ||
                findValueRecursively(rightNode, value);
    }

返回对找到的节点的引用的递归版本:

public BinaryNode find(BinaryNode node, int value) {
    // Finds the node that contains the value and returns a reference to the node.
    // Returns null if value does not exist in the tree.                
    if (node == null) return null;
    if (node.data == value) {
        return node;
    } else {
        BinaryNode left = find(node.leftChild, value);
        BinaryNode right = find(node.rightChild, value);
        if (left != null) {
            return left;
        }else {
            return right;
        }   
    }
}

我相信你的
isFound=false
;是什么总是得到回报

应该是这样的:

isFound= findValueRecursively(currentNode.getLeftNode(), value);
公共TreeNode二进制搜索树(TreeNode节点,E数据){
如果(节点!=null){
int side=node.getData().compareTo(数据);
如果(side==0)返回节点;
else if(side<0)返回二进制搜索树(node.getRightChild(),data);
else if(side>0)返回binarySearchTree(node.getLeftChild(),data);
}
返回null;
}

这将返回对节点的引用,这是一个更有用的IRL。您可以将其更改为返回布尔值。

当我们使用具有数千个节点的巨树时,它是否有效?@KamelBOUYACOUB因为您最多只接触一次任何节点,我会说是的-它是最有效的。请注意,这基本上是从根节点开始向下对树进行DFS扫描。
    public TreeNode<E> binarySearchTree(TreeNode<E> node, E data){
    if(node != null) {
        int side = node.getData().compareTo(data);
        if(side == 0) return node;
        else if(side < 0) return binarySearchTree(node.getRightChild(), data);
        else if(side > 0 ) return binarySearchTree(node.getLeftChild(), data);
    }

    return null;
}