c#堆

using System;
using System.Collections.Generic;
//System.Collections.Generic 命名空间包含定义泛型集合的接口和类,
//用户可以使用泛型集合来创建强类型集合,
//这种集合能提供比非泛型强类型集合更好的类型安全性和性能。




namespace 所以
{
    class Tree
    {
        public class TreeNode
        {
            public int Data;//用来决定树的形状
            public int number;
            public TreeNode Left;//字段“成员变量”,一般在类的内部做数据交互使用。
            public TreeNode Right;//就是TreeNode.Right的意思
            //data left right都是treenode中的私有元素
            //下面的东西是一个元素
            public TreeNode(int Dta, int numb)//TreeNode.TreeNode(int Dta)是一个TreeNode里的函数
            {
                this.Data = Dta;//this可用于限定类似名称隐藏的成员
                                //eg   this.name = name;
                                //将对象作为参数传递给方法,例如:
                                //CalcTax(this);
                this.number = numb;
                this.Left = null;//this代表treenode
                this.Right = null;
            }

            public TreeNode AddNode(TreeNode Root, TreeNode NewNode)//引入两个treenode格式变量
            //表示TreeNode.AddNode函数
            {
                if (Root == null)
                {
                    Root = NewNode;
                    return Root;
                }
                else if (Root.Data > NewNode.Data)
                //上面的用法!引用root这个treenode类中的public int data 元素
                {
                    Root.Left = AddNode(Root.Left, NewNode);//给左节点赋值
                }
                else
                {
                    Root.Right = AddNode(Root.Right, NewNode);
                }
                return Root;

            }



        }
        private TreeNode Root = null;


        public void Add(int Data, int number)
        {
            TreeNode newNodeObj = new TreeNode(Data, number);
            Root = newNodeObj.AddNode(Root, newNodeObj);
        }




        //先序列遍
        private void Print(TreeNode Root)
        {
            if (Root == null)
            {
                return;
            }
            else
            {
                // Console.WriteLine(Root.Data);//输出的是字符串,所以用它也可以
                Console.WriteLine(Root.number);
                Print(Root.Left);//自己写的函数,遍历左子树
                Print(Root.Right);
            }
        }











        //中序遍历
        public void Print1()
        {
            InOrder(Root);//在这个print中运行的是带参数的print,细心!!!
        }
        public void InOrder(TreeNode Root)
        {
            if (Root == null)
            {
                return;
                ;
            }
            else
            {
                {
                    InOrder(Root.Left);
                    // Console.WriteLine(Root.Data);
                    Console.WriteLine(Root.number);
                    InOrder(Root.Right);
                }
            }
        }



        public void Print2()
        {
            PostOrder(Root);//在这个print中运行的是带参数的print,细心!!!
        }
        //后序遍历
        public void PostOrder(TreeNode Root)
        {
            if (Root == null)
            {
                return;
            }
            else
            {
                PostOrder(Root.Left);
                PostOrder(Root.Right);
                // Console.WriteLine(Root.Data);
                Console.WriteLine(Root.number);
            }
        }

        public void Print()
        {
            Print(Root);//在这个print中运行的是带参数的print,细心!!!
        }




        public void PrintLevelOrder()//层序遍历
        {
            int i;
            int j;
            int flag;
            flag = 0;
            i = 0;
            j = 0;
            List<TreeNode> Data = new List<TreeNode>();//列表形元素
            Data.Add(Root);//在其中新增元素
            while (Data.Count != 0)
            {
                TreeNode temp = Data[0];//tmp取代了data[0]
                Data.RemoveAt(0);//删除data[0]的意思
                Console.WriteLine($"编号为{temp.number%100}的元素优先度为{temp.number/10}");
                if (temp.Left != null)
                {
                    Data.Add(temp.Left);
                    i = i + 1;
                }
                j = j + 1;
                if (temp.Right != null)
                {
                    Data.Add(temp.Right);
                    i = i + 1;
                }
                j = j + 1;
                if ((i != j) && ((i == 6)))
                {

                    flag = 1;
                }
            }
           
        }



        public void Search(int FindIt)
        {
            SearchIt(Root, FindIt);
        }

        private void SearchIt(TreeNode Root, int FindIt)
        {
            if (Root == null)
            {
                Console.WriteLine("Not Found!!");
                return;
            }


            else if (FindIt == Root.Data)
            {
                Console.WriteLine("Found!!");
                return;

            }
            else if (FindIt > Root.Data)
            {
                SearchIt(Root.Right, FindIt);
            }
            else if (FindIt < Root.Data)
            {
                SearchIt(Root.Left, FindIt);
            }
            return;

        }
    }

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值