Article Directory
Serialization and deserialization binary search trees (serialize-and-deserialize-bst)
Serialization is the process of converting a data structure or object during a series of bits, so that it can be stored in the memory buffer or a file, or transmitted over a network connection link, for later reconstruction in the same or another computer environment.
Design an algorithm to serialization and de-serialization binary search tree . Serialization / de-serialization algorithm works is not limited. Just be sure binary search tree can be serialized to a string, and the string of anti-sequence can be reduced to the initial binary search tree.
Encoded string should be as compact as possible.
Note : Do not use class members / global / static variables to store state. Your serialization and de-serialization algorithm should be stateless.
Algorithms and ideas
Sequence of (encoded)
Preorder traversal binary search tree, traversing the integer data into a string and connecting the string data, special use delimited connection.
//将整型的数据转为字符串
void change_int_to_string(int val, std::string &str_val){
std::string tmp;
while(val){
tmp += val % 10 + '0';
val = val / 10;
}
for (int i = tmp.length() - 1; i >= 0; i--){
str_val += tmp[i];
}
str_val += '#';
}
//前序遍历
void BST_preorder(TreeNode *node, std::string &data){
if (!node){
return;
}
std::string str_val;
change_int_to_string(node->val, str_val);
data += str_val;
BST_preorder(node->left, data);
BST_preorder(node->right, data);
}
Deserialize (decoding)
The decoded string as a binary search tree:
the string in the separator when code "#", the respective numbers split out one by one, the first number of build a binary search tree root construct, each back digital a node when parsing the order of the root node, the root node returns, to complete the decoding work.
void BST_insert(TreeNode *node, TreeNode *insert_node){
if (insert_node->val < node->val){
if (node->left){
BST_insert(node->left, insert_node);
}
else{
node->left = insert_node;
}
}
else{
if (node->right){
BST_insert(node->right, insert_node);
}
else{
node->right = insert_node;
}
}
}
8#3#1#6#10#15#
8 | ||||||
---|---|---|---|---|---|---|
/ | \ | |||||
3 | 10 | |||||
/ | \ | \ | ||||
1 | 6 | 15 |
Code
#include <stdio.h>
struct TreeNode {
int val;
TreeNode *left;
TreeNode *right;
TreeNode(int x) : val(x), left(NULL), right(NULL) {}
};
#include <string>
#include <vector>
void BST_insert(TreeNode *node, TreeNode *insert_node){
if (insert_node->val < node->val){
if (node->left){
BST_insert(node->left, insert_node);
}
else{
node->left = insert_node;
}
}
else{
if (node->right){
BST_insert(node->right, insert_node);
}
else{
node->right = insert_node;
}
}
}
void change_int_to_string(int val, std::string &str_val){
std::string tmp;
while(val){
tmp += val % 10 + '0';
val = val / 10;
}
for (int i = tmp.length() - 1; i >= 0; i--){
str_val += tmp[i];
}
str_val += '#';
}
void BST_preorder(TreeNode *node, std::string &data){
if (!node){
return;
}
std::string str_val;
change_int_to_string(node->val, str_val);
data += str_val;
BST_preorder(node->left, data);
BST_preorder(node->right, data);
}
class Codec {
public:
std::string serialize(TreeNode* root) {//编码
std::string data;
BST_preorder(root, data);
return data;
}
TreeNode *deserialize(std::string data) {//解码
if (data.length() == 0){
return NULL;
}
std::vector<TreeNode *> node_vec;
int val = 0;
for (int i = 0; i < data.length(); i++){
if (data[i] == '#'){
node_vec.push_back(new TreeNode(val));
val = 0;
}
else{
val = val * 10 + data[i] - '0';
}
}
for (int i = 1; i < node_vec.size(); i++){
BST_insert(node_vec[0], node_vec[i]);
}
return node_vec[0];
}
};
void preorder_print(TreeNode *node,int layer){
if (!node){
return;
}
for (int i = 0; i < layer; i++){
printf("-----");
}
printf("[%d]\n", node->val);
preorder_print(node->left, layer + 1);
preorder_print(node->right, layer + 1);
}
int main(){
TreeNode a(8);
TreeNode b(3);
TreeNode c(10);
TreeNode d(1);
TreeNode e(6);
TreeNode f(15);
a.left = &b;
a.right = &c;
b.left = &d;
b.right = &e;
c.left = &f;
Codec solve;
std::string data = solve.serialize(&a);
printf("%s\n", data.c_str());
TreeNode *root = solve.deserialize(data);
preorder_print(root, 0);
return 0;
}