• [ZT]线索二叉树(C#数据结构五)


      1using System;
      2
      3namespace BiThrTree
      4{
      5    /// <summary>
      6    /// 定义结点类:
      7    /// </summary>

      8    class BTNode
      9    {
     10        //先序:先处理父结点,然后递归处理孩子
     11        //中序:先处理左孩子,再处理根节结,然后递归处理右孩子
     12        //后序:先递归处理所有孩子,再递归处理节点本点
     13        public char data;
     14        public int ltag, rtag;//0表示线索,1表示结点
     15        public BTNode lchild, rchild;
     16    }

     17    class BiThrTree
     18    {
     19        /// <summary>
     20        /// 建立一棵新二叉树:
     21        /// </summary>
     22        /// <param name="T"></param>

     23        static public void CreateBiThrTree(ref BTNode T)
     24        {
     25            char ch;
     26            ch = (char)Console.Read();
     27            if (ch == '#')
     28            {
     29                T = null;
     30            }

     31            else
     32            {
     33                T = new BTNode();
     34                T.data = ch;
     35                CreateBiThrTree(ref T.lchild);
     36                CreateBiThrTree(ref T.rchild);
     37            }

     38        }

     39        /// <summary>
     40        /// 线索化二叉树:
     41        /// </summary>
     42        /// <param name="T"></param>

     43        static BTNode pre, H;
     44        static public void Threading(ref BTNode T)
     45        {
     46            H = pre = new BTNode();
     47            pre.rchild = pre.lchild = null;
     48            pre.rtag = pre.ltag = 0;
     49            Thread(ref T);
     50        }

     51        static public void Thread(ref BTNode T)
     52        {
     53            if (T != null)
     54            {
     55                if (T.lchild == null{ T.lchild = pre; T.ltag = 0; }
     56                else { T.ltag = 1; }
     57                if (T.rchild == null{ T.rtag = 0; }
     58                else { T.rtag = 1; }
     59                if (pre.rchild == null && pre.rtag == 0) pre.rchild = T;
     60                pre = T;
     61                if (T.ltag == 1) Thread(ref T.lchild);
     62                if (T.rtag == 1) Thread(ref T.rchild);
     63            }

     64        }

     65        /// <summary>
     66        /// 先序输出:
     67        /// </summary>

     68        static public void PrePrint(BTNode T)
     69        {
     70            if (T != null)
     71            {
     72                Console.Write(T.data + "\t");
     73                if (T.ltag == 1) PrePrint(T.lchild);
     74                if (T.rtag == 1) PrePrint(T.rchild);
     75            }

     76        }

     77        /// <summary>
     78        /// 先序线索遍历输出:
     79        /// </summary>

     80        static public void PreThrPrint(BTNode T)
     81        {
     82            T = H.rchild;
     83            //Console.WriteLine("H.rchild.date::"+H.rchild.data);
     84            while (T != null)
     85            {
     86                Console.Write(T.data + "\t");
     87                if (T.rtag == 0) T = T.rchild;
     88                else
     89                {
     90                    if (T.ltag == 1) T = T.lchild;
     91                    else
     92                    {
     93                        T = T.rchild;
     94                    }

     95                }

     96            }

     97        }

     98        /// <summary>
     99        /// Deepth of a BiThrTree:
    100        /// </summary>

    101        static public int Deepth(BTNode T)
    102        {
    103            int a, b;
    104            if (T != null)
    105            {
    106                if (T.ltag == 1) a = Deepth(T.lchild); else a = 0;
    107                if (T.rtag == 1) b = Deepth(T.rchild); else b = 0;
    108                return (1 + max(a, b));
    109            }

    110            else
    111            {
    112                return 0;
    113            }

    114        }

    115        static public int max(params int[] w)
    116        {
    117            int max;
    118            max = w[0];
    119            for (int i = 0; i < w.Length; i++)
    120                if (max < w[i]) max = w[i];
    121            return max;
    122        }

    123        /// <summary>
    124        /// 复制线索二叉树:
    125        /// </summary>

    126        static public void DulplicateBiThrTree(BTNode T1, ref BTNode T2)
    127        {
    128            if (T1 != null)
    129            {
    130                T2 = new BTNode();
    131                T2.data = T1.data;
    132                T2.ltag = T1.ltag; T2.rtag = T1.rtag;
    133                if (T2.ltag == 1) DulplicateBiThrTree(T1.lchild, ref T2.lchild); else T2.lchild = T1.lchild;
    134                if (T2.rtag == 1) DulplicateBiThrTree(T1.rchild, ref T2.rchild); else T2.rchild = T1.rchild;
    135            }

    136        }

    137
    138        static void Main()
    139        {
    140            BTNode mytree = null;
    141            Console.WriteLine("Please input a tree(for example:abc##d##ed###):");
    142            CreateBiThrTree(ref mytree);
    143            Threading(ref mytree);
    144            PrePrint(mytree);
    145            Console.WriteLine("\n按先序输出:\n");
    146            PreThrPrint(mytree);
    147            Console.WriteLine("\n该树的深度为:{0}", Deepth(mytree));
    148            BTNode mytree2 = null;
    149            Console.WriteLine("调用复制函数得到的新树为:");
    150            DulplicateBiThrTree(mytree, ref mytree2);
    151            PrePrint(mytree2);
    152            Console.ReadLine();
    153            Console.ReadLine();
    154        }

    155
    156    }

    157
    158}
  • 相关阅读:
    转 Python常见数据结构整理
    转 Python爬虫实战二之爬取百度贴吧帖子
    转 Python——UnicodeEncodeError: 'ascii' codec can't encode/decode characters
    慕课 python 操作数据库2 银行转账实例
    转 Python爬虫实战一之爬取糗事百科段子
    SQL 锁 lock
    SQL SERVER CROSS/OUTER APPLY 用法
    sql server 怎样用select语句调用自定义表值函数
    计算日期类型
    行转列:SQL SERVER PIVOT与用法解释
  • 原文地址:https://www.cnblogs.com/solo/p/599646.html
Copyright © 2020-2023  润新知