GoConqr has a new look! Let us know what you think.

Flashcards

Serialize and Deserialize nAry Tree

Suhas S English 1 card

Cards in this deck (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

Information

Description
Flashcards on Serialize and Deserialize nAry Tree, created by Suhas S on 13/05/2018.
Created by
Suhas S · May 2018
Updated
May 13, 2018
Views
0
Rating

Feedback

What is it about?

What do you think of the new look?

How do you like it?