zoukankan      html  css  js  c++  java
  • C#泛型

    一构造二叉树 读取二叉树的值

    新建工程类库:命名BinaryTree

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    namespace BinaryTree
    {
        //where表示TItem类型实现接口IComparable
        public class Tree<TItem> where TItem:IComparable<TItem>
        {
           //属性
            public TItem DataNode { get; set; }
            public Tree<TItem> LeftNode { get; set; }
            public Tree<TItem> RightNode { get; set; }
            //构造函数
            public Tree(TItem nodevalue)
            {   
                this.DataNode = nodevalue;//作为树根节点的值
                this.LeftNode = null;
                this.RightNode = null;
            }
            //向树中插入节点
            public void Insert(TItem newitem)
            {
                //把独立树的节点值赋值给当前节点
                TItem currentNodeValue = this.DataNode;
                //currentNodeValue与newitem比较 大于0插入左边树,小于0插入右边树
                if (currentNodeValue.CompareTo(newitem) > 0)
                {
                    //左节点是否为空
                    if (this.LeftNode == null)
                    {
                        //创建左节点
                        this.LeftNode = new Tree<TItem>(newitem);
                    }
                    else
                    {   //把当前左节点看作为独立树的根节点继续递归向下查找,直到没有左节点然后插入
                        this.LeftNode.Insert(newitem);
                    }
                }
                else 
                { 
                      //右节点是否为空
                    if (this.RightNode == null)
                    {
                        //创建右节点
                        this.RightNode = new Tree<TItem>(newitem);
                    }
                    else
                    { 
                        //把当前右节点看作独立树的根节点继续递归向下查找,直到没有右节点然后插入
                        this.RightNode.Insert(newitem);
                    }
                }
    
            }
            //读取树中的数据
            public void WalkTree()
            { 
                //是否存在左节点
                if (this.LeftNode != null)
                {
                    //递归继续读取
                    this.LeftNode.WalkTree();
                }
                Console.WriteLine(this.DataNode.ToString());
                //是否存在右节点
                if (this.RightNode != null)
                {   //递归继续读取
                    this.RightNode.WalkTree();
                }
            }
        }
    }

    在Main函数中测试:

     class Program
        {
            static void Main(string[] args)
            {
                //实例化
            Tree<char> tree = new Tree<char>('g');
                tree.Insert('a');
                tree.Insert('m');
                tree.Insert('b');
                tree.Insert('v');
                tree.WalkTree();
                Console.Read();
            }   
       }

    效果图:

    输出的数据已排好序

  • 相关阅读:
    头文件stdio与stdlib.h的区别
    宝塔利用git+ webhooks 实现git更新远程同步Linux服务器
    Linux源码安装步骤
    Promise.all和Promise.race区别,和使用场景
    vue显示富文本
    Js实现将html页面或div生成图片
    JS
    关于Swiper和vue数据顺序加载问题处理
    php 数据库备份(可用作定时任务)
    js async await 终极异步解决方案
  • 原文地址:https://www.cnblogs.com/zcttxs/p/2563447.html
Copyright © 2011-2022 走看看