From a742225d4210bcc4d7aa1d654a0165ea59531585 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Zeynep=20Nur=20Do=C4=9Faner?= <58522099+zndoganer@users.noreply.github.com> Date: Sun, 30 Jul 2023 22:34:13 +0300 Subject: [PATCH] Segmentation Fault is Fixed - "For loop" index limit made "n/2". This change avoids "temp" to accessing out of its boundary and prevents segmentation fault. - Code is formatted. --- .../Serialize and Deserialize Binary Tree.cpp | 129 +++++++++--------- 1 file changed, 66 insertions(+), 63 deletions(-) diff --git a/Trees/Serialize and Deserialize Binary Tree.cpp b/Trees/Serialize and Deserialize Binary Tree.cpp index 201fc0f4..c61268ff 100644 --- a/Trees/Serialize and Deserialize Binary Tree.cpp +++ b/Trees/Serialize and Deserialize Binary Tree.cpp @@ -1,103 +1,106 @@ -#include +#include 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((indleft=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<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 v(n); - for(int i=0;i>v[i]; - vector temp(n); - for(int i=0;i 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;ileft=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:"<