Problem 1 Please write a Python program which serializes a binary tree (each node contains a character) and deserializes back to the original binary tree with O(n) complexity, where n is the number of...

1 answer below »
please see instructions in the word doc


Problem 1 Please write a Python program which serializes a binary tree (each node contains a character) and deserializes back to the original binary tree with O(n) complexity, where n is the number of nodes in the binary tree. This is useful in transmit data from one site to another using communication network by serializing the data from source site, transmit the serialized data via the network, and then deserializes the data back to the original form. To make your job easier, you can serialize binary tree by generating preorder traversal sequence from the binary tree and then concatenating the in-order traversal sequence. Then do deserialization on the other site by reconstruct the binary tree accordingly, and print the postorder traversal sequence to verify your code is correct. In theory, you can use either preorder with in-order or use post-order with in-order, but the former one is easier than the latter because the first node in the preorder traversal is the root of the binary tree. Please name this program either p10.py or p10.ipynb. Your program should read all ta*.dat in the same directory as your p10.py or p10.ipynb, and generate output for each ta*.dat as the example below.   Example Input: abcdefg # preorder cbaedfg # inorder   Example Postorder Output: cbegfda
Answered Same DayNov 07, 2021

Answer To: Problem 1 Please write a Python program which serializes a binary tree (each node contains a...

Sudipta answered on Nov 12 2021
149 Votes
{
"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...
SOLUTION.PDF

Answer To This Question Is Available To Download

Related Questions & Answers

More Questions »

Submit New Assignment

Copy and Paste Your Assignment Here