无法在二叉搜索树 c++ 中定位项目

问题描述 投票:0回答:0

当我运行它并尝试输入一个有效的 ID 时,它说没有找到任何一个,我确认正在找到并打开 .dat 文件,这是下面的 dat 文件。

Record# AccountID   FirstName   LastName    Balance
0   6274            James           Johnson         415.56
1   2843            Marcus          Wilson          9217.23
2   2892            Maureen         Albright        51462.56
3   8837            Debra           Douglas         27.26
4   1892            Mary            Smith           918.26
5   9523            Bruce           Gold            719.32
6   3165            John            Carlson         1496.24
7   3924            Simon           Becker          386.85
8   6023            John            Edgar           9.65
9   5290            George          Truman          16110.68
10  8529            Ellen           Fairchild       86.77
11  1144            Donald          Williams        4114.26
#include <iostream>
#include <fstream>
#include <string>

using namespace std;

struct Account
{
    int acctID;      // Account identifier
    string firstName;
    string lastName;
    double balance;
};

struct IndexEntry
{
    int acctID;      // (key) Account identifier
    long recNum;     // Record number
};

struct TreeNode {
    IndexEntry data;
    TreeNode* left;
    TreeNode* right;
};

// Function to insert a new node into the index tree
void insertNode(TreeNode*& root, IndexEntry data)
{
    IndexEntry record;
    record.acctID = data.acctID;
    record.recNum = data.recNum;
    if (root == nullptr) {
        root = new TreeNode;
        root->data = record;
        root->left = root->right = nullptr;
    }
    else if (data.acctID < root->data.acctID) {
        insertNode(root->left, record);
    }
    else if (data.acctID > root->data.acctID) {
        insertNode(root->right, record);
    }
}

// Function to search for a node with a given account ID in the index tree
TreeNode* searchNode(TreeNode* root, int acctID)
{
    if (root == nullptr || root->data.acctID == acctID) {
        return root;
    }
    else if (acctID < root->data.acctID) {
        return searchNode(root->left, acctID);
    }
    else {
        return searchNode(root->right, acctID);
    }
}

// Function to retrieve an account record from the database file
void getAccountRecord(ifstream& file, long recNum)
{
    file.seekg(recNum * sizeof(Account));
    Account record;
    file.read(reinterpret_cast<char*>(&record), sizeof(record));
    cout << record.acctID << "\t" << record.firstName << "\t" << record.lastName << "\t" << record.balance << endl;
}

int main()
{
    // Open the database file for reading
    ifstream file("accounts.dat", ios::binary);
    if (!file) {
        cerr << "Error opening database file!" << endl;
        return 1;
    }

    // Build the index tree by reading through the database file
    TreeNode* indexTree = nullptr;
    IndexEntry record;
    long recNum = 0;
    while (file.read(reinterpret_cast<char*>(&record), sizeof(record))) {
        record.recNum = recNum;
        insertNode(indexTree, record);
        recNum++;
    }

    // Search for account records based on account ID
    int acctID;
    cout << "Enter account ID to retrieve record: ";
    cin >> acctID;
    TreeNode* node = searchNode(indexTree, acctID);
    if (node == nullptr) {
        cout << "Account record not found!" << endl;
    }
    else {
        getAccountRecord(file, node->data.recNum);
    }

    // Clean up
    file.close();
    return 0;
}

我不确定是不是二叉树没有做对

c++ database binary-tree
© www.soinside.com 2019 - 2024. All rights reserved.