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);
            }

        }
  • 相关阅读:
    Android自定义drawable(Shape)详解
    如何设置对话框的宽度和高度
    Android资料之-EditText中的inputType
    android4.0 禁止横竖屏切换使用 android:configChanges="orientation|keyboardHidden"无效的解决方法
    android ScrollView 充满屏幕
    治疗神经衰弱最有效的方法和药物是什么
    交换机和路由器的区别
    小众编程语言同样值得你关注
    RotateAnimation详解
    你可能没听过的 Java 8 中的 10 个特性
  • 原文地址:https://www.cnblogs.com/hubcarl/p/1706406.html
Copyright © 2011-2022 走看看