#toyprogrammingchallenge
#python Here's another freebie, I assume it is python specific because they start with a base of python code. But if it makes sense, try it in whatever language you like:
This problem was asked by Google.
Given the root to a binary tree, implement serialize(root), which serializes the tree into a string, and deserialize(s), which deserializes the string back into the tree.
For example, given the following Node class
class Node:
def __init__(self, val, left=None, right=None):
self.val = val
self.left = left
self.right = right
The following test should pass:
node = Node('root', Node('left', Node('left.left')), Node('right'))
assert deserialize(serialize(node)).left.left.val == 'left.left'
@Absinthe How about, one crazy function that works both ways!
https://git.sr.ht/~namark/mercury_stuff/tree/master/toys/serialize_btree.m
Though, it goes haywire if input is invalid or if value contains anything in the form of "|number|", where number is less than depth of the tree. Probably has something to do with compiler complaining that code I wrote is impure and non-deterministic, and me just silencing it.
Also it only handles string values, cause I don't know how to write generic code yet.
@khird tried to fix those cases here
https://git.sr.ht/~namark/mercury_stuff/tree/master/toys/not_fun_anymore_serialize_btree.m
hopefully without introducing new problems?
But it doesn't look like a single function anymore, so no fun. Will maybe try to rectify that later.
@khird yes the problem with the separator still remains. I guess I need to "quote" the values somehow.
It's the weirdest language, and on top of that I'm not using it right.