简体   繁体   English

C ++反序列化二叉树

[英]C++ deserializing a binary tree

I'm attempting to serialize and then deserialize a binary tree, but seem to have run into a roadblock during the deserialization part.我正在尝试序列化然后反序列化二叉树,但在反序列化部分似乎遇到了障碍。 I have the following code:我有以下代码:

// Encodes a tree to a single string.
string serialize(TreeNode* root) {
    std::ostringstream oss;
    preorder_traversal(root, oss);
    return oss.str();
}

void preorder_traversal(TreeNode *root, ostringstream &oss) {
    if(root == nullptr) {
        oss << "null" << " "; // String streams are delimited by the space character
        return;
    }

    oss << root->val << " "; // String streams are delimited by the space character
    preorder_traversal(root->left, oss);
    preorder_traversal(root->right, oss);
}

// Decodes your encoded data to tree.
TreeNode* deserialize(string data) {
    std::istringstream iss(data);
    TreeNode *root;
    deserialize_tree(root, iss);
    return root;
}

void deserialize_tree(TreeNode *root, istringstream &iss) {        
    string current_val;
    iss >> current_val;
    if(current_val == "null") return;

    root = new TreeNode(std::stoi(current_val)); 

    // Testing to see if root->val is being assigned the correct value
    std::cout << root->val << std::endl;

    deserialize_tree(root->left, iss);
    deserialize_tree(root->right, iss);
}; 

The problem I'm facing, is that even though std::cout << root->val << std::endl shows that values are being assigned to the newly created TreeNodes in the deserialize_tree() function, when I finally return root in the deserialize() function, I get an empty output, which has left me scratching me head quite a bit.我面临的问题是,即使std::cout << root->val << std::endl显示值被分配给 deserialize_tree deserialize_tree()函数中新创建的TreeNodes时,我最终return rootdeserialize()函数中,我得到一个空输出,这让我摸不着头脑。

Note: The above code can be tested at https://leetcode.com/problems/serialize-and-deserialize-binary-tree/ if anyone would like to try and run it on some examples.注意:上面的代码可以在https://leetcode.com/problems/serialize-and-deserialize-binary-tree/进行测试,如果有人想在一些示例上运行它。

Maybe you need to pass the pointer and set the value it pointed to?也许您需要传递指针并设置它指向的值? (haven't tried it but I guess this is the problem) (没试过,但我想这是问题所在)

// note the **root
void deserialize_tree(TreeNode **root, istringstream &iss) {        
    string current_val;
    iss >> current_val;
    if(current_val == "null") return;

    // pass pointer in and set the value it pointed to
    *root = new TreeNode(std::stoi(current_val)); 

    // Testing to see if root->val is being assigned the correct value
    std::cout << (*root)->val << std::endl;

    // pass pointer
    deserialize_tree(&root->left, iss);
    deserialize_tree(&root->right, iss);
}; 

You have to modify the prototype and everywhere call this function too你必须修改原型并且到处调用这个函数

In code snippet which you have provided root = new TreeNode(std::stoi(current_val));在您提供的代码片段中root = new TreeNode(std::stoi(current_val)); is updating local instance of root and doesn't have any impact on calling function.正在更新root的本地实例,并且对调用函数没有任何影响。

Hope below snippet would work for you,希望下面的片段对你有用,

// Decodes your encoded data to tree.
TreeNode* deserialize(string data) {
    std::istringstream iss(data);
    TreeNode *root;
    deserialize_tree(root, iss);
    return root;
}

void deserialize_tree(TreeNode* &root, istringstream &iss) {        
    string current_val;
    iss >> current_val;
    if(current_val == "null") {root = nullptr;return;}

    root = new TreeNode(std::stoi(current_val)); 

     // Testing to see if root->val is being assigned the correct value
     std::cout << (root)->val << std::endl;
    deserialize_tree(root->left, iss);
    deserialize_tree(root->right, iss);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM