提问者:小点点

如何更改BST树中所有节点的值?


如何更改树中所有节点的值?

我用多个键值填充了BST树。

所有的工作都很完美,我只是不知道如何通过添加log2来改变所有节点的值。

我如何在每个节点中迭代?

我的节点。hpp

class Node{

    private:
        Node *left;                     //left child
        Node *right;                    //right child
        std::string num;
    public:
        int data;                       //number
        Node();                         //constructor
        void setData(string num, int data);         //sets number in node
        string getData();                   //return numbers from node
        int &getOcc();
        void setLeft(Node *l);          //sets left child pointer
        Node* &getLeft();                //returns left child pointer
        void setRight(Node *r);         //sets right child pointer
        Node* &getRight();               //return right child pointer
};

我的BST.HPP

class BST{

    private:
        Node * root;        //root node pointer

    public:
        BST();                                  //constructor
        ~BST();                                 //destructor
        void Insert(string num, int data);      //Inserts new number in tree
        void InsertIDF(string num, int data);      //Inserts new number in tree
        bool find(string num);                 //finds whether a number is present in tree
        void min();                             //find and print minimum number in the tree
        void max();                             //find and print maximum number in the tree
        void save_file(string filename);        //save the tree to file
        void Delete(string num);                //deletes a number from tree
        void LoadFromFile(string filename);     //loads numbers from file to tree
        void Print();                           //print tree to stdout


        //private functions used as helper functions in the public operations
    private:
        void printHelper(Node *root);
        bool findHelper(Node *root,string num);
        void InsertHelper(Node * &current, string num, int data);
        void InsertHelperIDF(Node * &current, string num, int data);
        void findMinHelper(Node* current);
        void findMaxHelper(Node * current);
        void saveHelper(ofstream &fout, Node* current);
        Node* DeleteHelper(Node *current, string num);
        Node * findMaximum(Node * n);
        void clear(Node *currnt);
};


共1个答案

匿名用户

由于您对节点bst有不同的概念(这很好),因此需要一个函数来作用于节点,用您决定的任何规则修改节点的值,并在该节点的子节点上调用它自己。

然后,您可以通过使用一个包装器函数将其封装到BST中,该包装器函数以根节点作为参数调用上述函数。

让第一个开始工作,然后再做下一个。

实现被忽略了几周,因为这显然是家庭作业。