Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Segmentation Fault is Fixed #625

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
129 changes: 66 additions & 63 deletions Trees/Serialize and Deserialize Binary Tree.cpp
Original file line number Diff line number Diff line change
@@ -1,103 +1,106 @@
#include<bits/stdc++.h>
#include <bits/stdc++.h>
using namespace std;

// Definition for a binary tree node.
struct TreeNode
{
int val;
TreeNode *left;
TreeNode *right;
TreeNode(int x) : val(x), left(NULL), right(NULL) {}
};

// Definition for a binary tree node.
struct TreeNode {
int val;
TreeNode *left;
TreeNode *right;
TreeNode(int x) : val(x), left(NULL), right(NULL) {}
};

class Codec {
class Codec
{
public:


string serialize(TreeNode* root) // Encodes a tree to a single string
string serialize(TreeNode *root) // Encodes a tree to a single string
{
string s="";
if(root==NULL)//Wherever the root is NULL,simply add "X ";
string s = "";
if (root == NULL) // Wherever the root is NULL,simply add "X ";
return "X ";
s+=(to_string(root->val)+" ");//Add the value string,then space and got to left and then right
s+=serialize(root->left);
s+=serialize(root->right);
s += (to_string(root->val) + " "); // Add the value string,then space and got to left and then right
s += serialize(root->left);
s += serialize(root->right);
return s;
}


TreeNode* func(string data,int& ind)// Decodes your encoded data to tree.
TreeNode *func(string data, int &ind) // Decodes your encoded data to tree.
{
if((data[ind]=='X')||(ind>=data.length()))//If the value at index is 'X',that implies the node was null
if ((data[ind] == 'X') || (ind >= data.length())) // If the value at index is 'X',that implies the node was null
{
ind+=2;
ind += 2;
return NULL;
}
string sx="";
while((ind<data.length())&&(data[ind]!=' '))//To get the value at that node
string sx = "";
while ((ind < data.length()) && (data[ind] != ' ')) // To get the value at that node
{
sx+=data[ind];
sx += data[ind];
ind++;
}
ind++;
int temp1=stoi( sx );
TreeNode* nx=new TreeNode(temp1);//Add this node and process for left and right
nx->left=func(data,ind);
nx->right=func(data,ind);
int temp1 = stoi(sx);
TreeNode *nx = new TreeNode(temp1); // Add this node and process for left and right
nx->left = func(data, ind);
nx->right = func(data, ind);
return nx;
}

TreeNode* deserialize(string data) {
int ind=0;
return func(data,ind);//Calling the function with indices

TreeNode *deserialize(string data)
{
int ind = 0;
return func(data, ind); // Calling the function with indices
}
};//End of Codec Class
}; // End of Codec Class

void inorder(TreeNode* temp)//Inorder traversal of the tree
void inorder(TreeNode *temp) // Inorder traversal of the tree
{
if(temp==NULL)
return ;
if (temp == NULL)
return;
inorder(temp->left);
cout<<temp->val<<" ";
cout << temp->val << " ";
inorder(temp->right);
return ;
return;
}

int main()
{
int n;
cin>>n;//Number of nodes in full binary tree.
cin >> n; // Number of nodes in full binary tree.

vector<int> v(n);
for(int i=0;i<n;i++)//Taking input of the tree as a level-order-vector and make it full binary tree by adding value -1 at null values
cin>>v[i];
vector<TreeNode* > temp(n);
for(int i=0;i<n;i++)//Creating the nodes
vector<TreeNode *> temp(n);

for (int i = 0; i < n; i++) // Taking input of the tree as a level-order-vector and make it full binary tree by adding value -1 at null values
cin >> v[i];

for (int i = 0; i < n; i++) // Creating the nodes
{
if(v[i]==-1)
temp[i]=NULL;
if (v[i] == -1)
temp[i] = NULL;
else
temp[i]=new TreeNode(v[i]);
temp[i] = new TreeNode(v[i]);
}
for(int i=0;i<n;i++)//Creating the binary tree by adding links

for (int i = 0; i < n / 2; i++) // Creating the binary tree by adding links
{
if(temp[i]!=NULL)
if (temp[i] != NULL)
{
temp[i]->left=temp[2*i+1];
temp[i]->right=temp[2*i+2];
temp[i]->left = temp[2 * i + 1];
temp[i]->right = temp[2 * i + 2];
}
}

Codec codec;//Object Creation
TreeNode* head=codec.deserialize(codec.serialize(temp[0]));//Passing temp[0] as root parameter
cout<<"Inorder Traversal is:"<<endl;
inorder(head);//Checking whether the process went correct by printing the values of the tree in inorder fashion
return 0;


Codec codec; // Object Creation
TreeNode *head = codec.deserialize(codec.serialize(temp[0])); // Passing temp[0] as root parameter
cout << "Inorder Traversal is:" << endl;
inorder(head); // Checking whether the process went correct by printing the values of the tree in inorder fashion
return 0;
}

//Eg input :-
// Tested with below example (Segmentation fault is fixed.)
// Eg input :-
// 15
//1 2 3 -1 -1 5 4 -1 -1 -1 -1 -1 -1 -1 -1
//Output:-
//Inorder Traversal is:
//2 1 5 3 4
// 1 2 3 -1 -1 5 4 -1 -1 -1 -1 -1 -1 -1 -1
// Output:-
// Inorder Traversal is:
// 2 1 5 3 4