namespace DataStructure
{
/// <summary>
/// Class1 的摘要说明。
/// </summary>
class Show
{
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main(string[] args)
{
//
// TODO: 在此处添加代码以启动应用程序
//
while(true)
{
Console.WriteLine("please choose a the No. of a item you want to perform:");
Console.WriteLine("1.Stack----- RPNCalCulator");
Console.WriteLine("2.SortedList-----the addition of polynomial realized by sortedlist ");
Console.WriteLine("3.GeneralTree----depthtravesal and breathtraval");
Console.WriteLine("4.NaryTree");
Console.WriteLine("5.ExpressionTree");
Console.WriteLine("6.AVLTree");
Console.WriteLine("7.BinaryHeap");
Console.WriteLine("exit--Exit this programme");
//Test();
switch(Console.ReadLine())
{
case "1"://Show Stack
ShowStack_RPNCalCulator();
break;
case "2"://SortedList
ShowSortedList_Polynomial();
break;
case "3":
ShowGeneralTree_travel();
break;
case "4":
ShowNaryTree();//演示一个三叉树的Attach和Detach
break;
case "5":
ShowExpressionTree();
break;
case "6":
ShowAVLTree();
break;
case "7":
ShowBinaryHeap();
break;
case "exit":
return;
default:
break;
}
}
//---------------------提示----------------------------
Console.WriteLine("please choose a the No. of a item you want to travel:");
Console.WriteLine("1.BreadthFirst----- 广度遍历");
Console.WriteLine("2.PreDepthFirst-----前序遍历");
Console.WriteLine("3.InDepthFirst----中序遍历");
Console.WriteLine("4.PostDepthFirst----后序遍历");
switch(Console.ReadLine())
{
case "1"://Show Stack
travelType=Tree.TraversalType.Breadth;
Console.WriteLine("广度遍历");
break;
case "2"://SortedList
travelType=Tree.TraversalType.PreDepth;
Console.WriteLine("前序遍历");
break;
case "3":
travelType=Tree.TraversalType.InDepth;
Console.WriteLine("中序遍历");
break;
case "4":
public static void ShowStack_RPNCalCulator()
{
//read a expression string and push every character into the stack in queue.
Console.WriteLine("this is performance for stack,you can input a string like this '123*+',then this subprogramme can compute it and get the result '7',this is RPN calculator. ");
Console.WriteLine("please input a expression string:");
string strExpression=Console.ReadLine();
char [] tmpChars=strExpression.ToCharArray(0,strExpression.Length);
Stack stackRPN=new Stack();
int numA,numB;
foreach(char tmp in tmpChars)
{
switch (tmp)
{
case '*':
numA=(int)stackRPN.Pop();
numB=(int)stackRPN.Pop();
stackRPN.Push(numA*numB);
break;
case '+':
numA=(int)stackRPN.Pop();
numB=(int)stackRPN.Pop();
stackRPN.Push(numA+numB);
break;
default:
stackRPN.Push(Int32.Parse(tmp.ToString()));
break;
}
}
Console.WriteLine("the result is:{0}",stackRPN.Pop().ToString());
}
public static void ShowSortedList_Polynomial()
{
//100+10*x+x^2 + 1+10*x+100x^2
SortedList tmpListA=new SortedList();
SortedList tmpListB=new SortedList();
SortedList tmpListC=new SortedList();//used to store the result
SortedList tmpKeyList=new SortedList();//used to store all keys of two polynomials
//init polynomial A and show it
tmpListA.Add(0,100);
tmpListA.Add(1,10);
tmpListA.Add(2,1);
ShowSortedList_ShowPolynomial("tmpListA",tmpListA.GetEnumerator());
//init polynomial B and show it
tmpListB.Add(0,1);
tmpListB.Add(1,10);
tmpListB.Add(2,100);
ShowSortedList_ShowPolynomial("tmpListB",tmpListB.GetEnumerator());
//init the key list which contains all keys of A and B but everyone once
IDictionaryEnumerator tmpIDic=tmpListA.GetEnumerator();
while(tmpIDic.MoveNext()!=false)
{
if(!tmpKeyList.ContainsKey(tmpIDic.Key))
{
tmpKeyList.Add(tmpIDic.Key,null);
}
}
//Add A and B and show the result
tmpIDic=tmpKeyList.GetEnumerator();
while(tmpIDic.MoveNext()!=false)
{
object objA=null,objB=null,objC=null;
objC=tmpIDic.Key;
if(tmpListA.ContainsKey(objC))
objA=tmpListA[objC];
if(tmpListA.ContainsKey(objC))
objB=tmpListB[objC];
//objC=objA+objB;
//tmpKeyList[objC]=(int)objA+(int)objC;
tmpListC.Add(objC,(int)objA+(int)objB);
}
ShowSortedList_ShowPolynomial("the addition result of A and B",tmpListC.GetEnumerator());
}
public static void ShowSortedList_ShowPolynomial(string tip,IDictionaryEnumerator iDic)
{
string strExpress=null;
iDic.Reset();
while(iDic.MoveNext()!=false)
{
strExpress+=iDic.Value.ToString()+"*X^"+iDic.Key.ToString()+"+";
}
Console.WriteLine(tip+":"+strExpress);
}
}
二叉树
using System;
using System.Collections;
namespace DataStructure
{
/// <summary>
/// BinaryTree 的摘要说明。
/// </summary>
public class BinaryTree:NaryTree
{
//构造二叉空树
public BinaryTree():base(2)
{
//
// TODO: 在此处添加构造函数逻辑
//
}