zoukankan      html  css  js  c++  java
  • 二叉树的实现

    代码
    /// <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);
            }

        }
  • 相关阅读:
    实例、数据库和表空间(转载)
    异步邮件阻塞MVC请求
    发布Asp.Net MVC 注意事项
    CWebBrowser2 图片缩放,点击小图看大图
    Web服务器与客户端时差问题
    一些好用的eclipse插件
    ASP.NET Deployment and Configuration Problems
    第二届游戏开发者大会
    MVC 请求参数中带有HTML会引发Validation异常 ("A potentially dangerous Request.Form value was detected from the client")
    网络语言标准实施规范 ISO2009
  • 原文地址:https://www.cnblogs.com/hubcarl/p/1706406.html
Copyright © 2011-2022 走看看