public string Serialize(NodeString node)
        {
            if (node == null)
            {
                return(Empty_Marker + '-');
            }

            var builder = new StringBuilder();

            builder.Append($"{node.Value}-");

            builder.Append(Serialize(node.left));
            builder.Append(Serialize(node.right));

            return(builder.ToString());
        }
        private NodeString DeserializeNode(Queue <string> nodes)
        {
            if (nodes.Peek() != null)
            {
                var nextNode = nodes.Dequeue();

                if (nextNode == Empty_Marker)
                {
                    return(null);
                }

                var node = new NodeString(nextNode);

                node.left = DeserializeNode(nodes);

                node.right = DeserializeNode(nodes);

                return(node);
            }

            return(null);
        }
Ejemplo n.º 3
0
        public void SerializeDeSerializeBinaryTreeM()
        {
            // var node = new NodeString("root", new NodeString("left",
            // new NodeString("left.left")), new NodeString("right"));
            NodeString root = new NodeString("root")
            {
                left  = new NodeString("left"),
                right = new NodeString("right")
            };

            root.left.left = new NodeString("left.left")
            {
            };

            SerializeDeSerializeBinaryTreeC serializeDeSerializeBinaryTree = new SerializeDeSerializeBinaryTreeC();

            var serialized = serializeDeSerializeBinaryTree.Serialize(root);

            Console.WriteLine(serialized);
            root = serializeDeSerializeBinaryTree.Deserialize(serialized);

            Console.WriteLine(root.left.left.Value);
        }