Java-包括BST
我试图用java解决这个BST问题,但我不知道为什么它不起作用。 问题是:Java-包括BST,java,Java,我试图用java解决这个BST问题,但我不知道为什么它不起作用。 问题是: 二叉搜索树(BST)是一种二叉树,其中 节点大于或等于该节点中所有节点的值 左子树,小于该树中所有节点中的值 节点的右子树 编写一个函数,检查给定的二进制搜索树是否包含 给定值 例如,对于以下树: n1(值:1,左:空,右:空)n2(值:2,左:n1,右: n3)n3(值:3,左:null,右:null)调用contains(n2,3) 应返回true,因为根位于n2的树包含数字3 这就是我试图解决它的方式:
- 二叉搜索树(BST)是一种二叉树,其中 节点大于或等于该节点中所有节点的值 左子树,小于该树中所有节点中的值 节点的右子树 编写一个函数,检查给定的二进制搜索树是否包含 给定值 例如,对于以下树: n1(值:1,左:空,右:空)n2(值:2,左:n1,右: n3)n3(值:3,左:null,右:null)调用contains(n2,3) 应返回true,因为根位于n2的树包含数字3
class Node {
public int value;
public Node left, right;
public Node(int value, Node left, Node right) {
this.value = value;
this.left = left;
this.right = right;
}
}
public class BinarySearchTree {
public static boolean contains(Node root, int value) {
if(root.value == value)
return true;
else if(value < root.value){
if(root.left == null)
return false;
contains(root.left, value);
}
else if(value > root.value){
if(root.right == null)
return false;
contains(root.right, value);
}
return false;
}
public static void main(String[] args) {
Node n1 = new Node(1, null, null);
Node n3 = new Node(3, null, null);
Node n2 = new Node(2, n1, n3);
System.out.println(contains(n2, 3));
}
}
类节点{
公共价值观;
公共节点左、右;
公共节点(int值、左节点、右节点){
这个值=值;
this.left=左;
这个。右=右;
}
}
公共类二进制搜索树{
公共静态布尔包含(节点根,int值){
if(root.value==值)
返回true;
else if(值<根值){
if(root.left==null)
返回false;
包含(root.left,值);
}
else if(值>根值){
if(root.right==null)
返回false;
包含(root.right,value);
}
返回false;
}
公共静态void main(字符串[]args){
节点n1=新节点(1,null,null);
节点n3=新节点(3,null,null);
节点n2=新节点(2,n1,n3);
系统输出println(包含(n2,3));
}
}
这应该返回true,但它不。。。提前感谢。return contains(…)
而不是只为这些递归调用调用contains(…)
,这样在进行这些调用时,您就不会丢失值return contains(…)
而不是只为这些递归调用调用contains(…)
,这样在创建公共类BinarySearchTree时不会丢失值{
public class BinarySearchTree {
public static boolean contains(Node root, int value) {
if(root.value==value)
return true;
else if(value<root.value && root.left!=null)
return contains(root.left,value);
else if(value> root.value && root.right!=null)
return contains(root.right,value);
else return false;
}
公共静态布尔包含(节点根,int值){
if(root.value==value)
返回true;
else if(value root.value&&root.right!=null)
返回包含(root.right,value);
否则返回false;
}
公共类二进制搜索树{
公共静态布尔包含(节点根,int值){
if(root.value==value)
返回true;
else if(value root.value&&root.right!=null)
返回包含(root.right,value);
否则返回false;
}
>P>我写了一个C++版本,看起来更明白了。
#include <stdexcept>
#include <string>
#include <iostream>
#include <algorithm>
class Node
{
public:
Node(int value, Node* left, Node* right)
{
this->value = value;
this->left = left;
this->right = right;
}
int getValue() const
{
return value;
}
Node* getLeft() const
{
return left;
}
Node* getRight() const
{
return right;
}
private:
int value;
Node* left;
Node* right;
};
class BinarySearchTree
{
public:
static bool isValidBST( Node& root)
{
if ((root.getLeft())&&(root.getValue()<=(root.getLeft()->getValue())))
return false;
if ((root.getRight())&&(root.getValue()>=(root.getRight()->getValue())))
{
return false;
}
if (root.getRight())
{
if(!isValidBST(*(root.getRight()))) return false;
}
if(root.getLeft())
{
if(!isValidBST(*(root.getLeft()))) return false;
}
return true;
}
};
#ifndef RunTests
int main(int argc, const char* argv[])
{
Node n7(7, NULL, NULL);
Node n10(11, NULL, NULL);
Node n2(2, NULL, NULL);
Node n5(5, NULL, NULL);
Node n4(4, &n2, &n5);
Node n9(9, &n7, &n10);
Node n6(6, &n4, &n9);
std::cout << BinarySearchTree::isValidBST(n6);
return 0;
}
#endif
#包括
#包括
#包括
#包括
类节点
{
公众:
节点(int值,节点*左,节点*右)
{
这个->值=值;
这个->左=左;
这个->右=右;
}
int getValue()常量
{
返回值;
}
节点*getLeft()常量
{
左转;
}
节点*getRight()常量
{
返还权;
}
私人:
int值;
节点*左;
节点*右;
};
类二进制搜索树
{
公众:
静态bool isvalidbtst(节点和根)
{
if((root.getLeft())&&(root.getValue()getValue()))
返回false;
如果((root.getRight())&&(&&(root.getValue()>=(root.getRight()->getValue()))
{
返回false;
}
if(root.getRight())
{
if(!isvalidbtst(*(root.getRight()))返回false;
}
if(root.getLeft())
{
如果(!isvalidbtst(*(root.getLeft()))返回false;
}
返回true;
}
};
#ifndef运行测试
int main(int argc,const char*argv[]
{
节点n7(7,NULL,NULL);
节点n10(11,NULL,NULL);
节点n2(2,NULL,NULL);
节点n5(5,NULL,NULL);
节点n4(4、n2和n5);
节点n9(9、n7和n10);
节点n6(6、n4和n9);
STD::CUT< P>我写了一个C++版本,似乎更明白了。
#include <stdexcept>
#include <string>
#include <iostream>
#include <algorithm>
class Node
{
public:
Node(int value, Node* left, Node* right)
{
this->value = value;
this->left = left;
this->right = right;
}
int getValue() const
{
return value;
}
Node* getLeft() const
{
return left;
}
Node* getRight() const
{
return right;
}
private:
int value;
Node* left;
Node* right;
};
class BinarySearchTree
{
public:
static bool isValidBST( Node& root)
{
if ((root.getLeft())&&(root.getValue()<=(root.getLeft()->getValue())))
return false;
if ((root.getRight())&&(root.getValue()>=(root.getRight()->getValue())))
{
return false;
}
if (root.getRight())
{
if(!isValidBST(*(root.getRight()))) return false;
}
if(root.getLeft())
{
if(!isValidBST(*(root.getLeft()))) return false;
}
return true;
}
};
#ifndef RunTests
int main(int argc, const char* argv[])
{
Node n7(7, NULL, NULL);
Node n10(11, NULL, NULL);
Node n2(2, NULL, NULL);
Node n5(5, NULL, NULL);
Node n4(4, &n2, &n5);
Node n9(9, &n7, &n10);
Node n6(6, &n4, &n9);
std::cout << BinarySearchTree::isValidBST(n6);
return 0;
}
#endif
#包括
#包括
#包括
#包括
类节点
{
公众:
节点(int值,节点*左,节点*右)
{
这个->值=值;
这个->左=左;
这个->右=右;
}
int getValue()常量
{
返回值;
}
节点*getLeft()常量
{
左转;
}
节点*getRight()常量
{
返还权;
}
私人:
int值;
节点*左;
节点*右;
};
类二进制搜索树
{
公众:
静态bool isvalidbtst(节点和根)
{
if((root.getLeft())&&(root.getValue()getValue()))
返回false;
如果((root.getRight())&&(&&(root.getValue()>=(root.getRight()->getValue()))
{
返回false;
}
if(root.getRight())
{
if(!isvalidbtst(*(root.getRight()))返回false;
}
if(root.getLeft())
{
如果(!isvalidbtst(*(root.getLeft()))返回false;
}
返回true;
}
};
#ifndef运行测试
int main(int argc,const char*argv[]
{
节点n7(7,NULL,NULL);
节点n10(11,NULL,NULL);
节点n2(2,NULL,NULL);
节点n5(5,NULL,NULL);
节点n4(4、n2和n5);
节点n9(9、n7和n10);
节点n6(6、n4和n9);
std::您是否需要说返回包含(…)
,而不仅仅是调用该方法。现在您正在丢弃递归调用的结果,并最终得到方法末尾的返回false;
。当然这就是原因…上帝,我觉得很愚蠢。非常感谢。您需要说返回包含(…)
,而不仅仅是调用该方法。现在,您正在丢弃递归调用的结果,并最终得到方法末尾的返回false;
。当然这就是原因……上帝,我觉得自己很愚蠢。非常感谢。解释您的答案解释您的答案