• 二叉树的实现


    代码
    /// <summary>
        
    /// 二叉树的应用
        
    /// </summary>
        
    /// <typeparam name="T"></typeparam>
        public class Node<T>
        {
            
    public T m_Item;
            
    public Node<T> m_LeftNode;
            
    public Node<T> m_RightNode;
            
    public Node(T item,Node<T> leftNode,Node<T> rightNode)
            {
                
    this.m_Item = item;
                
    this.m_LeftNode = leftNode;
                
    this.m_RightNode = rightNode;
            }

        }
        
    public class Tree<T>
        {
            Node
    <T> root;
            
    public Tree(Node<T> root)
            {
                
    this.root = root;
            }
            
    public IEnumerable<T> GetEnum
            {
                
    get
                {
                    
    return DisplayTree(root);
                }
            }
            
    public IEnumerable<T> DisplayTree(Node<T> root)
            {
                
    //遍历左子树
                if (root.m_LeftNode != null)
                    
    foreach (T item in DisplayTree(root.m_LeftNode))
                        
    yield return item;
                
    //遍历根节点
                yield return root.m_Item;
                
    //遍历右子树
                if (root.m_RightNode != null)
                    
    foreach (T item in DisplayTree(root.m_RightNode))
                        
    yield return item;
            }
        }
        
    public class CreateTree
        {
            
    public Tree<string> tree;
            
    public CreateTree()
            {
                tree 
    = new Tree<string>(new Node<string>("A",
                      
    new Node<string>("B"nullnull),
                      
    new Node<string>("C",
                        
    new Node<string>("D"nullnull),
                        
    new Node<string>("E"nullnull))));

            }
            
    public  void Display()
            {
               
                
    foreach (string s in tree.GetEnum)
                    Console.WriteLine(s);
            }

        }
  • 相关阅读:
    Discuz 网站移至 Ubuntu 14.04.4 LTS VPS 配置
    ubuntu的一些常用命令,测试版本:Ubuntu 12.04.5 LTS
    文本的水平对齐方式,带来的一系列问题
    庆祝我的Blog开张了,今天起和大家一起见证一款原生的富文本编辑组件FoxJsonRichView的诞生过程。
    竖直导航(一)
    测试
    第七次作业
    第六次作业
    第四次作业
    第五次作业
  • 原文地址:https://www.cnblogs.com/hubcarl/p/1706406.html
Copyright © 2020-2023  润新知