¡GoConqr estrena diseño! Cuéntanos qué te parece.

Fichas

Serialize and Deserialize nAry Tree

Suhas S Inglés 1 ficha

Fichas del conjunto (1)

  1. 1public static String serialize(Node root) { StringBuilder result = new StringBuilder(); if (null != root) { result.append("."); result.append(root.key); result.append("."); for (Node child : root.childs) { result.append(Node.serialize(child)); } result.append(")"); } return result.toString(); } public static Node deserialize(String node) throws UnsupportedEncodingException { Node result = null; Stack stack = new Stack(); boolean isData = false; StringBuilder data = null; for (int i = 0; i < node.length(); i++) { if (node.charAt(i) == '.') { isData = !isData; if (isData) { data = new StringBuilder(); } else { Node child = new Node(data.toString()); if (!stack.isEmpty()) { Node parent = stack.peek(); parent.addChild(child);} else {
    result = child;
    }
    stack.push(child);
    }
    } else {
    if (isData) {
    data.append(node.charAt(i));
    } else if (node.charAt(i) == ')') {
    stack.pop();
    } else {
    throw new UnsupportedEncodingException(
    "Format not recognized.");
    }
    }
    }
    return result;
    }
    http://interviewpractise.blogspot.com/2014/11/serialize-and-deserialize-n-ary-tree.html

Información

Descripción
Fichas sobre Serialize and Deserialize nAry Tree, creado por Suhas S el 13/05/2018.
Creado por
Suhas S · may 2018
Actualizado
13 may 2018
Visitas
0
Valoración

Escríbenos

¿Sobre qué es tu mensaje?

¿Qué te parece el nuevo diseño?

¿Te gusta?