2015-12-10 02:56

# 二叉搜索树的遍历问题

`````` #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;
}
}
}
}
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;
}
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(){