• Serialize And Deserialize Binary Tree


    1. Serialize And Deserialize Binary Tree

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;

    namespace SerializeAndDeserializeBinaryTree
    {
        class Program
        {
            static void Main(string[] args)
            {
                //Test serialize

                List<string> serializeResult = new List<string>();

                nodes<string> rootNode = BinaryTree.Initialization();
                BinaryTree.SerializationByPreOrder(rootNode, serializeResult);

                //Test Deserialize
                nodes<string> root = null;

                int index = 0;
                BinaryTree.DeSerializationByDePreorder(root, serializeResult, ref index);
               
            }
        }


        public class nodes<T>
        {
            T data;
            nodes<T> Lnode, rnode, pnode;
            public T Data
            {
                get { return data; }
                set { data = value; }
            }
            public nodes<T> LNode
            {
                get { return Lnode; }
                set { Lnode = value; }
            }
            public nodes<T> RNode
            {
                get { return rnode; }
                set { rnode = value; }
            }
            public nodes<T> PNode
            {
                get { return pnode; }
                set { pnode = value; }
            }
            public nodes() { }
            public nodes(T data)
            {
                this.data = data;
            }
        }

       public class BinaryTree
        {     

            //构造一棵已知的二叉树  
           static public nodes<string> Initialization()
            {
                nodes<string>[] binTree = new nodes<string>[8];

                //创建节点  
                binTree[0] = new nodes<string>("A");
                binTree[1] = new nodes<string>("B");
                binTree[2] = new nodes<string>("C");
                binTree[3] = new nodes<string>("D");
                binTree[4] = new nodes<string>("E");
                binTree[5] = new nodes<string>("F");
                binTree[6] = new nodes<string>("G");
                binTree[7] = new nodes<string>("H");

                //使用层次遍历二叉树的思想,构造一个已知的二叉树  
                binTree[0].LNode = binTree[1];
                binTree[0].RNode = binTree[2];
                binTree[1].RNode = binTree[3];
                binTree[2].LNode = binTree[4];
                binTree[2].RNode = binTree[5];
                binTree[3].LNode = binTree[6];
                binTree[3].RNode = binTree[7];

                //返回二叉树根节点  
                return binTree[0];
            }

            //先序遍历  
            static public void SerializationByPreOrder(nodes<string> rootNode, List<string> a)
            {
                if (rootNode != null)
                {
                    Console.WriteLine(rootNode.Data);
                    a.Add(rootNode.Data);
                    SerializationByPreOrder(rootNode.LNode, a);
                    SerializationByPreOrder(rootNode.RNode, a);
                }
                else
                {
                    a.Add(string.Empty);
                    Console.WriteLine("#");
                }
            }


            static public void DeSerializationByDePreorder(nodes<string> rootNode, List<string> a, ref int index)
            {
                if (a[index] != string.Empty)
                {
                    rootNode = new nodes<string>();
                    Console.WriteLine(a[index]);
                    rootNode.Data = a[index];
                    index = index + 1;
                    DeSerializationByDePreorder(rootNode.LNode, a,ref index);
                    DeSerializationByDePreorder(rootNode.RNode, a, ref index);
                }
                else
                {
                    index = index + 1;
                    Console.WriteLine("#");
                    return;
                }
            }

            //中序遍历二叉树    
            static void MidOrder<T>(nodes<T> rootNode)
            {
                if (rootNode != null)
                {
                    MidOrder<T>(rootNode.LNode);
                    Console.WriteLine(rootNode.Data);
                    MidOrder<T>(rootNode.RNode);
                }
            }

            //后序遍历二叉树   
            static void AfterOrder<T>(nodes<T> rootNode)
            {
                if (rootNode != null)
                {
                    AfterOrder<T>(rootNode.LNode);
                    AfterOrder<T>(rootNode.RNode);
                    Console.WriteLine(rootNode.Data);
                }
            }
            //层次遍历二叉树         
            static void LayerOrder<T>(nodes<T> rootNode)
            {
                nodes<T>[] Nodes = new nodes<T>[20];
                int front = -1;
                int rear = -1;
                if (rootNode != null)
                {
                    rear++;
                    Nodes[rear] = rootNode;
                }
                while (front != rear)
                {
                    front++;
                    rootNode = Nodes[front];
                    Console.WriteLine(rootNode.Data);
                    if (rootNode.LNode != null)
                    {
                        rear++;
                        Nodes[rear] = rootNode.LNode;
                    }
                    if (rootNode.RNode != null)
                    {
                        rear++;
                        Nodes[rear] = rootNode.RNode;
                    }
                }
            }
        }
    }
  • 相关阅读:
    基于MongoDB.Driver的扩展
    通用查询设计思想
    API接口通讯参数规范
    lambda简单记录
    list去重精简代码版
    spring boot file上传
    fastjson过滤器简单记录
    java读取properties文件
    list循环删除单个元素
    MapReduce运行流程分析
  • 原文地址:https://www.cnblogs.com/Jessy/p/2540529.html
Copyright © 2020-2023  润新知