二叉树遍历算法实现(C#2.0)

二叉树遍历算法实现(C#2.0)

Posted on 2006-09-20 17:42 xuanfeng 阅读(828) 评论(8)   编辑  收藏 引用 收藏至365Key 509897.html?webview=1
 

本人用C#2.0实现了二叉树的定义,怎么构造一颗已知的二叉树,用几种常规的算法(先序,中序,后序,层次)遍历二叉树。希望能给有需要人带来帮助,也希望能得到大家的指点。有关C#数据结构的书在书店里找到,网上也是极少,如果你有好的学习资源别忘了告诉我。先谢了。数据结构对一个程序员来说,现在是太重要了,数据结构学得好的人,逻辑思维一定很强,在程序设计的时候,就不会觉得太费劲了。而且是在设计多层应用程序的时候,真是让人绞尽脑汁啊。趁自己还年轻,赶紧练练脑子。哈哈,咱们尽快进入主题吧。

   本程序中将用到一棵已知的二叉树如图(二叉树图)所示。



 

 下面简单介绍一下几种算法和思路:

先序遍历:

1.       访问根结点

2.       按先序遍历左子树;

3.       按先序遍历右子树;

4.       例如:遍历已知二叉树结果为:A->B->D->G->H->C->E->F

中序遍历:

1.       按中序遍历左子树;

2.       访问根结点;

3.       按中序遍历右子树;

4.       例如遍历已知二叉树的结果:B->G->D->H->A->E->C->F

后序遍历:

1.       按后序遍历左子树;

2.       按后序遍历右子树;

3.       访问根结点;

4.       例如遍历已知二叉树的结果:G->H->D->B->E->F->C->A

层次遍历:

1.       从上到下,从左到右遍历二叉树的各个结点(实现时需要借辅助容器);

2.       例如遍历已知二叉树的结果:A->B->C->D->E->F->G->H

附加整个解决方案代码:

ContractedBlock.gif ExpandedBlockStart.gif 二叉遍历算法解决方案
None.gifusing System;
None.gif
using System.Collections.Generic;
None.gif
using System.Text;
ExpandedBlockStart.gifContractedBlock.gif
/**//*
InBlock.gif 作者:旋风
InBlock.gif 日期:2006/9/20
InBlock.gif 博客园主页:xuanfeng.cnblogs.com
InBlock.gif 
ExpandedBlockEnd.gif 
*/

None.gif
None.gif
namespace structure
ExpandedBlockStart.gifContractedBlock.gif
dot.gif{
InBlock.gif    
class Program
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
ContractedSubBlock.gifExpandedSubBlockStart.gif        
二叉树结点数据结构的定义#region 二叉树结点数据结构的定义 
InBlock.gif        
//二叉树结点数据结构包括数据域,左右结点以及父结点成员;
InBlock.gif
      class nodes<T>
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            T data;
InBlock.gif            nodes
<T> Lnode, Rnode, Pnode;
InBlock.gif            
public T Data
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif                
set dot.gif{ data = value; }
ExpandedSubBlockStart.gifContractedSubBlock.gif                
get dot.gifreturn data; }
InBlock.gif
ExpandedSubBlockEnd.gif            }

InBlock.gif            
public nodes<T> LNode
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif                
set dot.gif{ Lnode = value; }
ExpandedSubBlockStart.gifContractedSubBlock.gif                
get dot.gifreturn Lnode; }
ExpandedSubBlockEnd.gif            }

InBlock.gif            
public nodes<T> RNode
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif                
set dot.gif{ Rnode = value; }
ExpandedSubBlockStart.gifContractedSubBlock.gif                
get dot.gifreturn Rnode; }
InBlock.gif
ExpandedSubBlockEnd.gif            }

InBlock.gif
InBlock.gif            
public nodes<T> PNode
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif                
set dot.gif{ Pnode = value; }
ExpandedSubBlockStart.gifContractedSubBlock.gif                
get dot.gifreturn Pnode; }
InBlock.gif
ExpandedSubBlockEnd.gif            }

InBlock.gif          
public nodes()
ExpandedSubBlockStart.gifContractedSubBlock.gif          
dot.gif{ }
InBlock.gif          
public nodes(T data)
ExpandedSubBlockStart.gifContractedSubBlock.gif          
dot.gif{
InBlock.gif              
this.data = data;
ExpandedSubBlockEnd.gif          }

InBlock.gif
ExpandedSubBlockEnd.gif        }
 
ExpandedSubBlockEnd.gif        
#endregion

InBlock.gif
ContractedSubBlock.gifExpandedSubBlockStart.gif        
先序编历二叉树#region 先序编历二叉树
InBlock.gif        
static void PreOrder<T>(nodes<T> rootNode)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
if (rootNode != null)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                Console.WriteLine(rootNode.Data);
InBlock.gif                PreOrder
<T>(rootNode.LNode);
InBlock.gif                PreOrder
<T>(rootNode.RNode);
InBlock.gif
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif        
ExpandedSubBlockEnd.gif        
#endregion

InBlock.gif
ContractedSubBlock.gifExpandedSubBlockStart.gif        
构造一棵已知的二叉树#region 构造一棵已知的二叉树
InBlock.gif
InBlock.gif        
static nodes<string> BinTree()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            nodes
<string>[] binTree = new nodes<string>[8];
InBlock.gif            
//创建结点
InBlock.gif
            binTree[0= new nodes<string>("A");
InBlock.gif            binTree[
1= new nodes<string>("B");
InBlock.gif            binTree[
2= new nodes<string>("C");
InBlock.gif            binTree[
3= new nodes<string>("D");
InBlock.gif            binTree[
4= new nodes<string>("E");
InBlock.gif            binTree[
5= new nodes<string>("F");
InBlock.gif            binTree[
6= new nodes<string>("G");
InBlock.gif            binTree[
7= new nodes<string>("H");
InBlock.gif            
//使用层次遍历二叉树的思想,构造一个已知的二叉树
InBlock.gif

InBlock.gif            binTree[
0].LNode = binTree[1];
InBlock.gif            binTree[
0].RNode = binTree[2];
InBlock.gif            binTree[
1].RNode = binTree[3];
InBlock.gif            binTree[
2].LNode = binTree[4];
InBlock.gif            binTree[
2].RNode = binTree[5];
InBlock.gif            binTree[
3].LNode = binTree[6];
InBlock.gif            binTree[
3].RNode = binTree[7];
InBlock.gif            
//返回二叉树的根结点
InBlock.gif
            return binTree[0];
InBlock.gif
InBlock.gif
InBlock.gif
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif        
#endregion

InBlock.gif
ContractedSubBlock.gifExpandedSubBlockStart.gif        
中序遍历二叉树#region 中序遍历二叉树
InBlock.gif        
static void MidOrder<T>(nodes<T> rootNode)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
if (rootNode != null)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                MidOrder
<T>(rootNode.LNode);
InBlock.gif                Console.WriteLine(rootNode.Data);
InBlock.gif                MidOrder
<T>(rootNode.RNode);
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }
 
ExpandedSubBlockEnd.gif        
#endregion

ContractedSubBlock.gifExpandedSubBlockStart.gif        
后序遍历二叉树#region 后序遍历二叉树
InBlock.gif        
static void AfterOrder<T>(nodes<T> rootNode)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
if (rootNode != null)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                AfterOrder
<T>(rootNode.LNode);
InBlock.gif                AfterOrder
<T>(rootNode.RNode);
InBlock.gif                Console.WriteLine(rootNode.Data);
ExpandedSubBlockEnd.gif            }

InBlock.gif
ExpandedSubBlockEnd.gif        }
 
ExpandedSubBlockEnd.gif        
#endregion

InBlock.gif
ContractedSubBlock.gifExpandedSubBlockStart.gif        
层次遍历二叉树#region 层次遍历二叉树
InBlock.gif        
static void LayerOrder<T>(nodes<T> rootNode)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            nodes
<T>[] Nodes = new nodes<T>[20];
InBlock.gif            
int front = -1;
InBlock.gif            
int rear = -1;
InBlock.gif            
if (rootNode != null)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                rear
++;
InBlock.gif                Nodes[rear] 
= rootNode;
InBlock.gif
ExpandedSubBlockEnd.gif            }

InBlock.gif
InBlock.gif            
while (front != rear)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                front
++;
InBlock.gif                rootNode 
= Nodes[front];
InBlock.gif                Console.WriteLine(rootNode.Data);
InBlock.gif                
if (rootNode.LNode != null)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    rear
++;
InBlock.gif                    Nodes[rear] 
= rootNode.LNode;
ExpandedSubBlockEnd.gif                }

InBlock.gif                
if (rootNode.RNode != null)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    rear
++;
InBlock.gif                    Nodes[rear] 
= rootNode.RNode;
ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif        
ExpandedSubBlockEnd.gif        
#endregion

InBlock.gif
ContractedSubBlock.gifExpandedSubBlockStart.gif        
测试的主方法#region 测试的主方法
InBlock.gif        
static void Main(string[] args)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            nodes
<string> rootNode = BinTree();
InBlock.gif
InBlock.gif            Console.WriteLine(
"先序遍历方法遍历二叉树:");
InBlock.gif            PreOrder
<string>(rootNode);
InBlock.gif           
InBlock.gif            Console.WriteLine(
"中序遍历方法遍历二叉树:");
InBlock.gif            MidOrder
<string>(rootNode);
InBlock.gif            
InBlock.gif            Console.WriteLine(
"后序遍历方法遍历二叉树:");
InBlock.gif            AfterOrder
<string>(rootNode);
InBlock.gif
InBlock.gif
InBlock.gif            Console.WriteLine(
"层次遍历方法遍历二叉树:");
InBlock.gif            LayerOrder
<string>(rootNode);
InBlock.gif
InBlock.gif
InBlock.gif            Console.Read();
InBlock.gif
ExpandedSubBlockEnd.gif        }
 
ExpandedSubBlockEnd.gif        
#endregion

ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

None.gif

转载于:https://www.cnblogs.com/xiongeee/archive/2006/09/23/512443.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值