• 二叉树的序列化和反序列化


    思路:如何序列化,就如何反序列化。每个节点值后面用 ‘!’表示结束,‘#’表示空。如下按先序遍历,序列化为:1!2!4!#!#!5!#!#!3!6!#!#!7!#!#!,注意空节点,要添加进来

    package class_04;
    
    import java.util.LinkedList;
    import java.util.Queue;
    
    public class Code_04_SerializeAndReconstructTree {
    
    	public static class Node {
    		public int value;
    		public Node left;
    		public Node right;
    
    		public Node(int data) {
    			this.value = data;
    		}
    	}
    
    	public static String serialByPre(Node head) {  // 按先序,序列化
    		if (head == null) {
    			return "#!";
    		}
    		String res = head.value + "!";
    		res += serialByPre(head.left);
    		res += serialByPre(head.right);
    		return res;
    	}
    
    	public static Node reconByPreString(String preStr) {  // 按先序,反序列化
    		String[] values = preStr.split("!");
    		Queue<String> queue = new LinkedList<String>();
    		for (int i = 0; i != values.length; i++) {
    			queue.offer(values[i]);
    		}
    		return reconPreOrder(queue);
    	}
    
    	public static Node reconPreOrder(Queue<String> queue) {
    		String value = queue.poll();
    		if (value.equals("#")) {
    			return null;
    		}
    		Node head = new Node(Integer.valueOf(value));
    		head.left = reconPreOrder(queue);
    		head.right = reconPreOrder(queue);
    		return head;
    	}
    
    	public static String serialByLevel(Node head) {  // 按层,序列化
    		if (head == null) {
    			return "#!";
    		}
    		String res = head.value + "!";
    		Queue<Node> queue = new LinkedList<Node>();
    		queue.offer(head);
    		while (!queue.isEmpty()) {
    			head = queue.poll();
    			if (head.left != null) {
    				res += head.left.value + "!";
    				queue.offer(head.left);
    			} else {
    				res += "#!";
    			}
    			if (head.right != null) {
    				res += head.right.value + "!";
    				queue.offer(head.right);
    			} else {
    				res += "#!";
    			}
    		}
    		return res;
    	}
    
    	public static Node reconByLevelString(String levelStr) {  // 按层,反序列化
    		String[] values = levelStr.split("!");
    		int index = 0;
    		Node head = generateNodeByString(values[index++]);
    		Queue<Node> queue = new LinkedList<Node>();
    		if (head != null) {
    			queue.offer(head);        // 根节点首先压入队列
    		}
    		Node node = null;
    		while (!queue.isEmpty()) {
    			node = queue.poll();      // 弹出队列中的元素
    			node.left = generateNodeByString(values[index++]);
    			node.right = generateNodeByString(values[index++]);
    			if (node.left != null) {  // 如果有左孩子,压入队列
    				queue.offer(node.left);
    			}
    			if (node.right != null) {  // 如果有右孩子,压入队列
    				queue.offer(node.right);
    			}
    		}
    		return head;
    	}
    
    	public static Node generateNodeByString(String val) {
    		if (val.equals("#")) {
    			return null;
    		}
    		return new Node(Integer.valueOf(val));
    	}
    
    	// for test -- print tree
    	public static void printTree(Node head) {
    		System.out.println("Binary Tree:");
    		printInOrder(head, 0, "H", 17);
    		System.out.println();
    	}
    
    	public static void printInOrder(Node head, int height, String to, int len) {
    		if (head == null) {
    			return;
    		}
    		printInOrder(head.right, height + 1, "v", len);
    		String val = to + head.value + to;
    		int lenM = val.length();
    		int lenL = (len - lenM) / 2;
    		int lenR = len - lenM - lenL;
    		val = getSpace(lenL) + val + getSpace(lenR);
    		System.out.println(getSpace(height * len) + val);
    		printInOrder(head.left, height + 1, "^", len);
    	}
    
    	public static String getSpace(int num) {
    		String space = " ";
    		StringBuffer buf = new StringBuffer("");
    		for (int i = 0; i < num; i++) {
    			buf.append(space);
    		}
    		return buf.toString();
    	}
    
    	public static void main(String[] args) {
    		Node head = null;
    		printTree(head);
    
    		String pre = serialByPre(head);
    		System.out.println("serialize tree by pre-order: " + pre);
    		head = reconByPreString(pre);
    		System.out.print("reconstruct tree by pre-order, ");
    		printTree(head);
    
    		String level = serialByLevel(head);
    		System.out.println("serialize tree by level: " + level);
    		head = reconByLevelString(level);
    		System.out.print("reconstruct tree by level, ");
    		printTree(head);
    
    		System.out.println("====================================");
    
    		head = new Node(1);
    		printTree(head);
    
    		pre = serialByPre(head);
    		System.out.println("serialize tree by pre-order: " + pre);
    		head = reconByPreString(pre);
    		System.out.print("reconstruct tree by pre-order, ");
    		printTree(head);
    
    		level = serialByLevel(head);
    		System.out.println("serialize tree by level: " + level);
    		head = reconByLevelString(level);
    		System.out.print("reconstruct tree by level, ");
    		printTree(head);
    
    		System.out.println("====================================");
    
    		head = new Node(1);
    		head.left = new Node(2);
    		head.right = new Node(3);
    		head.left.left = new Node(4);
    		head.right.right = new Node(5);
    		printTree(head);
    
    		pre = serialByPre(head);
    		System.out.println("serialize tree by pre-order: " + pre);
    		head = reconByPreString(pre);
    		System.out.print("reconstruct tree by pre-order, ");
    		printTree(head);
    
    		level = serialByLevel(head);
    		System.out.println("serialize tree by level: " + level);
    		head = reconByLevelString(level);
    		System.out.print("reconstruct tree by level, ");
    		printTree(head);
    
    		System.out.println("====================================");
    
    		head = new Node(100);
    		head.left = new Node(21);
    		head.left.left = new Node(37);
    		head.right = new Node(-42);
    		head.right.left = new Node(0);
    		head.right.right = new Node(666);
    		printTree(head);
    
    		pre = serialByPre(head);
    		System.out.println("serialize tree by pre-order: " + pre);
    		head = reconByPreString(pre);
    		System.out.print("reconstruct tree by pre-order, ");
    		printTree(head);
    
    		level = serialByLevel(head);
    		System.out.println("serialize tree by level: " + level);
    		head = reconByLevelString(level);
    		System.out.print("reconstruct tree by level, ");
    		printTree(head);
    
    		System.out.println("====================================");
    
    	}
    }
    
  • 相关阅读:
    Vector 、ArrayList、LinkedList比较
    MySQL主从复制
    多浏览器兼容flv视频播放HTML
    [转]javascript对联广告、漂浮广告封装类,多浏览器兼容
    ASP获取json天气信息
    IIS6的SSL配置,如何配置SSL到登陆页,如何将SSL证书设置成受信任的证书
    Web Service初探
    算法之逆序对
    算法之数组和问题
    重读算法导论之算法基础
  • 原文地址:https://www.cnblogs.com/horken/p/10706130.html
Copyright © 2020-2023  润新知