C++ 如何检查树是否为BST?
我必须检查一棵树是否是二叉搜索树。我使用一个收集值的临时数组进行顺序遍历。我必须检查数组是否为升序,如果是,则返回true:C++ 如何检查树是否为BST?,c++,binary-tree,binary-search-tree,C++,Binary Tree,Binary Search Tree,我必须检查一棵树是否是二叉搜索树。我使用一个收集值的临时数组进行顺序遍历。我必须检查数组是否为升序,如果是,则返回true: boolmyisbst(Node*Node,std::vector v); bool myisBST(节点*节点) { 返回myisBST(节点,std::vector()); } bool myisBST(节点*节点,标准::向量v) { 如果(节点) { 如果(节点->左) 返回myisBST(节点->左,v); v、 推回(节点->数据); 如果(节点->右侧) 返
boolmyisbst(Node*Node,std::vector v);
bool myisBST(节点*节点)
{
返回myisBST(节点,std::vector());
}
bool myisBST(节点*节点,标准::向量v)
{
如果(节点)
{
如果(节点->左)
返回myisBST(节点->左,v);
v、 推回(节点->数据);
如果(节点->右侧)
返回myisBST(节点->右,v);
}
return std::已排序(v.begin(),v.end());
}
当二叉树是:
50
/ \
25 75
/ \ / \
1 12 62 -99
如您所见,-99
使这不是一个二进制搜索树,但它仍然返回true。我的实现有什么问题吗
两个问题:
myisBST
中,您通过值传递v
,而不是通过引用,因此当您递归传递向量时,对它所做的更改不会改变调用方法中的值。只需将函数签名更改为boolmyisbst(Node*Node,std::vector&v)
即可解决此问题returnmyisbst(node->left,v)提前返回
和返回myisBST(节点->右侧,v)代码>。实际上,您对这些方法的返回值并不感兴趣;你只是用它们来按顺序填充向量。从这两行中删除返回
myisBST
中,您通过值传递v
,而不是通过引用,因此当您递归传递向量时,对它所做的更改不会改变调用方法中的值。只需将函数签名更改为boolmyisbst(Node*Node,std::vector&v)
即可解决此问题returnmyisbst(node->left,v)提前返回
和返回myisBST(节点->右侧,v)代码>。实际上,您对这些方法的返回值并不感兴趣;你只是用它们来按顺序填充向量。从这两行中删除返回
在这两个修复之后,您的方法可以工作。首先,您可能应该通过引用传递
向量,否则每个递归调用都会得到一个副本,因此原始向量可能为空
其次,您甚至不需要先创建向量,然后再进行检查,您只需检查每个节点的BST属性,即根必须大于左子节点,小于右子节点,例如
bool isBST(const Node* root, vector<int>* v) {
if (!root) { return true; }
bool leftBST = true;
if (root->left) {
if (root->data > root->left->data) {
leftBST = isBST(root->left, v);
} else {
// the current node violates the BST precondition
return false;
}
}
// push the root
v->push_back(root->data);
// return false if left subtree is not a BST
if (!leftBST) return false;
if (root->right) {
if (root->data < root->right->data) {
// return whether or not the right subtree is a BST
return isBST(root->left, v);
} else {
// the current node violates the BST precondition
return false;
}
}
// everything good, this is a BST
return true;
}
bool isBST(常量节点*根,向量*v){
如果(!root){return true;}
bool leftBST=true;
如果(根->左){
如果(根->数据>根->左->数据){
leftBST=isBST(根->左,v);
}否则{
//当前节点违反BST前提条件
返回false;
}
}
//推根
v->push_back(根->数据);
//如果左子树不是BST,则返回false
如果(!leftBST)返回false;
如果(根->右){
如果(根->数据<根->右->数据){
//返回是否正确的子树是BST
返回isBST(根->左,v);
}否则{
//当前节点违反BST前提条件
返回false;
}
}
//一切都好,这是BST
返回true;
}
首先,您可能应该通过引用传递向量
,否则每次递归调用都会得到一个副本,因此原始向量可能为空
其次,您甚至不需要先创建向量,然后再进行检查,您只需检查每个节点的BST属性,即根必须大于左子节点,小于右子节点,例如
bool isBST(const Node* root, vector<int>* v) {
if (!root) { return true; }
bool leftBST = true;
if (root->left) {
if (root->data > root->left->data) {
leftBST = isBST(root->left, v);
} else {
// the current node violates the BST precondition
return false;
}
}
// push the root
v->push_back(root->data);
// return false if left subtree is not a BST
if (!leftBST) return false;
if (root->right) {
if (root->data < root->right->data) {
// return whether or not the right subtree is a BST
return isBST(root->left, v);
} else {
// the current node violates the BST precondition
return false;
}
}
// everything good, this is a BST
return true;
}
bool isBST(常量节点*根,向量*v){
如果(!root){return true;}
bool leftBST=true;
如果(根->左){
如果(根->数据>根->左->数据){
leftBST=isBST(根->左,v);
}否则{
//当前节点违反BST前提条件
返回false;
}
}
//推根
v->push_back(根->数据);
//如果左子树不是BST,则返回false
如果(!leftBST)返回false;
如果(根->右){
如果(根->数据<根->右->数据){
//返回是否正确的子树是BST
返回isBST(根->左,v);
}否则{
//当前节点违反BST前提条件
返回false;
}
}
//一切都好,这是BST
返回true;
}
检查树是否为BST的C++程序
struct Node
{
int data;
struct Node* left, *right;
};
bool IsBST(Node* ObjNode)
{
bool leftBST = false;
bool rightBST = false;
if( ObjNode->left != null && ObjNode-left < objNode->data)
{
leftBST = IsBST(ObjNode->left)
}
else if( ObjNode->left == null)
{
leftBST = true;
}
else if( ObjNode->left != null && ObjNode-left >= objNode->data)
{
leftBST = false;
}
if( ObjNode->left != null && ObjNode-left < objNode->data)
{
rightBST = IsBST(ObjNode->right)
}
else if( ObjNode->right == null)
{
rightBST = true;
}
else if( ObjNode->right != null && ObjNode-right >= objNode->data)
{
rightBST = false;
}
return (leftBST && rightBST );
}
struct节点
{
int数据;
结构节点*左、*右;
};
bool-IsBST(节点*ObjNode)
{
bool leftBST=false;
bool-rightBST=false;
if(ObjNode->left!=null&&ObjNode leftdata)
{
leftBST=IsBST(对象节点->左)
}
else if(ObjNode->left==null)
{
leftBST=true;
}
else if(ObjNode->left!=null&&ObjNode left>=ObjNode->data)
{
leftBST=false;
}
if(ObjNode->left!=null&&ObjNode leftdata)
{
rightBST=IsBST(对象节点->右侧)
}
else if(ObjNode->right==null)
{
rightBST=true;
}
else if(ObjNode->right!=null&&ObjNode right>=ObjNode->data)
{
rightBST=假;
}
返回(左BST和右BST);
}
检查树是否为BST的C++程序
struct Node
{
int data;
struct Node* left, *right;
};
bool IsBST(Node* ObjNode)
{
bool leftBST = false;
bool rightBST = false;
if( ObjNode->left != null && ObjNode-left < objNode->data)
{
leftBST = IsBST(ObjNode->left)
}
else if( ObjNode->left == null)
{
leftBST = true;
}
else if( ObjNode->left != null && ObjNode-left >= objNode->data)
{
leftBST = false;
}
if( ObjNode->left != null && ObjNode-left < objNode->data)
{
rightBST = IsBST(ObjNode->right)
}
else if( ObjNode->right == null)
{
rightBST = true;
}
else if( ObjNode->right != null && ObjNode-right >= objNode->data)
{
rightBST = false;
}
return (leftBST && rightBST );
}
struct节点
{
int数据;
结构节点*左、*右;
};
bool-IsBST(节点*ObjNode)
{
bool leftBST=false;
bool-rightBST=false;
if(ObjNode->left!=null