Разрешить функции напрямую изменять элементы ввода - PullRequest
0 голосов
/ 25 апреля 2020

У меня есть следующий класс

template<class ItemType>
class BSTree
{
    public:
    BSTNode* root=NULL;
    BSTree(); //default constructor
    void insert(BSTNode* temproot,address toin); //insert node
    void delet(address searchkey); //delete a node that matches the given search key
    bool search(address searchkey); //check if the searched node is in the tree, has two inputs so the search can be done recursively
    BSTNode* getRef(BSTNode *temproot, address searchkey); //return a pointer of the searched node if it's in the tree
    int size(); //return the size of the tree
    bool isEmpty(); //check if the tree is empty
    void emptyBook(); //make the entire tree empty

    void inorder(BSTNode *temproot);//traversing the tree inorder and print out the elements
};

И вот как реализована функция вставки

template<class ItemType>
void BSTree<ItemType>::insert(BSTNode* temproot, address toin)
{
    if(temproot == NULL) //if it's an empty tree, set the new node as the root
    {
        printf("Setting Root\n");
        temproot=new BSTNode;
        temproot->key = toin;
        temproot->left = NULL;
        temproot->right = NULL;
        temproot->parent = NULL;
        root=temproot; //this line DOES NOT WORK with more than one nodes in the tree

        return;
    }
    else if(toin.firstName>temproot->key.firstName)
    {
        printf("Traveling right\n");
        insert(temproot->right, toin); //recur down the right
        temproot->right->parent = temproot; //set the parent of the child
        temproot->right=getRef(root,toin); //set the child of the parent
    }
    else if(toin.firstName<temproot->key.firstName)
    {
        printf("Traveling left\n");
        insert(temproot->left, toin); //recur down the left
        temproot->left->parent = temproot; //set the parent of the child
        temproot->left=getRef(root,toin); //set the child of the parent
    }
    else
    {
        printf("Error\n");
        return;
    }
}

В настоящее время она работает только для одного ввода, если я хочу вставить второй элемент в дерево все это разваливается. Есть ли способ для меня напрямую изменить определенный элемент дерева, просто изменив переменную temp root в функции? Так что мне не нужно делать что-то вроде root = temp root

Ниже приведена структура узла и структура адреса

struct BSTNode //each node stores the element and three pointers to the left/right and parents if the node
{
    address key;
    BSTNode *left;
    BSTNode *right;
    BSTNode *parent;//used to identify the root
};

class address
{
    public:
    std::vector<char> firstName;
    std::vector<char> lastName;
    std::vector<char> street;
    std::vector<char> city;
    std::vector<char> state;
    long int zipCode; //zip codes are usually short enough to be stored as int, but to be safe long int is used
    long long int phone; //to hold a phone number, more bits are required
    void setAddress(char firstName[],char lastName[],char street[],char city[], char state[], long int zipCode, long long int phone); //initialize a single address
    void printAddress(); //print out the address and all its information, mainly for testing
};

Я думаю, что могут быть некоторые хитрости Я могу сделать с передачей по значению / указателю / адресу, но понятия не имею, как go об этом

...