'C++ - Making BST move onto the next node
I need to make a bank system where each customer that registers onto the bank will have a generated Customer ID that is stored inside BST. I brought all the code needed and it works perfectly until I need it to make a new node to store a new customer ID because all it is doing right now is just replacing the new ID with the old ID inside the same node.
{
private:
int data;
BST* left, * right;
public:
BST()
{
data = 0;
left = right = NULL;
}
BST(int value)
{
data = value;
left = right = NULL;
}
BST* createNewNode(int value)
{
BST* newBST = new BST;
newBST->data = value;
newBST->left = NULL;
newBST->right = NULL;
return newBST;
}
BST* Insert(BST* root, int data)
{
if (root == NULL)
{
return createNewNode(data);
}
if (data < root->data)
{
root->left = Insert(root->left, data);
}
else if (data > root->data)
{
root->right = Insert(root->right, data);
}
return root;
}
void Inorder(BST* root)
{
if (!root)
{
return;
}
Inorder(root->left);
cout << root->data << endl;
Inorder(root->right);
}
};
#include <iostream>
#include <string>
#include "BST.h"
using namespace std;
class Customer: public BST
{
private:
int customerID = 1;
string customerName;
string customerAddress;
string customerMobileNumber;
string customerEmergencyPerson;
string customerEmergencyPersonNumber;
string customerEmail;
string customerFacebook;
string customerHomeNumber;
public:
int getID()
{
return customerID;
}
string getName()
{
return customerName;
}
Customer(int x, string y, string z, string p)
{
x = customerID;
y = customerName;
z = customerAddress;
p = customerMobileNumber;
}
Customer()
{
}
void createCustomer()
{
BST customers, *root = NULL;
root = customers.Insert(root, 1);
cout << "If you are new to our bank you need to have an ID before you make bank accounts :)" << endl;
customerID = customerID + 1;
cout << "Your Customer ID is " << customerID << endl;
cout << "Customer's name: ";
getline(cin >> ws, customerName);
cout << "Customer's address: ";
getline(cin >> ws, customerAddress);
cout << "Customer's mobile number: ";
getline(cin >> ws, customerMobileNumber);
cout << "Customer's Emergency Contact: ";
getline(cin >> ws, customerEmergencyPerson);
cout << "Customer's Emergency Contact number: ";
getline(cin >> ws, customerEmergencyPersonNumber);
cout << "Customer's Email address: ";
getline(cin >> ws, customerEmail);
cout << "Customer's Facebook name: ";
getline(cin >> ws, customerFacebook);
cout << "Customer's home number: ";
getline(cin >> ws, customerHomeNumber);
customers.Insert(root, customerID);
customers.Inorder(root);
}
};
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
