编辑二叉树结构中的节点

Editing a Node in a binary tree structure

) 我正在编写代码...快完成了,但我仍然坚持最后一件事, 我需要找到一种方法来在用户希望时编辑客户的信息。 这是我的代码...任何人都可以告诉我有什么问题吗? :

        #include <iostream>
#include <string>
#include <fstream>
using namespace std;
ifstream infile;
ofstream outfile;
struct INFO {
    int id;
    string name;
    unsigned balance;
    long phone;
};
class Node
{
public:
    INFO data;
    Node *left;
    Node *right;
    Node() {
        data.id = 0;
        data.name = "NULL";
        data.phone = 0;
        data.balance = 0;
    }
    Node(INFO a)
    {
        data = a;
        left = 0;
        right = 0;
    }
};
class Tree
{

public:
    Node *root;
    INFO inf;
    Node *zero;
    Tree()
    {
        root = 0;
    }
    bool insert(INFO inf)
    {
        if (root == 0)
        {
            root = new Node(inf);
            return true;
        }
        Node *p = root;
        Node *q = root;

        while (p != 0)
        {
            q = p;
            if (p->data.id == inf.id)
                return false;
            if (p->data.id > inf.id)
                p = p->left;
            else
                p = p->right;
        }
        if (inf.id < q->data.id)
            q->left = new Node(inf);
        else
            q->right = new Node(inf);
        return true;
    }
    bool searchid(Node *p, int y)
    {
        if (p != 0) {
            if (p->data.id == y) {
                cout << "ID: ";
                cout << p->data.id << "\t";
                cout << "Name: ";
                cout << p->data.name << "\t";
                cout << "Balance: ";
                cout << p->data.balance << "\t";
                cout << "Phone number: ";
                cout << p->data.phone << endl;
                cout << "_________________" << endl;
                return true;
            }
        }
        if (p->left != 0) {
            if (searchid(p->left, y)) {
                return true;
            }
        }
        if (p->right != 0) {
            if (searchid(p->right, y)) {
                return true;
            }
        }

        return false;
    }
    Node SAD(int id) {
        Node *p = root;
        if (p->data.id == id) {
            Node *q = p;
            return *p;
        }
        if (p->left != 0)
            SAD(p->left->data.id);
        if (p->right != 0)
            SAD(p->right->data.id);
        return *zero;
    }
    void edit(int q) {
        Node p = SAD(q);
        cout << "The ID is: " << p.data.id << endl;
        cout << "The account owner: ";
        cout << p.data.name << endl;
        cout << "Do you want to edit the owner?(Y/N) ";
        char x;
        cin >> x;
        if (x == 'Y' || x == 'y') {
            string New;
            cout << "Enter the new owner name: ";
            cin >> New;
            p.data.name = New;
        }
        cout << "The Balance in the account is: ";
        cout << p.data.balance << endl;
        cout << "Do you want to edit the balance?(Y/N) ";
        cin >> x;
        if (x == 'Y' || x == 'y') {
            int New;
            cout << "Enter the new Balance: ";
            cin >> New;
            p.data.balance = New;
        }
        cout << "The phone number is: ";
        cout << p.data.phone << endl;
        cout << "Do you want to edit the phone number?(Y/N) ";
        cin >> x;
        if (x == 'Y' || x == 'y') {
            long New;
            cout << "Enter the new phone number";
            cin >> New;
            p.data.phone = New;
        }
        cout << p.data.id << "   " << p.data.name << "   " << p.data.balance << "    " << p.data.phone << endl;
        insert(p.data);
    }
    void print(Node *p)
    {

        if (p != 0) {
            cout << "ID: ";
            cout << p->data.id << "\t";
            cout << "Name: ";
            cout << p->data.name << "\t";
            cout << "Balance: ";
            cout << p->data.balance << "\t";
            cout << "Phone number: ";
            cout << p->data.phone << endl;
            cout << "_______________________________________________________________" << endl<<endl;
        }
        if (p->left != 0)
            print(p->left);
        if (p->right != 0)
            print(p->right);
    }
    void store(Node *p)
    {
        if (p != 0) {
            outfile << "ID: ";
            outfile << p->data.id << "   ";
            outfile << "Name: ";
            outfile << p->data.name << "   ";
            outfile << "Balance: ";
            outfile << p->data.balance << "   ";
            outfile << "Phone number: ";
            outfile << p->data.phone << endl;
            outfile << "_______________________________________________________________" << endl;
        }
        if (p->left != 0)
            store(p->left);
        if (p->right != 0)
            store(p->right);
    }
    bool searchname(Node *p, string x)
    {
        Node *q = root;
        q = p;
        while (p != 0) {
            if (p->data.name == x) {
                cout << "ID: " << p->data.id << "\t";
                cout << "Name: " << p->data.name << "\t";
                cout << "Balance: " << p->data.balance << "\t";
                cout << "Phone number: " << p->data.phone << endl;
            }
            else {

            }
        }
    }
};
void main()
{
    outfile.open("clients.txt");
    int opt;
    Tree t;
    int m = 1;
    while (m != 0) {
        cout << "Choose an option:" << endl << "1- To Add new clients." << endl << "2- To Display the clients." << endl << "3- To Store the clients in a Text Document." << endl << "4- To Search for a specific client through it's ID." << endl << "5- To Edit a specific client's information" << endl << "6- To Delete a specific client." <<endl<<"7- Exit."<< endl;
        cin >> opt;
        switch (opt) {

        case 1:
            int n;
            cout << "Enter the amount of clients: ";
            cin >> n;
            INFO *arr;
            arr = new INFO[n];
            cout << "Enter the elements of the array: " << endl;
            for (int i = 0; i < n; i++)
            {
                cout << "Client #" << i + 1 << endl << "-----------" << endl;
                cout << "Enter the ID: ";
                cin >> arr[i].id;
                cout << "Enter the name of the client: ";
                cin >> arr[i].name;
                cout << "Enter the balance: ";
                cin >> arr[i].balance;
                cout << "Enter the phone number: ";
                cin >> arr[i].phone;
                t.insert(arr[i]);
            }
            break;
        case 2:
            t.print(t.root);
            break;
        case 3:
            t.store(t.root);
            cout << "Saved!" << endl << "in directory:  C:/Users/Taiseer/Documents/Visual Studio 2015/Projects/ADS Project/ADS Project" << endl;
            break;
        case 4:
            cout << endl;
            int s;
            cout << "What element do you want to search for? ";
            cin >> s;
            if (t.searchid(t.root, s) == false) {
                cout << " Not here.... :( \n";
            }

            cout << endl;
            break;
        case 5:
            char x;
            cin >> x;
            if (x == 'y' || x == 'Y') {
                int id;
                cout << "Enter the id you want to edit: ";
                cin >> id;
                t.edit(id);
            }
            else
                return;
            break;
        case 6:
            break;
        case 7:
            m = 0;
            break;
        default:
            cout << "lol" << endl;
        }
    }
}

问题是 Tree::SAD() return 正在按值 搜索 的节点。这意味着在 Tree::edit() 中,以下行:

Node p = SAD(q);

获取实际节点的副本p 中的任何更改在实际树中都不会更改。在 edit() 的末尾,您尝试 insert(p.data),但这没有做任何事情,因为您的 insert() 的实现永远不会覆盖已经存在的节点。

一个解决方案是使 SAD() return 成为指向找到的节点的指针。这有一个额外的好处,您可以 return nullptr 表示搜索的 id 不存在的情况。然后可以在 edit() 中使用它来直接更改 Node 结构的字段。