【问题标题】:How to change the value of all nodes in a BST tree?如何更改 BST 树中所有节点的值?
【发布时间】:2020-07-25 10:26:53
【问题描述】:

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

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

一切正常,我只是不知道如何通过在它们上添加 log2 来更改所有节点的值。

如何遍历每个节点?

我的 Node.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);
};

【问题讨论】:

  • 通过递归,改变当前节点的一个值,然后调用兄弟节点的改变
  • 你的意思是,我创建了一个函数来更改值并在其中调用它?
  • 您想将 log(2)_base10 添加到树中的每个节点?如果我有来自单个节点的值,我该如何修改它以获得你想要的结果?
  • 我会在一秒钟内编辑我的帖子
  • 为什么要将数字存储在字符串中?好吧,您可以添加一个方法,该方法将指向节点的指针作为输入,增加此 $\log_2$ 存储的值,然后从 silbling 节点调用此方法。为根节点调用此方法

标签: c++ tree binary-search-tree


【解决方案1】:

由于您有 NodeBST 的不同概念(这很好),您需要一个函数作用于节点,使用您决定的任何规则修改节点的值,并调用自身该节点的子节点。

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

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

由于这显然是家庭作业,因此省略了几个星期的实施。

【讨论】:

    猜你喜欢
    • 2012-10-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-01-19
    相关资源
    最近更新 更多