ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

C# 完全二叉树

2020-08-12 17:32:15  阅读:354  来源: 互联网

标签:rightChild C# Tree 完全 int static 二叉树 null root


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

namespace 快排
{
    class Tree
    {
        private int _data;
        private Tree _leftChild;
        private Tree _rightChild;

        public Tree(int value)
        {
            this._data = value;
            this._leftChild = null;
            this._rightChild = null;
        }

        public int data
        {
            get { return this._data; }
            set { this._data = value; }
        }

        public Tree leftChild
        {
            get { return this._leftChild; }
            set { this._leftChild = value; }
        }

        public Tree rightChild
        {
            get { return this._rightChild; }
            set { this._rightChild = value; }
        }
    }

    class text
    {
        private static int dataCount = 100; //数据量

        static Tree comTree = new Tree(0);  //完全二叉树的根节点
        static Tree sortTree = new Tree(0); //排序二叉树的根节点
        static List<Tree> comList = new List<Tree>();   
        static int[] ranData = new int[dataCount];
        static int comLeafCount = 0;
        static int sortLeafCount = 0;

        static void Main(string[] args)
        {
            Random ran = new Random();
            for(int i=0;i<dataCount;i++)
            {
                ranData[i] = ran.Next(1, 200);
            }
            do
            {
                switch (menu())
                {
                    case 1:
                        {
                            comTree = new Tree(0);
                            buildComTree();
                            Console.WriteLine("完全二叉树构建完成!按任意键继续...\n");
                            Console.ReadKey();
                            break;
                        }
                    case 2:
                        {
                            sortTree = new Tree(0);
                            buildSortTree();
                            Console.WriteLine("排序二叉树构建完成!按任意键继续...\n");
                            Console.ReadKey();
                            break;
                        }
                    case 3:
                        {
                            if(sortTree.data==0)
                            {
                                Console.WriteLine("排序二叉树尚未被构建!按任意键继续...\n");
                            }
                            else
                            {
                                InOrder(sortTree);
                                Console.WriteLine();
                                Console.WriteLine("\n中序序列输出完毕!按任意键继续...\n");
                            }
                            Console.ReadKey();
                            break;
                        }
                    case 4:
                        {
                            if (sortTree.data != 0)
                            {
                                sortLeafCount = 0;
                                countSortLeaf(sortTree);
                                Console.WriteLine("leafCount:" + sortLeafCount + "\n");
                            }
                            if ( comTree.data != 0 )
                            {
                                comLeafCount = 0;
                                countComLeaf(comTree);
                                Console.WriteLine("leafCount:" + comLeafCount + "\n");
                            }
                            Console.WriteLine("输出完毕!按任意键继续...\n");
                            Console.ReadKey();
                            break;
                        }
                    case 5:
                        {
                            if (sortTree.data != 0)
                            {
                                int leafDepth = 0;
                                leafDepth = depth(sortTree);
                                Console.WriteLine("sortTreeDepth:" + leafDepth + "\n");
                            }
                            if (comTree.data != 0)
                            {
                                int leafDepth = 0;
                                leafDepth = depth(comTree);
                                Console.WriteLine("comTreeDepth:" + leafDepth + "\n");
                            }
                            Console.WriteLine("输出完毕!按任意键继续...\n");
                            Console.ReadKey();
                            break;
                        }
                    default: return; 
                }
            } while (true);
            
        }

        /// <summary>
        /// 显示菜单,提示用户输入,处理输入并作为返回值
        /// </summary>
        static int menu()
        {
            int selected;
            Console.WriteLine("\n\n                                       菜单\n\n");
            Console.WriteLine("* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *\n\n");
            Console.WriteLine("1 .生成完全二叉树\n");
            Console.WriteLine("2 .生成二叉排序树\n");
            Console.WriteLine("3 .输出中序遍历序列\n");
            Console.WriteLine("4 .计算叶子结点数\n");
            Console.WriteLine("5 .计算二叉树深度\n");
            Console.WriteLine("6 .退出程序\n");
            Console.WriteLine("* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *\n\n\n");
            do
            {
                try
                {
                    selected = int.Parse(Console.ReadLine());
                }
                catch (Exception)
                {
                    Console.WriteLine("请输入正确的选项!\n");
                    continue;
                }
                    if(selected>=1 && selected<=7)
                    {
                        return selected;
                    }
                    else
                    {
                        Console.WriteLine("请输入正确的选项!\n");
                    }
            } while (true);
        }

        /// <summary>
        /// 创建完全二叉树
        /// </summary>
        static void buildComTree()
        {
            comList.Clear();
            comTree.data = ranData[0];
            comList.Add(comTree);
            for(int i=1;i<dataCount;i++)
            {
                Tree comTree2 = new Tree(ranData[i]);
                comList.Add(comTree2);
                if(i%2==0)
                {
                    comList[(i-1)/2].rightChild = comTree2;
                }
                else
                {
                    comList[i/2].leftChild = comTree2;
                }
            }
        }

        /// <summary>
        /// 创建排序二叉树
        /// </summary>
        static void buildSortTree()
        {
            sortTree.data = ranData[0];
            for(int i=1;i<dataCount;i++)
            {
                Tree newSort = new Tree(ranData[i]);
                Tree temp = sortTree;
                while(true)
                {
                    if(temp.data<=newSort.data)
                    {
                        if (temp.rightChild == null)
                        {
                            temp.rightChild = newSort;
                            break;
                        }
                        else
                        {
                            temp = temp.rightChild;
                        }
                    }
                    else
                    {
                        if (temp.leftChild == null)
                        {
                            temp.leftChild = newSort;
                            break;
                        }
                        else
                        {
                            temp = temp.leftChild;
                        }
                    }
                }
            }
        }

        /// <summary>
        /// 计算完全二叉树的叶子数
        /// </summary>
        static void countComLeaf(Tree root)
        {
            if (root != null)
            {
                if (root.leftChild == null && root.rightChild == null)
                {
                    comLeafCount++;
                }
                else
                {
                    if (root.leftChild != null)
                    {
                        countComLeaf(root.leftChild);
                    }
                    if (root.rightChild != null)
                    {
                        countComLeaf(root.rightChild);
                    }
                }
            }
        }

        /// <summary>
        /// 计算排序二叉树的叶子数
        /// </summary>
        static void countSortLeaf(Tree root)
        {
            if (root != null)
            {
                if (root.leftChild == null && root.rightChild == null)
                {
                    sortLeafCount++;
                }
                else
                {
                    if (root.leftChild != null)
                    {
                        countSortLeaf(root.leftChild);
                    }
                    if (root.rightChild != null)
                    {
                        countSortLeaf(root.rightChild);
                    }
                }
            }
        }

        /// <summary>
        /// 计算二叉树的深度,并作为返回值
        /// </summary>
        static int depth(Tree root)
        {
            int treeDepth = 0;
            if(root!=null)
            {
                int depthLeft = depth(root.leftChild);
                int depthRight = depth(root.rightChild);
                treeDepth = 1 + (depthLeft >= depthRight ? depthLeft : depthRight);
            }
            return treeDepth;
        }

        /// <summary>
        /// 用以输出中序序列
        /// </summary>
        /// <param name="root"></param>
        static void InOrder(Tree root)
        {
            if (root != null)
            {
                InOrder(root.leftChild);
                Console.Write(root.data+" ");
                InOrder(root.rightChild);
            }
        }
    }
}

 

标签:rightChild,C#,Tree,完全,int,static,二叉树,null,root
来源: https://www.cnblogs.com/netlock/p/13491865.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有