Binary Tree serialization

Source: Internet
Author: User

 

Serialization and deserialization of common Binary Trees:

Traverse in sequence, and the null node is marked with a special symbol.

import java.io.File;import java.io.FileNotFoundException;import java.io.PrintStream;import java.util.Scanner;public class Solution {    public static void serialize(TreeNode root, PrintStream ps) {        if (root == null)            ps.print("# ");        else {            ps.print(root.val + " ");            serialize(root.left, ps);            serialize(root.right, ps);        }    }    public static TreeNode deserialize(Scanner cin) {        String token = cin.next();        if (token.equals("#"))            return null;        int val = Integer.parseInt(token);        TreeNode root = new TreeNode(val);        root.left = deserialize(cin);        root.right = deserialize(cin);        return root;    }    public static void main(String[] args) throws FileNotFoundException {        TreeNode root = new TreeNode(30);        root.left = new TreeNode(20);        root.left.left = new TreeNode(10);        root.right = new TreeNode(40);        root.right.left = new TreeNode(35);        root.right.right = new TreeNode(50);        PrintStream ps = new PrintStream(new File("serialize.txt"));        serialize(root, ps);        Scanner cin = new Scanner(new File("serialize.txt"));        TreeNode back = deserialize(cin);                System.out.println(back.val);    }}

 

 

BST serialization and deserialization:

Serialization: directly first traverse the output

Deserialization: it is constructed by first-order traversal. O (n) time.

import java.io.FileNotFoundException;import java.util.Scanner;public class Solution {    private static int curVal;    public static TreeNode deserializeBSTfromInorder(Scanner cin) {        if (!cin.hasNext())            return null;        else {            curVal = cin.nextInt();            return deserialize(cin, Integer.MIN_VALUE, Integer.MAX_VALUE);        }    }    private static TreeNode deserialize(Scanner cin, int min, int max) {        if (curVal > min && curVal < max) {            int val = curVal;            TreeNode root = new TreeNode(val);            if (cin.hasNext()) {                curVal = cin.nextInt();                root.left = deserialize(cin, min, val);                root.right = deserialize(cin, val, max);            }            return root;        } else            return null;    }    public static void main(String[] args) throws FileNotFoundException {        String s = "30 20 10 40 35 50";        Scanner cin = new Scanner(s);        TreeNode back = deserializeBSTfromInorder(cin);        System.out.println(back.val);    }}

 

From array level data:

public class Solution {    private int curr;    public TreeNode deserialize(int[] preorder) {        curr = 0;        return deserialize(preorder, Integer.MIN_VALUE, Integer.MAX_VALUE);    }    public TreeNode deserialize(int[] preorder, int min, int max) {        if (curr >= preorder.length || preorder[curr] <= min || preorder[curr] >= max) {            return null;        }        int val = preorder[curr++];        TreeNode root = new TreeNode(val);        root.left = deserialize(preorder, min, val);        root.right = deserialize(preorder, val, max);        return root;    }            public static void main(String[] args) {        int[] pre = new int[] { 30, 20, 10, 40, 35, 50 };        TreeNode root = new Solution().deserialize(pre);                System.out.println(root);    }}

 

Refer:

Http://leetcode.com/2010/09/saving-binary-search-tree-to-file.html

Http://leetcode.com/2010/09/serializationdeserialization-of-binary.html

Binary Tree serialization

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.