C#来完成二叉树的搜索、遍历、及查找

二叉树结点类:
public   class  Node
    {
        
private  Node left;
        
private  Node right;
        
private   int  value;

        
public  Node(Node left, Node right,  int  value)
        {
            
this .left  =  left;
            
this .right  =  right;
            
this .value  =  value;
        }

        
public  Node( int  value)
        {
            
this .value  =  value;
        }

        
public  Node getLeftNode() {  return  left; }
        
public  Node getRightNode() {  return  right; }
        
public   int  getValue() {  return  value; }


        
///   <summary>
        
///  查找某一节点
        
///   </summary>
        
///   <param name="node"> 根节点 </param>
        
///   <param name="value"> 要查找的值 </param>
        
///   <returns></returns>
         public  Node findNode(Node root,  int  value)
        {
            
if  (root  ==   null )
            {
                
return   null ;
            }

            
if  (root.getValue()  ==  value)
            {
                
return  root;
            }

            
if  (value  <  root.getValue())
            {
                findNode(root.getLeftNode(), value);
            }
            
else
            {
                findNode(root.getRightNode(), value);
            }
            
return   null ;
        }

        
///   <summary>
        
///  先根遍历
        
///   </summary>
        
///   <param name="root"></param>
         public   static   void  PreOrder(Node root)
        {
            
if  (root  !=   null )
            {
                Console.WriteLine(root.getValue());

                PreOrder(root.getLeftNode());
                PreOrder(root.getRightNode());
            }
        }

        
///   <summary>
        
///  后根遍历
        
///   </summary>
        
///   <param name="root"></param>
         public   static   void  AfterOrder(Node root)
        {
            
if  (root  !=   null )
            {
                AfterOrder(root.getLeftNode());
                AfterOrder(root.getRightNode());
                Console.WriteLine(root.getValue());
            }
        }

        
///   <summary>
        
///  中序遍历二叉树
        
///   </summary>
        
///   <param name="root"></param>
         public   static   void  MidOrder(Node root)
        {
            
if  (root  !=   null )
            {
                MidOrder(root.getLeftNode());
                Console.WriteLine(root.getValue());
                MidOrder(root.getRightNode());
            }
        }

        
public   static   int  leafNodeCount  =   0 ;
        
public   static   void  count_leafNode(Node root)
        {
            
if  (root  ==   null )
            {
                
return ;
            }
            
if  (root.getLeftNode()  ==   null   &&  root.getRightNode()  ==   null )
            {
                Console.WriteLine(
" 叶子节点 "   +   leafNodeCount.ToString()  +   " 的值为: "   +    root.getValue());
                leafNodeCount
++ ;
            }
            
else
            {
                count_leafNode(root.getLeftNode());
                count_leafNode(root.getRightNode());
            }
        }
    }


为了简便起见,将遍历等相关方法都写在了该节点中。
以下是主程序调用:
public   void  showMain()
        {
// 构建二叉树
            Node[] node = new  Node[ 8 ];
            node[
4 =   new  Node( 5 );
            node[
5 =   new  Node( 6 );
            node[
6 =   new  Node( 7 );

            node[
3 =   new  Node(node[ 6 ], node[ 7 ],  3 );
            node[
2 =   new  Node(node[ 4 ], node[ 5 ],  2 );
            node[
1 =   new  Node(node[ 3 ],  null 2 );
            node[
0 =   new  Node(node[ 1 ], node[ 2 ],  0 );

            Console.WriteLine(
" 后根遍历: " );
            Node.AfterOrder(node[
0 ]);

            Console.WriteLine(
" 先根遍历: " );
            Node.PreOrder(node[
0 ]);

            Console.WriteLine(
" 中根遍历: " );
            Node.MidOrder(node[
0 ]);

            Console.WriteLine(
" 共有叶子节点个数为: " );
            Node.count_leafNode(node[
0 ]);
            Console.WriteLine(Node.leafNodeCount);

            Console.Read();

        }

【转载;来源:http://www.cnblogs.com/hanxianlong】

转载于:https://www.cnblogs.com/4kapple/archive/2008/06/13/1219397.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值