1. 程式人生 > >多叉樹序列化與反序列

多叉樹序列化與反序列

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.*;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * Created by Ok on 2017/1/19.
 */
public class SerilizedBean {

    public static void main(String[] args) {
        Node A = Node.createRoot
("A"); Node B = Node.addNode("B", A, null, null); Node C = Node.addNode("C", null, B, A); Node D = Node.addNode("D", null, C, A); Node E = Node.addNode("E", B, null, null); Node F = Node.addNode("F", C, null, null); Node G = Node.addNode("G", null, F, C); Node H = Node.addNode
("H", D, null, null); Node I = Node.addNode("I", null, H, D); // Node.preOrder(A, 0); String path = "D://1484810293157_q.obj"; Set<Map<String, String>> set2 = new HashSet<Map<String, String>>(); Node.putAllData(set2, A); try { ObjectOutputStream out = new
ObjectOutputStream(new FileOutputStream(path)); out.writeObject(set2); out.close(); } catch (Exception e) { e.printStackTrace(); } try { ObjectInputStream in = new ObjectInputStream(new FileInputStream(path)); Set<Map<String, String>> set = (Set<Map<String, String>>) in.readObject(); System.out.println(set.size()); List<Map<String, String>> list = new ArrayList<Map<String, String>>(set); Collections.sort(list, new Comparator<Map<String, String>>() { public int compare(Map<String, String> o1, Map<String, String> o2) { return o1.get("auto").compareTo(o2.get("auto")); } }); Node root = null; Map<String, Node> nodes = new TreeMap<String, Node>(); for (Map<String, String> obj : list) { String data = obj.get("data").toString(); String length = obj.get("length").toString(); nodes.put(data, Node.createNode(data,StringUtil.toInteger(length,0))); } for (Map<String, String> obj : list) { // data=A, parent=, auto=0, firstClild=B, pre=, next= String auto = obj.get("auto").toString(); String firstClild = obj.get("firstClild").toString(); String pre = obj.get("pre").toString(); String data = obj.get("data").toString(); String parent = obj.get("parent").toString(); Node parentNode = nodes.get(parent); Node me = nodes.get(data); Node preNode = nodes.get(pre); if (StringUtil.isEmpty(parent)) { root = me; } else if (StringUtil.isEmpty(pre)) { parentNode.firstChild = me; } else { me.preSibling = preNode; preNode.nextSibling = me; me.parent = parentNode; } } Node.preOrder(root, 0); } catch (Exception e) { e.printStackTrace(); } } static class Node { private String data; private int length; //所處級別 private Node firstChild; private Node nextSibling; private Node preSibling; //檢查firstChild private Node parent; //關聯parent private Node() { } private Node(String data) { this.data = data; } private Node(String data, int length) { this.data = data; this.length = length; } public void setLength(int length) { this.length = length; } public int getLength() { return length; } static Node createRoot(String data) { return createNode(data,0); } static Node createNode(String data,int length) { Node root = new Node(data, length); return root; } static Node addNode(String data, Node parent, Node willLeftNode, Node getparent) { if ((parent == null && willLeftNode == null) || (parent != null && willLeftNode != null)) { System.out.println("ERROR"); return null; } Node child = new Node(data); if (parent != null) { parent.firstChild = child; child.parent = parent; } else { willLeftNode.nextSibling = child; child.preSibling = willLeftNode; child.parent = getparent; } child.setLength(checkLength(child)); child.firstChild = null; child.nextSibling = null; return child; } static int checkLength(Node node) { if (node.parent == null && node.preSibling == null) { return 0; } if (node.parent != null) { return 1 + checkLength(node.parent); } return checkLength(node.preSibling); } static void preOrder(Node root, int none) { System.out.println(root.data + "..." + root.length); if (root.firstChild != null) { preOrder(root.firstChild, 1); } if (root.nextSibling != null) { preOrder(root.nextSibling, 2); } } private static AtomicInteger auto = new AtomicInteger(); private static Map<String, String> node2Map(Node root) { Map<String, String> map = new HashMap<String, String>(5); map.put("parent", root.parent == null ? "" : root.parent.data); map.put("pre", root.preSibling == null ? "" : root.preSibling.data); map.put("next", root.nextSibling == null ? "" : root.nextSibling.data); map.put("firstClild", root.firstChild == null ? "" : root.firstChild.data); map.put("data", root.data); map.put("length", String.valueOf(root.length)); map.put("auto", String.valueOf(auto.getAndIncrement())); return map; } public static Node set2Node(Set<Map<String, String>> set) { return null; } static void putAllData(Set<Map<String, String>> result, Node root) { if (root != null) result.add(node2Map(root)); if (root.firstChild != null) { putAllData(result, root.firstChild); } if (root.nextSibling != null) { putAllData(result, root.nextSibling); } } static void midOrder(Node root, int none) { if (root.firstChild != null) { preOrder(root.firstChild, 1); } System.out.println(root.data + "..." + root.length); if (root.nextSibling != null) { preOrder(root.nextSibling, 2); } } static boolean isfind(Node root, String data) { return ((root != null) && data.equals(root.data)) || (root.firstChild != null && isfind(root.firstChild, data)) || (root.nextSibling != null && isfind(root.nextSibling, data)); } } }