布尔函数在不应该是C ++时返回true

时间:2017-03-27 21:57:06

标签: c++ algorithm tree

我有一个程序,使用广度优先搜索和每种类型的遍历来搜索二叉搜索树。我的所有函数都工作并运行,但最后一次遍历,后序遍历,在搜索不在树中的数字时返回true。我调试了程序并观察它在后序遍历函数中贯穿整个树,它从来没有真正返回true,但仍然打印出真正的输出。谁能告诉我为什么?

#include <queue>
#include <iostream>
using namespace std;

struct Node
{
    int data;
    Node *left, *right;
}*root;

Node *createNode(int data)
{
    Node *Tnode = new Node;

    Tnode->left = NULL;

    Tnode->right = NULL;

    Tnode->data = data;

    return Tnode;
}

vbool breadthFirstSearch(Node *node, int item)
{
    queue<Node*> q;

    if(root)
    {
        q.push(root);
        while(!q.empty())
        {
            Node *d = q.front();
            q.pop();
            if(d->data == item)
                return true;
            if(d->left)
                q.push(d->left);
            if(d->right)
                q.push(d->right);

        }
    }
    return false;
}

bool inorderTraversal(Node *node, int item)
{
    if(node == NULL)
        return false;
    inorderTraversal(node->left, item);
    if(node->data == item)
        return true;
    inorderTraversal(node->right, item);
}

bool preorderTraversal(Node *node, int item)
{
    if(node == NULL)
        return false;
    if(node->data == item)
        return true;
    preorderTraversal(node->left, item);
    preorderTraversal(node->right, item);
}

bool postorderTraversal(Node *node, int item)
{
    if(node == NULL)
        return false;
    postorderTraversal(node->left, item);
    postorderTraversal(node->right, item);
    if(node->data == item)
        return true;
}

int main()
{
    root = createNode(20);
    root->left = createNode(10);
    root->left->left = createNode(5);
    root->left->right = createNode(15);
    root->right = createNode(30);
    root->right->left = createNode(25);
    root->right->right = createNode(35);

    cout<<"\n-----Breadth First Search Traversal-----\n";

    if(breadthFirstSearch(root, 35) == true)
    {
        cout<< "The item 35 is in the tree\n";
    }
    else
    {
        cout << "The item 35 is not in the tree\n";
    }
    if(breadthFirstSearch(root, 29) == true)
    {
        cout<< "The item 29 is in the tree\n";
    }
    else
    {
        cout << "The item 29 is not in the tree\n";
    }

    cout<<"\n-----Inorder Search Traversal-----\n";

    if(inorderTraversal(root, 35) == true)
    {
        cout<< "The item 35 is in the tree\n";
    }
    else
    {
        cout << "The item 35 is not in the tree\n";
    }
    if(inorderTraversal(root, 29) == true)
    {
        cout<< "The item 29 is in the tree\n";
    }
    else
    {
        cout << "The item 29 is not in the tree\n";
    }
    cout<<"\n-----Preorder Search Traversal-----\n";

    if(preorderTraversal(root, 35) == true)
    {
        cout<< "The item 35 is in the tree\n";
    }
    else
    {
        cout << "The item 35 is not in the tree\n";
    }
    if(preorderTraversal(root, 29) == true)
    {
        cout<< "The item 29 is in the tree\n";
    }
    else
    {
        cout << "The item 29 is not in the tree\n";
    }
    cout<<"\n-----Postorder Search Traversal-----\n";

    if(postorderTraversal(root, 35) == true)
    {
        cout<< "The item 35 is in the tree\n";
    }
    else
    {
        cout << "The item 35 is not in the tree\n";
    }
    if(postorderTraversal(root, 29) == true)
    {
        cout<< "The item 29 is in the tree\n";
    }
    else
    {
        cout << "The item 29 is not in the tree\n";
    }
    return 0;
}

每个其他遍历正确打印出35个在树中,29个不在树中。只有邮政订单打印29在树中,即使在检查29时永远不会返回true。我确定我错过了一些小而愚蠢的东西,但我似乎无法找到它。

3 个答案:

答案 0 :(得分:1)

如果节点为非null且其键不等于postorderTraversal,则item函数不会返回任何内容。这是未定义的行为。

答案 1 :(得分:0)

bool preorderTraversal(Node *node, int item)
{
    if(node == NULL)
        return false;
    if(node->data == item)
        return true;
    preorderTraversal(node->left, item);
    preorderTraversal(node->right, item);
} 

更改为

bool preorderTraversal(Node *node, int item)
{
    if(node == NULL)
        return false;
    if(node->data == item)
        return true;
    return preorderTraversal(node->left, item) || preorderTraversal(node->right, item);
} 

还有其他功能。

答案 2 :(得分:0)

编写应该返回值但不调用未定义行为的函数。

最简单的方法是在这些函数的末尾添加cout行,看看是否有任何执行:

bool inorderTraversal(Node *node, int item)
{
    if (node == NULL)
        return false;
    inorderTraversal(node->left, item);
    if (node->data == item)
        return true;
    inorderTraversal(node->right, item);
    cout << "OOPS1" << "\n";  // error if this line is executed
}

bool preorderTraversal(Node *node, int item)
{
    if (node == NULL)
        return false;
    if (node->data == item)
        return true;
    preorderTraversal(node->left, item);
    preorderTraversal(node->right, item);
    cout << "OOPS2" << "\n";  // error if this line is executed
}

bool postorderTraversal(Node *node, int item)
{
    if (node == NULL)
        return false;
    postorderTraversal(node->left, item);
    postorderTraversal(node->right, item);
    if (node->data == item)
        return true;
    cout << "OOPS3" << "\n";   // error if this line is executed
}

Live Example of compiler warnings and running of the program

您将看到“OOPS”行全部被执行,从而表明您已经完成了函数的结束而没有返回值。

因此,你需要回过头来重新思考你的功能逻辑。其他答案表明你应该做出哪些改变。