Fortune left-law basis class4- title sequence of 4 binary tree and deserialization

1. Title: binary serialization and deserialization

Here Insert Picture Description

① Serialization refers save a record tree to be saved with the serialization sequence preorder traversal manner. Order of the first sequence is 1,2,4,5,3,6,7; between a "_" separated, to which four 4,5,6,7 leaf node to node around it is stored as " # ", the final result of the first sequence is a sequence 1_2_4_ # # 5 # # 3_6 # # 7 # #_.
② deserialization is to restore the serialization of a tree structure in memory.

2. Analysis

Sequence of (1) a first sequence

Essence of the first sequence motif is still preorder traversal, the intermediate connector but added "_" and left and right subtrees of the empty "#." Consider first traversal recursion mode, and a string is stored with the entire sequence of results. Here put int type values stored in string nodes: node tree is int type, not directly into the string in the string, the process follows
the header file: #include
string S = the to_string (I); // integer i is converted to a string representation. After 22 eg.int type converted to string type "22" literal, but actually stored is two char-2

string serialByPre(Tree *head)
{
	if(head == NULL)
		return "#_";//为空时返回#,_是连接符
	string res = to_string(head->val) + '_';//字符串拼接
	res += serialByPre(head->left);
	res += serialByPre(head->right);
	return res;
}

(2) first order deserialization

Deserialization two steps, since the connector elements apart so only to help the first step is to remove the connector, stored in a vessel; second step is to re-configuration of the spanning tree.

① remove the separator and stored in a queue

Remove delimiters getline function. istream& getline (istream&& is, string& str, char delim);Function is read into the stream from the line IS, delim stop delimiter is encountered when delim delimiter is found, it will be extracted and discarded and is not stored into str, the next input from the operation after it starts; first parameter is a stream object, the string using the operation flow, the sequence of the generated string to string stream stringstream ss(s);. Then use a while loop continues to read getline()without "_" of content can be placed in the queue. Because the queue FIFO storage is guaranteed the same sequence, the element required to use direct easily pop queue in the queue.

Tree* reconByPreString(string s)
{
	if(s == "#_")//空树直接返回NULL
		return NULL;

	stringstream ss(s);//string流#include<sstream>
	string item;
	queue<string> q;
	while (getline(ss, item, '_')) 
		q.push(item);
		
	return reconPreOrder(q);
}

② re spanning tree structure

Re spanning tree configuration is used to reconstruction of preorder traversal of tree nodes, the specific idea is to first queue element pop as the head node, then according to preorder fashion about the establishment of the first node left and the right recursion. Here the literal value "11" is converted to an int 11 Stoi use () function, used as follows.

//stoi的形参是const string*,而atoi的形参是const char*。c_str()的作用是将const string*转化为const char*。
	/*string s1("1234567");
	char* s2 = "1234567";
	int a = stoi(s1);
	int b = atoi(s2);
	int c = atoi(s1.c_str());*/

Part of the code

Tree* reconPreOrder(queue<string>& q)//一定要用引用,因为要弹出最先的元素
{
	string val = q.front();//
	q.pop();
	if(val == "#")
		return NULL;
	Tree *head1 = new Tree(stoi(val));//“11”转换为int型11
	
	head1->left = reconPreOrder(q);
	head1->right = reconPreOrder(q);
	return head1;
}

3. The complete code

#include <iostream>
#include<string>
#include<queue>
#include<sstream>
using namespace std;

//树的数据结构
class Tree
{
public:
	int val;
	Tree *right;
	Tree *left;
	Tree(int x)
	{
		this->val = x;
		this->left = NULL;
		this->right = NULL;
	}
	Tree(){;}
};

//反序列化生成节点
Tree* reconPreOrder(queue<string>& q)//一定要用引用,因为要弹出最先的元素
{
	string val = q.front();//
	q.pop();
	if(val == "#")
		return NULL;
	Tree *head1 = new Tree(stoi(val));//“11”转换为int型11
	
	head1->left = reconPreOrder(q);
	head1->right = reconPreOrder(q);
	return head1;
}

//去掉间隔符
Tree* reconByPreString(string s)
{
	if(s == "#_")
		return NULL;

	stringstream ss(s);
	string item;
	queue<string> q;
	while (getline(ss, item, '_')) 
		q.push(item);

	return reconPreOrder(q);
}


//先序序列化
string serialByPre(Tree *head)
{
	if(head == NULL)
		return "#_";//为空时返回#,_是连接符
	string res = to_string(head->val) + '_';//字符串拼接
	res += serialByPre(head->left);
	res += serialByPre(head->right);
	return res;
}

int main()
{
	Tree *head = new Tree(11);
	head->left = new Tree(22);
	head->left->left = new Tree(44);
	head->left->right = new Tree(55);
	head->right = new Tree(33);
	head->right->left = new Tree(66);
	head->right->right= new Tree(77);
	string s1 = serialByPre(head);

	Tree *head1 = reconByPreString(s1);
	system("pause");
	return 0;
}
Published 51 original articles · won praise 1 · views 1374

Guess you like

Origin blog.csdn.net/shi_xiao_xuan/article/details/103903992