fanluping123
2015-12-10 02:56
采纳率: 100%
浏览 1.7k

二叉搜索树的遍历问题

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

我是在做一个二叉平衡树的实验,出现了一个问题,所以简化代码测试如上,但是运行不能通过,若是通过了,在连续两次输对用户名,输错密码之后,会有一个小错误,求解啊

4条回答 默认 最新

相关推荐 更多相似问题