在c ++中纠正二进制搜索树搜索功能的代码 [英] rectify the code of binary search tree search function in c++

查看:60
本文介绍了在c ++中纠正二进制搜索树搜索功能的代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

#include<iostream.h>
#include<conio.h>
#include<process.h>

struct tree_node
{
	tree_node *left;
	tree_node *right;
	int data;
} ;
class bst
{
	tree_node *root;
	public:
	bst()
	{
		root=NULL;
	}
	int isempty() 
	{
		return(root==NULL);
	}
	void insert(int item);
	bool search(tree_node * ,int); // 2 errors on this line
                                       // 1. Type name expected.
                                       // 2. Declaration missing ;   
};
bool search(tree_node* root,int data) { //error on this line(decl. syntax error)
	if(root == NULL) {
		return false;
	}
	else if(root->data == data) {
		return true;
	}
	else if(data <= root->data) {
		return Search(root->left,data);
	}
	else {
		return Search(root->right,data);
	}
}
void main()
{
	int digit;
	bst b;
        int number;
	cout<<"Enter number be searched\n";
	cin>>number;
	//If number is found, print "FOUND"
	if (b.search(root,number) == true) cout<<"Found\n";
	else cout<<"Not Found\n";
	getch();
}



此代码的搜索功能给出三个错误

1.预期输入名称。

2.声明丢失;

3.声明语法错误。

请纠正。


the search function of this code is giving three errors
1. Type name expected.
2. Declaration missing ;
3. Declaration syntax error.
please rectify.

推荐答案

1 。修复当您尝试构建该源文件时将发生的所有恼人的编译器错误



2.考虑搜索的接口会员功能。你确定它应该有那个界面吗?是公共会员吗?是否应该有比您指定的更多搜索版本?



3.实施插入



4.实际在主函数中插入一些数据,以便搜索它



5.如果你没有'知道问题出在哪里然后回来,有人可以提出更多建议。
1. Fix all the irritating compiler errors that'll occur when you try and build that source file

2. Consider the interface to the search member function. Are you sure it should have that interface? Should it be a public member? Should there be more versions of search than you've specified?

3. Implement insert

4. Actually insert some data in your main function so you can search for it

5. If you haven't realised where the problem is by then come back and someone can make some more suggestions.


这不是应该包括iostream.h的方式。您需要使用 #include< iostream> 。是的,这就是std库头的设计方式。



现在,这一行没有错误

This is not how "iostream.h" should be included. You need to use #include <iostream>. Yes, this is how std library headers are designed.

Now, there is no an error in this line:
bool search(tree_node * ,int); // 2 errors on this line.



通常,您的问题是报告问题而不是您尝试编译的代码。你不应该把这个部分放在带有入口点(main)的CPP文件中,它属于包含文件并且没有任何意义,然后实现将在你提供给编译的单独的CPP文件中。如果你想将它放在同一个文件中(因为其他文件没有使用这个类和结构),你应该更好地实现类定义中的所有函数。但是如果你单独进行,这个定义


Generally, your problem is reporting the problem not with the code you were trying to compile. You should not have put this part in the CPP file with the entry-point ("main"), it belongs to the include file and just makes no sense, and then the implementation would be in a separate CPP file you feed to the compilation. If you want to have it in the same file (because other files are not using this class and structure), you should better implement all functions inside the class definition. But if you do it separately, this definition

bool search(tree_node* root,int data) { /* ... */ }

定义类函数 search 。要编写这样的定义,您应该将其写为

would not define the class function search. To write such definition, you should write it as

bool bst::search(tree_node* root,int data) { /* ... */ }



结论:您需要学习一些非常基本的东西:C / C ++如何分离编译,以及声明类和他们成员的定义。



祝你好运,

-SA


这篇关于在c ++中纠正二进制搜索树搜索功能的代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆