二叉树的实现- 蓝天_白云- 博客园
代码
/// <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);
        }

    }

 

郑重声明:资讯 【二叉树的实现- 蓝天_白云- 博客园】由 发布,版权归原作者及其所在单位,其原创性以及文中陈述文字和内容未经(企业库qiyeku.com)证实,请读者仅作参考,并请自行核实相关内容。若本文有侵犯到您的版权, 请你提供相关证明及申请并与我们联系(qiyeku # qq.com)或【在线投诉】,我们审核后将会尽快处理。
—— 相关资讯 ——