数据结构与算法(C#实现)..pdf
- 文档编号:30838918
- 上传时间:2024-01-30
- 格式:PDF
- 页数:67
- 大小:3.69MB
数据结构与算法(C#实现)..pdf
《数据结构与算法(C#实现)..pdf》由会员分享,可在线阅读,更多相关《数据结构与算法(C#实现)..pdf(67页珍藏版)》请在冰豆网上搜索。
数据结构与算法(C#实现)系列前言Heavenkiller(原创)搞计算机的人都应该很清楚,语言只是一种工具,算法才是灵魂。
现在的开发语言有很多,如C+,VB,Perl,java,c#,还有如脚本语言js,vbs等,在如此多的选择面前,很多人不知道该选择哪一种好。
其实不管哪一种语言,既然他存在,就一定有他的价值,有它的特定用途,而这往往是其它语言所无法比拟的。
譬如C+就适合于系统底层的编程,而java一般就用于对稳定性,兼容性要求较高的场合,正所谓各有所长。
像我一般用C+编写网络基层和与操作系统相关的程序,用C#写ASP.NET等程序,必要的时候再辅以Rose,RationalXDE等建模工具。
但无论选择哪一种语言,算法才是根本,掌握了算法,就掌握了所有语言的根本,以不变应万变。
微软的C#是一种全新的语言,利用它能快捷、高效地布署程序。
现在关于C#的资料也己经有很多了,各个方面的资料都能找得到,但用C#做数据结构的似乎还没有什么,在CSDN上我只找到了三四篇,而且仅仅是讲了一下链表之类简单的数据结构。
于是我利用空闲的时间用C#写了一些数据结构与算法的实现,希望对大家学习数据结构能够有所帮助。
另外,由于时间仓促,难免出现一些维漏,希望大家不吝赐教给予指正,我的email是heavenkiller2002yahoo.com.cn.欢迎大家和我一起交流学习。
本系列包括树,N叉树,广义树,二叉树,BST二叉查找树,AVL平衡树,堆,二叉堆,以及图。
还有一些如哈希表,散列,左翼树,二项树,Haffman编码树等因时间关系,暂时未能奉上,以后有时间再补上吧。
首先给大家展示一幅用RationalXDEfor.NET生成的类模型图,让大家对所有的类有一个大概的了解。
数据结构与算法(C#实现)系列演示篇(-)HeavenkiIler(原创)这一篇主要是针对以后各篇的数据类型进行一个实质性的演示。
因此希望大家具体看了各种数据结构的分析之后再看这篇。
主要包括如下几个方面的演示:
1.堆栈。
演示了一个利用堆栈作的RPN计算器2.排序表。
演示了一个利用排序表做的多项式表达式的加法运算3.广义树。
演示了深度遍历和广度遍历4.N叉树。
演示了N叉树的生成插入删除等基本操作5.表达式树。
演示了一个用二叉树和堆栈做的可以将一个后缀表达式翻译为日常中熟悉的中缀表达式的例子6.AVL树。
演示了基本操作usingSystem;usingSystem.Collections;namespaceDataStructure/Classi的摘要说明。
IIIclassShow(III/应用程序的主入口点。
/STAThreadstaticvoidMain(stringargs)(/TODO:
在此处添加代码以启动应用程序/while(true)(Console.WriteLineCpleasechooseatheNo.ofaitemyouwanttoperform:
z,);Console.WriteLine(?
/l.Stack-RPNCalCulator,z);Console.WriteLine(,z2.SortedList-theadditionofpolynomialrealizedbysortedlist);Console.WriteLineCS.GeneralTree-depthtravesalandbreathtraval);Console.WriteLine(4.NaryTree,?
);Console.WriteLine(5.ExpressionTree,z);Console.WriteLine(6.AVLTreez,);Console.WriteLine(,z7.BinaryHeap,z);Console.WriteLine(z,exit-Exitthisprogramme7);/Test();switch(Console.ReadLine()(casel:
ShowStackShowStackRPNCalCulatorO;break;case2:
SortedListShowSortedList_Polynomial();break;case3:
ShowGeneralTree_travel();break;“case4:
ShowNaryTree();演示一个三叉树的Attach和Detachbreak;case5:
ShowExpressionTree();break;case6:
ShowAVLTreeO;break;case7:
ShowBinarylleap();break;caseexit”:
return;default:
break;)publicstaticvoidShowBinaryHeap()(构造一个二叉堆,包含2,4,6,8,10,12Binarylleapblleap=newBinarylleap(10);bHeap.Enqueue(12);blleap.Enqueue(10);bHeap.Enqueue(8);bHeap.Enqueue(6);bHeap.Enqueue(4);bHeap.Enqueue
(2);测试Dequeue();while(blleap.Count!
=0)(Console.WriteLine(bHeap.DequeueMin().ToString();publicstaticvoidShowAVLTree()(AVLTreetestAVL=newAVLTree(5);testAVL.Insert
(1);testAVL.Insert(3);testAVL.Insert(7);testAVL.Insert(8);testAVL.Insert(9);testAVL.Insert(10);testAVL.Insert(11);PrintVisitorvis=newPrintVisitor();Tree.InOrderinVis=newDataStructure.Tree.InOrder(vis);testAVL.DepthFirstTraversal(inVis);publicstaticvoidShowExpressionTree()ExpressionTree.PostfixToInfix();publicstaticvoidShowNaryTree()构造一个三叉树,具体见图1-2NaryTreeA=newNaryTree(3,/ZA,Z);NaryTreeB=newNaryTree(3,B);NaryTreeC=newNaryTree(3,C);NaryTreeD=newNaryTree(3,D);NaryTreeE=newNaryTree(3,E);B.AttachSubtree(l,D);B.AttachSubtree(2,E);A.AttachSubtreed,B);A.AttachSubtree(3,C);/-Console.WriteLine(广度遍历”);PrintVisitorvis=newPrintVisitor();A.BreadthFirstTraversal(vis);广度遍历Console.WriteLine(前序遍历);Tree.PreOrderpreVisit=newDataStructure.Tree.PreOrder(vis);A.DepthFirstTraversal(preVisit);Console.WriteLine(后序遍历”);Tree.PostOrderpostVisit=newDataStructure.Tree.PostOrder(vis);A.DepthFirstTraversal(postVisit);Console.WriteLine(中序遍历);Tree.InOrderinVisit=newDataStructure.Tree.InOrder(vis);A.DepthFirstTraversal(inVisit);数据结构与算法(C#实现)系列演示篇
(二)Heavenkiller(原创)publicstaticvoidShowGeneralTree_travel()(lEnumeratortmpIEnum;Tree.TraversalTypetravelType=O;/-提示-Console.WriteLine(,pleasechooseatheNo.ofaitemyouwanttotravel:
M);Console.WriteLine(l.BreadthFirst-广度遍历”);Console.WriteLine(M2.PreDepthFirst-前序遍历”);Console.WriteLine(n3.InDepthFirst中序遍历);Console.WriteLine(n4.PostDepthFirst-后序遍历)switch(Console.ReadLine()(casel:
ShowStacktravelType=Tree.TraversalType.Breadth;Console.WriteLine(广度遍历”);break;case,2,:
/SortedListtravelType=Tree.TraversalType.PreDepth;Console.WriteLine(前序遍历);break;case“3:
travelType=Tree.Traversa!
Type.InDepth;Console.WriteLine(u,11序遍历);break;case”4:
travelType=Tree.TraversalType.PostDepth;Console.WriteLine(后序遍历);break;default:
break;)构造一棵广义树generaltreeGeneralTreeA=newGeneralTree(A);GeneralTreeB=newGeneralTree(Bn);GeneralTreeC=newGeneralTreeCC*);GeneralTreeD=newGeneralTree(D);GeneralTreeE=newGeneralTree(,E,);GeneralTreeF=newGeneralTree(nFn);A.AttackSubtree(B);A.AttackSubtree(C);B.AttackSubtree(D);B.AttackSubtree(E);A.Attacksubtree(F);/showtheoperationConsole.WriteLine(nA.AttackSubtree(B)H);Console.WriteLine(,A.AttackSubtree(C)H);Console.WriteLine(nB.AttackSubtree(D)H);Console.WriteLine(uB.Attacksubtree(E)n);Console.WriteLine(A.AttackSubtree(F);/-A.SetTraversalType(trave】Type);设置遍历类型tmpIEnum=A.GetEnumerator();/Console.WriteLine(begintodepthfisttravel:
n);while(tmpIEnum.MoveNext()Console.WriteLine(tmpIEnum.Current.ToStringO);)publicstaticvoidShowStack_RPNCalCulator()/readaexpressionstringandpusheverycharacterintothestackinqueue.Console.WriteLineCthisisperformanceforstack,youcaninputastringlikethis123*+,thenthissubprogrammecancomputeitandgettheresult7,thisisRPNcalculator.n);Console.WriteLine(pleaseinputaexpressionstring:
*);stringstrExpression=Console.ReadLine();chartmpChars=strExpression.ToCharArray(0,strExpression.Length);StackstackRPN=newStack();intnumA,numB;foreach(chartmpintmpChars)(switch(tmp)(case*:
numA=(int)stackRPN.Pop();numB=(int)stackRPN.Pop();stackRPN.Push(numA*numB);break;casenumA=(int)stackRPN.Pop();numB=(int)stackRPN.Pop();stackRPN.Push(numA+numB);break;default:
stackRPN.Push(Int32.Parse(tmp.ToString();break;)Console.WriteLine(theresultis:
0n,stackRPN.Pop().ToString();数据结构与算法(C#实现)系列一演示篇(三)Heavenki1ler(原创)publicstaticvoidShowSortedList_Polynomial()(100+10*x+x八2+1+10*x+100xA2SortedListtmpListA=newSortedList();SortedListtmpListB=newSortedList();SortedListtmpListC=newSortedList();/usedtostoretheresultSortedListtmpKeyList=newSortedList();/usedtostoreallkeysoftwopolynomials/initpolynomialAandshowittmpListA.Add(0,100);tmpListA.Add(1,10);tmpListA.Add(2,1);ShowSortedList_ShowPolynomial(ntmpListAn,tmpListA.GetEnumerator();/initpolynomialBandshowittmpListB.Add(0,1);tmpListB.Add(l,10);tmpListB.Add(2,l00);ShowSortedList_ShowPolynomial(,tmpListB,tmpListB.GetEnumerator();/initthekeylistwhichcontainsallkeysofAandBbuteveryoneonceIDictionaryEnumeratortmpIDic=tmpListA.GetEnumerator();while(tmpIDic.MoveNext()!
=false)(if(!
tmpKeyList.ContainsKey(tmpIDic.Key)(tmpKeyList.Add(tmplDic.Key,null);)tmpIDic=tmpListB.GetEnumerator();while(tmpIDic.MoveNext()!
=false)(if(!
tmpKeyList.ContainsKey(tmpIDic.Key)tmpKeyList.Add(tmpIDic.Key,nuH);)/AddAandBandshowtheresulttmpIDic=tmpKeyList.GetEnumerator();while(tmpIDic.MoveNext()!
=false)objectobjA=null,objB=null,objC=null;objC=tmpIDic.Key;if(tmpListA.ContainsKey(objC)objA=tmpListAobjC;if(tmpListA.ContainsKey(objC)objB=tmpListBobjC;/objC=objA+objB;/tmpKeyListobjC=(int)objA+(int)objC;tmpListC.Add(objC,(int)objA+(int)objB);ShowSortedLiscShowPolynomialCtheadditionresultofAandBtmpListC.GetEnumeratorO);)publicstaticvoidShowSortedList_ShowPolynomial(stringtipJDictionaryEnumeratoriDic)stringstrExpress=null;iDic.Reset();while(iDic.MoveNext()!
=false)(strExpress+=iDic.Value.ToString()+*XA,,+iDic.Key.ToString()+;)Console.WriteLine(tip+:
n+strExpress);数据结构与算法(C#实现)系列一树
(一)HeavenkiHer(原创)首先我们给树下一个定义:
树是一个有限的、非空的结点集,T=rorTlorT2or.orTn它具有下列性质:
1.集合指定的结点r叫做树的根结点2.其余的结点可以划分成n个子集,T1,T2,Tn(n=0),其中每一个子集都是一棵树。
树的其它定义如度,叶子,高等就请大家查阅别的资料吧,到处都有的。
树的主要性质一个就是遍历,分为深度遍历和广度遍历在这里分别实现为DepthFirstTravesal。
和WidthFirstTravesal()其中深度遍历又分为前序遍历、中序遍历、和后序遍历这是是采用适配器技术实现的。
usingSystem;usingSystem.Collections;namespaceDataStructure/Tree的摘要说明。
IIIwhentraverse,traversaltypecantbechanged,orthrowaexceptionIII支持枚举、比较、深度复制/publicabstractclassTree:
IEnumerable,IComparablepublicTree()/TODO:
在此处添加构造函数逻辑/)protectedQueuekeyqueue=newQueue。
;/仅仅用于枚举时存放数据,不参与Equals实现中的比较protectedTraversalTypetraversaltype=TraversalType.Breadth;/chooseatraversaltype,andDepthFirstisdefault/protecteduintdegree=O;/degreeofthetree,inititas0/protecteduintheight=O;/heightofthetree,inititas0/枚举不同的遍历类型publicenumTraversalType(Breadth=1,广度遍历PreDepth=2前序遍历InDepth=3,中序遍历PostDepth=4后序遍历);/publicvirtualabstractobjectKeypublicabstractTreethisuint_indexget;set;/ifIonlyuseget,canIchangeitlater?
publicabstractobjectKeyget;publicabstractuintDegreeget
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 数据结构 算法 C# 实现