c++ - 在二叉树中搜索

标签 c++ search binary-search-tree

我编写了以下函数来在存储整数值的二叉树中搜索值(该函数是一个更大程序的一部分):

bool tree::search(int num)       //the function belongs to class 'tree'
{
   node *temp=head;      //'head' is pointer to root node

   while(temp!=NULL)
   {
      if(temp->data==num)
         break;

      if(num>temp->data)
         temp=temp->right;

      if(num<temp->data)
         temp=temp->left;
   }

   if(temp==NULL)
      return false;
   else if(temp->data==num)
         return true;   
}    

问题是:当我搜索树中存在的值时,它运行正常。但是如果我搜索树中不存在的值,程序就会挂起,我必须关闭它。 还有一件事——我知道我们可以通过将 node *temp 作为参数传递而不是在内部声明它来递归地实现搜索功能,我已经这样做了,这导致程序正确运行,但我想知道问题是什么在上面的代码中。

我在这里给出了完整的程序,以防万一它更容易发现错误(请注意我只写了两个函数):

#include<iostream>
using namespace std;

struct node
{
int data;
node *left;
node *right;
};

class tree
{
public:
    node *head;    //pointer to root
    int count;     //stores number of elements in tree
    tree();
    void addnode(int);
    void deletenode(int);
    bool search(int);
    int minimum();
    int maximum();
    void inorder();
    void preorder();
    void postorder();
    void printtree();
    int mthlargest();     //finds 'm'th largest element
    int mthsmallest();    //finds 'm'th smallest element
    void convert();       //converts binary tree to linked list
};

tree::tree()
{
   head=NULL;
   count =0;
}

void tree::addnode(int num)
{
   node *temp= new node;
   temp->data=num;
   temp->left=NULL;
   temp->right=NULL;

   node **ptr=&head;          //double pointer

   while(*ptr!=NULL)
   {
      if(num>(*ptr)->data)
         ptr=&((*ptr)->right);

      if(num<(*ptr)->data)
         ptr=&((*ptr)->left);
   }

   *ptr=temp;
}


bool tree::search(int num)
{
   node *temp=head;

   while(temp!=NULL)
   {
      if(temp->data==num)
         break;

      if(num>temp->data)
         temp=temp->right;

      if(num<temp->data)
         temp=temp->left;
   }

   if(temp==NULL)
      return false;
   else if(temp->data==num)
      return true;   
}    




int main()
{
   tree ob;
   ob.addnode(2);

   ob.search(2);

   ob.search(3);

   ob.search(-1);
   ob.search(2);
   cout<<endl<<endl;

   system("pause");
   return 0;
}               

旁注:我使用的是 Dev C++ 编译器和 Windows 7 操作系统。

最佳答案

输入一个else,您的问题就会消失。

因为在 temp = temp->right; 之后你必须再次检查 temp 但在你的原始代码中你会立即测试 temp->data这可能不是有效的指针。

bool tree::search(int num)
{
    node *temp = head;

    while (temp != NULL)
    {
        if (temp->data == num)
            break;

        if (num > temp->data)
            temp = temp->right;
        else                  //  <--- Put this 'else' here
        if (num < temp->data)
            temp = temp->left;
    }

    if (temp == NULL)
        return false;

    if (temp->data == num)
        return true;

    return false;
}

关于c++ - 在二叉树中搜索,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14302754/

相关文章:

c - 如何修复未打印树中节点的错误

c++ - 二叉搜索树C++实现(动态内存问题)

c++ - QStandardItemModel::findItems 不在 QModelIndex(row, col, QModel(row,col)) 中搜索

c++ - RcppArmadillo 中稀疏和密集矩阵的模板函数

java - 如何用Java编写惰性删除二叉搜索树的findMinimum

ruby-on-rails - 用太阳黑子重新索引一个特定的记录

c++ - 按相对距离查找点的子集

c++ - double 值的最大长度?

c++ - 需要在 C++ 中使用 char** 而不是 std::string*

ruby-on-rails - Ransack::Search<class: 错误的未定义方法 `name_cont'?