Answer To: Problem 1 Please write a Python program which serializes a binary tree (each node contains a...
Sudipta answered on Nov 12 2021
{
"cells": [
{
"cell_type": "code",
"execution_count": 5,
"metadata": {
"collapsed": false,
"deletable": true,
"editable": true
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"cbegfda\n"
]
}
],
"source": [
"import csv\n",
"import pandas as pd\n",
"class Node:\n",
" def __init__(self, data):\n",
" self.data = data\n",
" self.left = None\n",
" self.right = None\n",
"\n",
"#This is a serialization function where we take inOrder and preOrder and create the tree\n",
"ans=[]\n",
"def Tree(inOrder, preOrder, s, e):\n",
" if (s > e):\n",
" return None\n",
" node = Node(preOrder[Tree.preIndex])\n",
" Tree.preIndex += 1\n",
" if s == e:\n",
" return node\n",
" indx = search(inOrder, s, e, node.data)\n",
" node.left = Tree(inOrder, preOrder, s, indx - 1)\n",
" node.right = Tree(inOrder, preOrder, indx + 1, e)\n",
" return node\n",
"def search(arr, start, end, value):\n",
" for i in range(start, end + 1):\n",
" if arr[i] == value:\n",
" return i\n",
"\n",
"#This is a deserialization function where we print the post order of the binary tree which is being created.\n",
"def...