Function doesn't return default value

时间:2018-09-18 08:48:28

标签: c++ recursion binary-tree

Here is my code:

bool BinarySearchTree::CheckIfTreeIsBinary(){
    bool isBinary=true;
    isBinary=CheckIfTreeIsBinaryPrivate(root); // So if my tree is binary, this function does not return anything
                                               // and isBinary should remain true, but it is false.
    return isBinary;
}

bool BinarySearchTree::CheckIfTreeIsBinaryPrivate(nodePtr Ptr){ 
    if(Ptr->left!=NULL){
        CheckIfTreeIsBinaryPrivate(Ptr->left);
    }

    if(Ptr->left!=NULL){
        if(Ptr->data<Ptr->left->data)
            return false; // possibility 1 to return false
    }

    if(Ptr->right!=NULL){
        if(Ptr->data>Ptr->right->data)
            return false; // possibility 2 to return false
    }

    if(Ptr->right!=NULL){
        CheckIfTreeIsBinaryPrivate(Ptr->right);
    }
}

In my function CheckIfTreeIsBinary, I have set boolean isBinary to true for default value. After that, isBinary is assigned to function CheckIfTreeIsBinaryPrivate, which will not return anything if the tree is binary.
The problem is that function CheckIfTreeIsBinaryPrivate doesn't return anything if the tree is binary, but in the end, isBinary is false.

4 个答案:

答案 0 :(得分:2)

问题是CheckIfTreeIsBinaryPrivate在所有程序控制路径上都没有没有具有明确的return值。

这意味着程序的行为是未定义

您的编译器会警告您,注意这些警告是您的工作。

答案 1 :(得分:1)

您的递归逻辑不正确。函数中的所有路径都应返回一个值,并且您应始终检查对CheckIfTreeIsBinaryPrivate的递归调用的返回值。没有“保持不变的价值”的概念。这是我认为您要尝试实现的目标,但这非常复杂。

bool BinarySearchTree::CheckIfTreeIsBinaryPrivate(nodePtr Ptr) { 
    return
        // check the left sub tree is ok
        (Ptr->left == NULL ||                 // NULL is ok OR
            (Ptr->data >= Ptr->left->data &&  // data >= left->data && left is ok
                 CheckIfTreeIsBinaryPrivate(Ptr->left))) &&
        // and check the right sub tree is ok
        (Ptr->right == NULL ||                // NULL is ok OR
            (Ptr->data <= Ptr->right->data && // data <= right->data && right is ok
                 CheckIfTreeIsBinaryPrivate(Ptr->right)));
}

答案 2 :(得分:0)

CheckIfTreeIsBinaryPrivate()中再添加一个基本条件以将其设置为true, 因为一旦将isBinary分配给CheckIfTreeIsBinaryPrivate(),默认情况下它将设置为false,因此需要一个返回值才能成为true。

答案 3 :(得分:0)

我想我明白你的误解在哪里;您期望isBinary仅在CheckIfTreeIsBinaryPrivate返回一个值时才被更新。

这不是它的工作方式-具有非void返回类型的函数必须始终返回某物
如果该函数未明确返回任何内容(例如,通过到达该函数的末尾),则该程序具有未定义的行为。

您无法确定某个函数是否返回任何东西,您必须在该函数的每条路径上返回一些东西。

您可以用一个大表情来做到这一点,

bool BinarySearchTree::isBST(nodePtr Ptr){
    return Ptr == nullptr
        || (   (Ptr->left == nullptr || (Ptr->left->data < Ptr->data && isBST(Ptr->left)))
            && (Ptr->right == nullptr || (Ptr->right->data > Ptr->data && isBST(Ptr->right))));
}

或逐段:

bool BinarySearchTree::isBST(nodePtr Ptr){
    // An empty tree is a BST.
    if (Ptr == nullptr)
        return true;
    // If the left subtree is not a BST, neither is the entire tree.
    else if (Ptr->left != nullptr && (Ptr->left->data > Ptr->data || !isBST(Ptr->left)))
        return false;
    // If the right subtree is not a BST, neither is the entire tree.
    else if (Ptr->right != nullptr && (Ptr->right->data < Ptr->data || !isBST(Ptr->right)))
        return false;
    // All tests passed.
    else
        return true;
}