Please design a algorithm to implement serialization and deserialization of binary tree, there is no limit your sequence/deserialization algorithm logic, you only need to make sure that a binary tree can be serialized as a string and put the string deserialization for primitive tree structure,
Example:
You can take the following binary tree:
1
/\
2, 3,
/\
4, 5
Serialized as "1 # 2 # 3 # 4 # 5 # #"
Error timeout now, don't know whether I the following string can use, or is it the other problems?
void DFS (struct root TreeNode * and char * s);
Char * serialize (struct root TreeNode *) {
If (root==NULL)
return NULL;
Char * re=(char *) malloc (sizeof (char) * 5000);
DFS (root, re);
Return re;
}
Void DFS (struct root TreeNode * and char * s)
{
If (root==NULL)
{
Strcat (s, "#");//if it is NULL, the string after add "#";
return;
}
Int temp=root - & gt; val;
Int index=strlen (s);
While (temp)
{
S [index++]=(char) (temp % 10 + '0');//directly add value, at the back of the string, and finally add a space, difference, where numerical ended,
Temp/=10;
}
S [index++]=' ';
S [index]='\ 0';
DFS (root - & gt; Left, s);
DFS (root - & gt; Right, s);
}
/* * Decodes the your encoded data to the tree. */
Data struct TreeNode * deserialize (char *) {
If (data [0]=='\ 0')
return NULL;
If (data [0]=='#')
{
Data=https://bbs.csdn.net/topics/data + 1;//if the string passed is the first "#", will string address translation for the location of the next index,
return NULL;
}
Struct root TreeNode *=(struct TreeNode *) malloc (sizeof (struct TreeNode));
Root - & gt; Val=0;
Int I=0;
For (I=0; Data [I] & gt;='0' & amp; & Data [I] <='9'; I++)
{
Root - & gt; Val *=10;
Root - & gt; Val +=(int) (data [I] - '0');
}
Data=https://bbs.csdn.net/topics/data + I + 1;//the current values, take out after, will be the address of the string to the value and the subsequent Spaces, after an address, don't know whether this operation is possible here????????????????
Root - & gt; Left=deserialize (data);
Root - & gt; Right=deserialize (data);
Return the root;
}
//Your functions provides will be called as to:
//char * data=https://bbs.csdn.net/topics/serialize (root);
//deserialize (data);
CodePudding user response:
Changed a way to write, finally passed, though time-consuming and memory consumption is larger,/* ** Definition for a binary tree node.
* struct TreeNode {
* int val;
* struct TreeNode * left;
* right * struct TreeNode;
*};
*/
/* * Encodes a tree to a single string. */
Void DFS (struct root TreeNode * and char * s);
Data struct TreeNode * DE (char * and an int * index, int len);
Char * serialize (struct root TreeNode *) {
If (root==NULL)
return NULL;
Char * re=(char *) malloc (sizeof (char) * 5000);
Re [0]='\ 0';
DFS (root, re);
Return re;
}
Void DFS (struct root TreeNode * and char * s)
{
If (root==NULL)
{
Strcat (s, "#");
return;
}
Char * STR=(char *) malloc (sizeof (char) * 12);
Sprintf (STR, "% d", root - & gt; Val);
//itoa (root - & gt; Val, STR, 10); Itoa function is incompatible with the ANSI standard;
Strcat (s, STR);
//strcat (s, "");
Free (STR);
DFS (root - & gt; Left, s);
DFS (root - & gt; Right, s);
}
/* * Decodes the your encoded data to the tree. */
Data struct TreeNode * deserialize (char *) {
If (data=NULL https://bbs.csdn.net/topics/=
return NULL;
Int index=0;
Int len=strlen (data);
Return DE (data, & amp; The index, len);
}
Data struct TreeNode * DE (char * and an int * index, int len)
{
//int len=strlen (data);
If ((* index) & gt;=len)
return NULL;
If ((* index) & lt; Len & amp; & Data [* index]=='#')
{
(* index) + +;
return NULL;
}
Int flag=1;
If (data [* index]=='-')
{
Flag=1;
(* index) + +;
}
Else if (data [* index]=='+')
{
(* index) + +;
}
Struct root TreeNode *=(struct TreeNode *) malloc (sizeof (struct TreeNode));
Root - & gt; Val=0;
While (index (*) & lt;=len & amp; & Data [* index] & gt;='0' & amp; & Data [* index] <='9')
{
Root - & gt; Val *=10;
Root - & gt; Val +=(int) (data/index (*) - '0');
(* index) + +;
}
Root - & gt; Val *=flag;
(* index) + +;
Root - & gt; Left=DE (data, index, len);
Root - & gt; Right=DE (data, index, len);
Return the root;
}
//Your functions provides will be called as to:
//char * data=https://bbs.csdn.net/topics/serialize (root);
//deserialize (data);
CodePudding user response:
"You only need to make sure that a binary tree can be serialized as a string and put the string deserialization for primitive tree structure,"This string, is not necessarily a text string, also can be binary, so you can be omitted, text to digital conversion process,
In addition
Char * STR=(char *) malloc (sizeof (char) * 12);//char STR [12], binary tree is not too deep
CodePudding user response:
This is a sequence of binary tree traversal process, then print or read traversal process, boundary node content//output
Void ioOutput (TreeNode * pTree, fp) FILE * {
Output pTree - & gt; The data;
If (pTree - & gt; Left) ioOutput (pTree - & gt; Left, fp); The else output #;
If (pTree - & gt; Right) ioOutput (pTree - & gt; Right, fp); The else output #
}
//read
TreeNode * ioInput (fp) FILE * {
ElementType data=https://bbs.csdn.net/topics/read data
If (data='#') https://bbs.csdn.net/topics/=return NULL;
TreeNode * pTree=(TreeNode *) malloc (TreeNode);
PTree - & gt; nullnullnullnullnull