簡體   English   中英

函數不返回默認值

[英]Function doesn't return default value

這是我的代碼:

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

在我的函數CheckIfTreeIsBinary ,我已將 boolean isBinary設置為 true 作為默認值。 之后,將 isBinary 分配給函數 CheckIfTreeIsBinaryPrivate,如果樹是二叉樹,它將不會返回任何內容。
問題是如果樹是二叉樹,函數 CheckIfTreeIsBinaryPrivate 不會返回任何東西,但最終 isBinary 是假的。

問題是CheckIfTreeIsBinaryPrivate在所有程序控制路徑上都沒有顯式return值。

這意味着您的程序的行為是undefined

你的編譯器會警告你這一點,你的工作是注意這些警告。

您的遞歸邏輯不正確。 函數中的所有路徑都應該返回一個值,並且您應該始終檢查遞歸調用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)));
}

CheckIfTreeIsBinaryPrivate()再添加一個基本條件以設置為 true,因為一旦將isBinary分配給CheckIfTreeIsBinaryPrivate()它將默認設置為 false,並且您需要一個返回值來獲取 true。

我想我明白你的誤解在哪里了; 您期望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;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM