前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >二叉树的序列化和反序列化

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

作者头像
名字是乱打的
发布2022-05-13 09:49:50
1810
发布2022-05-13 09:49:50
举报
文章被收录于专栏:软件工程
  • 二叉树被记录成文件的过程叫作二叉树的序列化
  • 通过文件内容重建原来的二叉树过程叫做二叉树反序列化

思路 : 序列化:先序遍历树,将树中字符转换进字符串,空值设置为null,隔断符号设置为! 反序列化:先将!做分隔符分隔字符串为数组,装进队列,递归遍历队列,设置结点和其左右结点.

代码:
代码语言:javascript
复制
package com.algorithm.practice.tree;

import com.algorithm.practice.tree.traversal.PreOrderPrint;

import java.util.LinkedList;
import java.util.Queue;
import java.util.Stack;

public class SerializeAndReconstructTree {

    public static class Node {
        public int value;
        public Node left;
        public Node right;

        public Node(int data) {
            this.value = data;
        }
    }
    //先序遍历序列化,将遍历结果转换为字符串
    public  static String SerializeTree(Node node){
        if (node==null){
            return "#!";
        }
        String res=node.value+"!";
        res+=SerializeTree(node.left);
        res+=SerializeTree(node.right);
        return res;
    }
    public static  Node ReconstructTree(String tS){
        String[] nodes = tS.split("!");
        if (nodes[0].equals("#")){
            return null;
        }
        Queue<String> queue=new LinkedList<>();
        for(int i=0;i<nodes.length;i++)
        {
            queue.offer(nodes[i]);
        }
        return reconPreOrder(queue);
    }

    private static Node reconPreOrder(Queue<String> queue) {
        String value=queue.poll();
        if (value.equals("#")){
            return null;
        }
        Node head=new Node(Integer.parseInt(value));
        head.left=reconPreOrder(queue);
        head.right=reconPreOrder(queue);
        return head;
    }
    public static void  preOrderPrint(Node head){
        System.out.print("pre-order: ");
        if (head!=null){
            Stack<Node> stack=new Stack<>();
            stack.push(head);
            while (!stack.isEmpty()){
                head = stack.pop();
                System.out.print(head.value);
                if (head.right!=null){
                    stack.push(head.right);
                }
                if (head.left!=null){
                    stack.push(head.left);
                }
            }
        }
    }

    public static void main(String[] args) {
        Node 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);
        String serializeTree = SerializeTree(head);
        Node node = ReconstructTree(serializeTree);
        preOrderPrint(node);

    }

}
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2022-05-13,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 代码:
相关产品与服务
文件存储
文件存储(Cloud File Storage,CFS)为您提供安全可靠、可扩展的共享文件存储服务。文件存储可与腾讯云服务器、容器服务、批量计算等服务搭配使用,为多个计算节点提供容量和性能可弹性扩展的高性能共享存储。腾讯云文件存储的管理界面简单、易使用,可实现对现有应用的无缝集成;按实际用量付费,为您节约成本,简化 IT 运维工作。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档