[zhuan]二叉树遍历算法实现(C#2.0)
本人用C#2.0實(shí)現(xiàn)了二叉樹(shù)的定義,怎么構(gòu)造一顆已知的二叉樹(shù),用幾種常規(guī)的算法(先序,中序,后序,層次)遍歷二叉樹(shù)。希望能給有需要人帶來(lái)幫助,也希望能得到大家的指點(diǎn)。有關(guān)C#數(shù)據(jù)結(jié)構(gòu)的書(shū)在書(shū)店里找到,網(wǎng)上也是極少,如果你有好的學(xué)習(xí)資源別忘了告訴我。先謝了。數(shù)據(jù)結(jié)構(gòu)對(duì)一個(gè)程序員來(lái)說(shuō),現(xiàn)在是太重要了,數(shù)據(jù)結(jié)構(gòu)學(xué)得好的人,邏輯思維一定很強(qiáng),在程序設(shè)計(jì)的時(shí)候,就不會(huì)覺(jué)得太費(fèi)勁了。而且是在設(shè)計(jì)多層應(yīng)用程序的時(shí)候,真是讓人絞盡腦汁啊。趁自己還年輕,趕緊練練腦子。哈哈,咱們盡快進(jìn)入主題吧。
???本程序中將用到一棵已知的二叉樹(shù)如圖(二叉樹(shù)圖)所示。
?
?下面簡(jiǎn)單介紹一下幾種算法和思路:
先序遍歷:
1.?????? 訪問(wèn)根結(jié)點(diǎn)
2.?????? 按先序遍歷左子樹(shù);
3.?????? 按先序遍歷右子樹(shù);
4.?????? 例如:遍歷已知二叉樹(shù)結(jié)果為:A->B->D->G->H->C->E->F
中序遍歷:
1.?????? 按中序遍歷左子樹(shù);
2.?????? 訪問(wèn)根結(jié)點(diǎn);
3.?????? 按中序遍歷右子樹(shù);
4.?????? 例如遍歷已知二叉樹(shù)的結(jié)果:B->G->D->H->A->E->C->F
后序遍歷:
1.?????? 按后序遍歷左子樹(shù);
2.?????? 按后序遍歷右子樹(shù);
3.?????? 訪問(wèn)根結(jié)點(diǎn);
4.?????? 例如遍歷已知二叉樹(shù)的結(jié)果:G->H->D->B->E->F->C->A
層次遍歷:
1.?????? 從上到下,從左到右遍歷二叉樹(shù)的各個(gè)結(jié)點(diǎn)(實(shí)現(xiàn)時(shí)需要借輔助容器);
2.?????? 例如遍歷已知二叉樹(shù)的結(jié)果:A->B->C->D->E->F->G->H
附加整個(gè)解決方案代碼:
using?System;
using?System.Collections.Generic;
using?System.Text;
/**//*
?作者:旋風(fēng)
?日期:2006/9/20
?博客園主頁(yè):xuanfeng.cnblogs.com
?
?*/
namespace?structure
{
????class?Program
????{
????????二叉樹(shù)結(jié)點(diǎn)數(shù)據(jù)結(jié)構(gòu)的定義#region?二叉樹(shù)結(jié)點(diǎn)數(shù)據(jù)結(jié)構(gòu)的定義?
????????//二叉樹(shù)結(jié)點(diǎn)數(shù)據(jù)結(jié)構(gòu)包括數(shù)據(jù)域,左右結(jié)點(diǎn)以及父結(jié)點(diǎn)成員;
??????class?nodes<T>
????????{
????????????T?data;
????????????nodes<T>?Lnode,?Rnode,?Pnode;
????????????public?T?Data
????????????{
????????????????set?{?data?=?value;?}
????????????????get?{?return?data;?}
????????????}
????????????public?nodes<T>?LNode
????????????{
????????????????set?{?Lnode?=?value;?}
????????????????get?{?return?Lnode;?}
????????????}
????????????public?nodes<T>?RNode
????????????{
????????????????set?{?Rnode?=?value;?}
????????????????get?{?return?Rnode;?}
????????????}
????????????public?nodes<T>?PNode
????????????{
????????????????set?{?Pnode?=?value;?}
????????????????get?{?return?Pnode;?}
????????????}
??????????public?nodes()
??????????{?}
??????????public?nodes(T?data)
??????????{
??????????????this.data?=?data;
??????????}
????????}?
????????#endregion
????????先序編歷二叉樹(shù)#region?先序編歷二叉樹(shù)
????????static?void?PreOrder<T>(nodes<T>?rootNode)
????????{
????????????if?(rootNode?!=?null)
????????????{
????????????????Console.WriteLine(rootNode.Data);
????????????????PreOrder<T>(rootNode.LNode);
????????????????PreOrder<T>(rootNode.RNode);
????????????}
????????}
????????
????????#endregion
????????構(gòu)造一棵已知的二叉樹(shù)#region?構(gòu)造一棵已知的二叉樹(shù)
????????static?nodes<string>?BinTree()
????????{
????????????nodes<string>[]?binTree?=?new?nodes<string>[8];
????????????//創(chuàng)建結(jié)點(diǎn)
????????????binTree[0]?=?new?nodes<string>("A");
????????????binTree[1]?=?new?nodes<string>("B");
????????????binTree[2]?=?new?nodes<string>("C");
????????????binTree[3]?=?new?nodes<string>("D");
????????????binTree[4]?=?new?nodes<string>("E");
????????????binTree[5]?=?new?nodes<string>("F");
????????????binTree[6]?=?new?nodes<string>("G");
????????????binTree[7]?=?new?nodes<string>("H");
????????????//使用層次遍歷二叉樹(shù)的思想,構(gòu)造一個(gè)已知的二叉樹(shù)
????????????binTree[0].LNode?=?binTree[1];
????????????binTree[0].RNode?=?binTree[2];
????????????binTree[1].RNode?=?binTree[3];
????????????binTree[2].LNode?=?binTree[4];
????????????binTree[2].RNode?=?binTree[5];
????????????binTree[3].LNode?=?binTree[6];
????????????binTree[3].RNode?=?binTree[7];
????????????//返回二叉樹(shù)的根結(jié)點(diǎn)
????????????return?binTree[0];
????????}
????????#endregion
????????中序遍歷二叉樹(shù)#region?中序遍歷二叉樹(shù)
????????static?void?MidOrder<T>(nodes<T>?rootNode)
????????{
????????????if?(rootNode?!=?null)
????????????{
????????????????MidOrder<T>(rootNode.LNode);
????????????????Console.WriteLine(rootNode.Data);
????????????????MidOrder<T>(rootNode.RNode);
????????????}
????????}?
????????#endregion
????????后序遍歷二叉樹(shù)#region?后序遍歷二叉樹(shù)
????????static?void?AfterOrder<T>(nodes<T>?rootNode)
????????{
????????????if?(rootNode?!=?null)
????????????{
????????????????AfterOrder<T>(rootNode.LNode);
????????????????AfterOrder<T>(rootNode.RNode);
????????????????Console.WriteLine(rootNode.Data);
????????????}
????????}?
????????#endregion
????????層次遍歷二叉樹(shù)#region?層次遍歷二叉樹(shù)
????????static?void?LayerOrder<T>(nodes<T>?rootNode)
????????{
????????????nodes<T>[]?Nodes?=?new?nodes<T>[20];
????????????int?front?=?-1;
????????????int?rear?=?-1;
????????????if?(rootNode?!=?null)
????????????{
????????????????rear++;
????????????????Nodes[rear]?=?rootNode;
????????????}
????????????while?(front?!=?rear)
????????????{
????????????????front++;
????????????????rootNode?=?Nodes[front];
????????????????Console.WriteLine(rootNode.Data);
????????????????if?(rootNode.LNode?!=?null)
????????????????{
????????????????????rear++;
????????????????????Nodes[rear]?=?rootNode.LNode;
????????????????}
????????????????if?(rootNode.RNode?!=?null)
????????????????{
????????????????????rear++;
????????????????????Nodes[rear]?=?rootNode.RNode;
????????????????}
????????????}
????????}
????????
????????#endregion
????????測(cè)試的主方法#region?測(cè)試的主方法
????????static?void?Main(string[]?args)
????????{
????????????nodes<string>?rootNode?=?BinTree();
????????????Console.WriteLine("先序遍歷方法遍歷二叉樹(shù):");
????????????PreOrder<string>(rootNode);
???????????
????????????Console.WriteLine("中序遍歷方法遍歷二叉樹(shù):");
????????????MidOrder<string>(rootNode);
????????????
????????????Console.WriteLine("后序遍歷方法遍歷二叉樹(shù):");
????????????AfterOrder<string>(rootNode);
????????????Console.WriteLine("層次遍歷方法遍歷二叉樹(shù):");
????????????LayerOrder<string>(rootNode);
????????????Console.Read();
????????}?
????????#endregion
????}
}
推薦看另一篇隨筆:線索二叉樹(shù)(C# 2.0)
http://www.cnblogs.com/xuanfeng/archive/2006/09/29/518493.html zhuanzi:http://www.cnblogs.com/xuanfeng/archive/2006/09/20/509897.html
轉(zhuǎn)載于:https://www.cnblogs.com/lifuyun/archive/2009/09/18/lifuyun09091826.html
總結(jié)
以上是生活随笔為你收集整理的[zhuan]二叉树遍历算法实现(C#2.0)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: DHCP Client 无法启动 拒绝访
- 下一篇: Oracle监听器Server端与Cli