【问题标题】:Binary Search Tree implemented in Java - Find Element recursively用 Java 实现的二叉搜索树 - 递归查找元素
【发布时间】:2013-09-21 15:48:41
【问题描述】:

使用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!");
    }
 }

我得到的打印是“找不到元素”。

任何帮助/提示或建议,都非常欢迎。

提前致谢!

【问题讨论】:

    标签: recursion find binary-search-tree


    【解决方案1】:

    递归版本:

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

    【讨论】:

    • 当我们使用具有数千个节点的巨大树时是否有效?
    • @KamelBOUYACOUB 因为您最多触摸任何节点一次,我会说是的 - 它尽可能高效。注意,基本上这是从根节点开始对树进行 DFS 扫描。
    【解决方案2】:

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

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

    【讨论】:

      【解决方案3】:

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

      应该是这样的:

      isFound= findValueRecursively(currentNode.getLeftNode(), value);
      

      【讨论】:

        【解决方案4】:
            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;
        }
        

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

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2020-02-17
          • 1970-01-01
          • 1970-01-01
          • 2011-07-19
          • 1970-01-01
          • 1970-01-01
          • 2014-01-02
          • 2021-09-01
          相关资源
          最近更新 更多