在BST中搜索价值代码

时间:2015-10-29 17:11:26

标签: c++ binary-search-tree

所以我需要调用contain()中的main函数。然后递归调用containsValue()函数以查看传入的值是否在BST中。当我搜索根值时,我得到一个返回值为true,但是,我在搜索的任何其他值上都是false。任何帮助都会很棒。感谢

BST.H

#include "stdafx.h"
#include <iostream>
#include <cstddef>
#include <string>
using namespace std;


#ifndef BST_H_
#define BST_H_

template <class bstdata>
class BST
{
private:
    struct Node
    {
        bstdata data;
        Node* left;
        Node* right;

        Node() : left(NULL), right(NULL){}
        Node(bstdata newdata) : left(NULL), right(NULL), data(newdata){}
    };

    typedef struct Node* Nodeptr;

    Nodeptr root;
    int size;

    /** Private Helper Functions **/

    void addValue(Nodeptr root, bstdata value);
    void printInOrder(Nodeptr root);
    void deleteTree(Nodeptr root);
    bool containsValue(Nodeptr root, bstdata);

public:
    BST();
    ~BST();
    bool isEmpty();
    int getSize();
    void add(bstdata value);
    bstdata getRoot();
    void inOrderPrint();
    void preOrderPrint();
    void postOrderPrint();
    bool contains(bstdata value);

};


/**Private helper functions*/

template <class bstdata>
void BST<bstdata>::addValue(Nodeptr root, bstdata value)
{
    if (value == root->data)
        return;

    if (value < root->data)
    {
        if (root->left == NULL)
        {
            root->left = new Node(value);
            size++;
        }
        else //(root->left != NULL);
            addValue(root->left, value);
    }

    if(value > root->data)
    {
        if (root->right == NULL)
        {
            root->right = new Node(value);
            size++;
        }
        else
            addValue(root->right, value);
    }
}

template <class bstdata>
void BST<bstdata>::printInOrder(Nodeptr root)
{
    if (root != NULL)
    {
        printInOrder(root->left);
        cout << root->data << " ";
        printInOrder(root->right);
    }
}

template <class bstdata>
void BST<bstdata>::deleteTree(Nodeptr root)
{
    if (root != NULL)
    {
        deleteTree(root->left);
        deleteTree(root->right);
        delete root;
    }

}

template<class bstdata>
bool BST<bstdata>::containsValue(Nodeptr root, bstdata value)
{
    if (value == root->data)
        return true;

    if (value < root->data)
    {
        if (root->left == NULL)
            return false;
        else // (root->left !=NULL)
            containsValue(root->left, value);
    }

    if (value > root->data)
    {
        if (root->right == NULL)
            return false;
        else //root->right !=NULL
            containsValue(root->right, value);
    }

    return  false;
}


/**Public functions*/

template <class bstdata>
BST<bstdata>::BST() : size(0), root(NULL){};

template <class bstdata>
void BST<bstdata>::add(bstdata value)
{
    if (root == NULL)
    {
        root = new Node(value);
        size++;
    }
    else
        addValue(root, value);
}


template <class bstdata>
bstdata BST<bstdata>::getRoot()
{
    if (size == 0)
        cout << "getRoot: there is no root in the BST" << endl;
    else
        return root->data;

}

template <class bstdata>
int BST<bstdata>::getSize()
{
    if (size == 0)
        cout << "getSize(): There is nothing in the tree, size = 0" << endl;
    else
        return size;
}

template <class bstdata>
bool BST<bstdata>::isEmpty()
{
    if (size == 0)
        return true;
    else
        return false;
}


template <class bstdata>
void BST<bstdata>::inOrderPrint()
{

    if (size == 0)
        cout << isEmpty() << endl;
    if (root != NULL)
    {
        printInOrder(root->left);
        cout << getRoot() << " ";
        printInOrder(root->right);
    }

}

template <class bstdata>
void BST<bstdata>::preOrderPrint()
{
    if (size == 0)
        cout << isEmpty() << endl;
    if (root != NULL)
    {
        cout << getRoot() << " ";
        printInOrder(root->left);
        printInOrder(root -> right);
    }
}

template <class bstdata>
void BST<bstdata>::postOrderPrint()
{
    if (size == 0)
        cout << isEmpty() << endl;
    if (root != NULL)
    {
        printInOrder(root->left);
        printInOrder(root->right);
        cout << getRoot() << " ";
    }
}

template <class bstdata>
BST<bstdata>::~BST()
{
    deleteTree(root);
}

template<class bstdata>
bool BST<bstdata>::contains(bstdata value)
{
    if (value == root->data)
        return true;
    else
        return containsValue(root, value);
}

#endif

BSTTEST.cpp

#include "stdafx.h"
#include "BST.h"
#include <iostream>
#include <cstddef>
#include <string>
using namespace std;


int main()
{

    BST<int> B;

    B.getSize();
    cout << B.isEmpty() << endl;

    B.add(7);
    B.add(1);
    B.add(5);
    B.add(15);
    B.add(10);
    B.add(11);

    cout << B.isEmpty() << endl;
    cout << "Root of tree: " << B.getRoot() << endl;
    cout << B.getSize() << endl;

    B.inOrderPrint();
    cout << endl << endl;
    B.preOrderPrint();
    cout << endl << endl;
    B.postOrderPrint();
    cout << endl << endl;
    cout << B.contains(1) << endl;
    cout << B.contains(7) << endl;
    cout << B.contains(78) << endl;
    cout << B.contains(11) << endl;



    return 0;
}

3 个答案:

答案 0 :(得分:1)

您忘了返回结果。当您的containsValue()函数迭代地调用自身时,它将找到返回true的值或到达BST的底部,返回false。一旦找到目标值,它就会将真实结果返回给调用它的函数,该函数本身就是ONLY ONCE。结果不存储也不返回。因此,它无法将最终结果返回到第一次调用。你需要添加&#39; return&#39;迭代地调用函数时。我已经添加了两个&#39; return&#39;你错过了。

template<class bstdata>
bool BST<bstdata>::containsValue(Nodeptr root, bstdata value)
{
    if (value == root->data)
        return true;

    if (value < root->data)
    {
        if (root->left == NULL)
            return false;
        else // (root->left !=NULL)
            return containsValue(root->left, value);
    }

    if (value > root->data)
    {
        if (root->right == NULL)
            return false;
        else //root->right !=NULL
            return containsValue(root->right, value);
    }

    return  false;
}

答案 1 :(得分:0)

将您的代码分段为if-else if-else块,如下所示:

template<class bstdata>
bool BST<bstdata>::containsValue(Nodeptr root, bstdata value)
{
    if (value == root->data)
        return true;

    else if (value < root->data)
    {
        if (root->left == NULL)
            return false;
        else // (root->left !=NULL)
            containsValue(root->left, value);
    }

    else if (value > root->data)
    {
        if (root->right == NULL)
            return false;
        else //root->right !=NULL
            containsValue(root->right, value);
    }

    else return false;
}

答案 2 :(得分:0)

您的contains似乎太复杂了。我会尝试这样的事情

template<class bstdata>
bool BST<bstdata>::containsValue(Nodeptr node, bstdata value)
{
    if (node == nullptr)
        return false;
    if (value == node->data)
        return true;

    if (value < node->data)
        return containsValue(node->left, value);
    else
        return containsValue(node->right, value);
}

template<class bstdata>
bool BST<bstdata>::contains(bstdata value)
{
    return containsValue(root, value);
}

更有效的迭代版本:

template<class bstdata>
bool BST<bstdata>::containsValue(Nodeptr node, bstdata value)
{
    while (node)
    {
        if (value == node->data)
            return true;

        node = value < node->data ? node->left : node->right;
    }
    return false;
}