问题描述
- 二叉搜索树的遍历问题
-
#include<iostream> #include<string> using namespace std; class node{ public: string name; string keyword; node* left; node* right; node(string a = 0, string b = 0, node* c = 0, node* d = 0) : name(a), keyword(b), left(c), right(d){} }; void search(string name2, string keyword2, bool& nameb, bool& keywordb,node* root){ node* p = root; nameb = keywordb = false; while (p){ if (name2 < p->name) p = p->left; else if (name2>p->name) p = p->right; else { nameb = true; if (keyword2 == p->keyword){ keywordb = true; return; } else{ keywordb = false; return; } } } } void login(){ node a; a.name = "f"; a.keyword = "f"; a.left = new node("c", "c"); a.left->left = new node("123", "123"); a.right = new node("one", "one"); a.right->right = new node("wang", "@@110"); node* root = &a; cout << "请输入要登录的用户名和密码" << endl; string c, d; cin >> c >> d; bool nameb, keywordb;//判断用户名和密码正确与否 search(c, d, nameb, keywordb,root); if (nameb&&keywordb)cout << "登录成功!" << endl; else if (nameb == true && keywordb == false){ while (keywordb == false){ cout << "您输入的密码有误,请重新输入密码" << endl; cin >> d; search(c,d, nameb, keywordb,root); if (keywordb == true){ cout << "登录成功!" << endl; return; } } } else { cout << "您输入的用户名不存在!请重新输入用户名和密码!" << endl; login(); } delete a.left->left; a.left->left = 0; delete a.left; a.left = 0; delete a.right->right; a.right->right = 0; delete a.right; a.right = 0; } int main(){ login(); }
我是在做一个二叉平衡树的实验,出现了一个问题,所以简化代码测试如上,但是运行不能通过,若是通过了,在连续两次输对用户名,输错密码之后,会有一个小错误,求解啊
解决方案
若是通过了,在连续两次输对用户名,输错密码之后,会连续两次cout << "您输入的用户名不存在!请重新输入用户名和密码!" << endl;
解决方案二:
Description
给定一组无序整数,以第一个元素为根节点,生成一棵二叉搜索树,对其进行中序遍历和先序遍历。
Input
输入包括多组数据,每组数据包含两行:第一行为整数m(1
Output
每组输入产生两行输出,第一行是中序遍历结果,第二行是先序遍历结果,每个整数后面带一个空格,每行中第一个整数前无空格。
Sample Input
Copy sampl......
答案就在这里:二叉搜索树的遍历
----------------------
解决方案三:
具体是什么错误啊,程序段错误还是逻辑问题啊?
解决方案四:
使用了无效的空指针,下面稍改了下看看是否可以。
另外,编码的格式太不严谨,建议使用指针之前要判空,内存申请后要记得释放。
下面是我写的二叉查找树代码,也许对你有用。
http://www.cnblogs.com/libin2015/articles/5010875.html
void search(string name2, string keyword2, bool& nameb, bool& keywordb,node* root){
node* p = root;
nameb = keywordb = false;
while (p){
if (name2 < p->name)
p = p->left;
else if (name2>p->name)
p = p->right;
else {
nameb = true;
if (keyword2 == p->keyword){
keywordb = true;
return;
}
else{
keywordb = false;
return;
}
}
}
return; //add
}
void login(){
node a;
a.name = "f";
a.keyword = "f";
a.left = new node("c", "c");
a.left->left = new node("123", "123");
a.right = new node("one", "one");
a.right->right = new node("wang", "@@110");
node* root = &a;
cout << "请输入要登录的用户名和密码" << endl;
string c, d;
cin >> c >> d;
bool nameb, keywordb;//判断用户名和密码正确与否
search(c, d, nameb, keywordb,root);
if (nameb&&keywordb)
{
cout << "登录成功!" << endl;
return; //add
}
else if (nameb == true && keywordb == false){
while (keywordb == false){
cout << "您输入的密码有误,请重新输入密码" << endl;
cin >> d;
search(c,d, nameb, keywordb,root);
if (keywordb == true){
cout << "登录成功!" << endl;
return;
}
}
}
else {
cout << "您输入的用户名不存在!请重新输入用户名和密码!" << endl;
//add
delete a.left->left;
a.left->left = 0;
delete a.left;
a.left = 0;
delete a.right->right;
a.right->right = 0;
delete a.right;
a.right = 0;
//add
login();
}
delete a.left->left;
a.left->left = 0;
delete a.left;
a.left = 0;
delete a.right->right;
a.right->right = 0;
delete a.right;
a.right = 0;
}
int main(){
login();
return 0;
}
时间: 2024-12-23 04:32:56