微软面试常用算法总结C#编写.docx
《微软面试常用算法总结C#编写.docx》由会员分享,可在线阅读,更多相关《微软面试常用算法总结C#编写.docx(15页珍藏版)》请在冰豆网上搜索。
微软面试常用算法总结C#编写
全是用C#写的算法,并且都在VS上运行通过啦,希望能给大家一点参考~!
1.冒泡算法(这个太简单啦,不会考的,不过我们可以把这个当作Sort()工具来用哦,下边你就知道啦!
)
publicstaticint[]bubble(int[]a)
{
inttemp=0;
for(inti=a.Length-1;i>0;i--)
for(intj=1;j<=i;j++)
if(a[j-1]>a[j])
temp=a[j-1];
a[j-1]=a[j];
a[j]=temp;
}
returna;
2.判断回文(比如给你一个字符串”madam”,这就是一个回文啦!
publicstaticboolCheckPalindrome(stringstr)
char[]charCompare=str.ToCharArray();
Stacks=newStack();
Queueq=newQueue();
booljustfy=true;
for(inti=0;i{s.Push(charCompare[i]);q.Enqueue(charCompare[i]);}for(inti=0;i{if(s.Pop()!=q.Dequeue()){justfy=false;} }returnjustfy;}3.两个无序数组的合并(如果是字符串数组的话把int[]改成string[]就可以啦!)publicstaticListmerge(int[]a,int[]b){Listc=newList();inti=0;intj=0;if(a==null||a.Length==0){returnb.ToList();}if(b==null||b.Length==0){returna.ToList();}Array.Sort(a);//如果数组是有序的话这句就不用啦!Array.Sort(b);//如果数组是有序的话这句就不用啦!if(a[0]>b[0]){c.Add(b[0]);j++;}else{c.Add(a[0]);i++;}while(i{if(a[i]{c.Add(a[i]);i++;}else{c.Add(b[j]);j++;}}while(i{c.Add(a[i]);i++;}while(j{c.Add(b[j]);j++;}returnc;}4.一个数组的去重(比如122334455变成12345,这个可以用来在两个有序或无序数组合并之后做去重用)publicstaticListdistll(int[]ar){Listlist1=newList();list1.Add(ar[0]);for(inti=0;i{intj=0;while(j{if(ar[i]==list1[j]){break;}else{j++;}}if(j==list1.Count()){list1.Add(ar[i]);}}returnlist1;}5.一个无序数组先排序再去重。(比如:输入:2143517645,得到1234567)publicstaticint[]distill(int[]ar){if(ar.Length>0){Array.Sort(ar);intsize=1;for(inti=1;i{if(ar[i]!=ar[i-1]){size++;}}int[]temp=newint[size];intk=0;temp[k++]=ar[0];for(intj=1;j{if(ar[j-1]!=ar[j]){temp[k++]=ar[j];}}returntemp;}elsereturnnull;}6.字符串比较去重,删除string2中和string1中相同的字符。(是这样子滴:string1=abc,string2=safbhcmnads,那么去重之后我要得到的东西是:string3=sfhmnds)publicstaticListdistill(stringsta,stringstb){ Listar=newList();if(sta==null||sta.Length==0){for(intl=0;l{ar.Add(stb[l].ToString());}returnar;}if(stb==null||stb.Length==0)returnar;else{for(intj=0;j{inti=0;while(i{if(stb[j]==sta[i]){break;}else{i++;}}if(i==sta.Length){ar.Add(stb[j].ToString());}}returnar;}} 6.不让你直接用库函数split来实现字符串的分割(比如:用”sp”来分割”goodspmorning”,之后会得到”goodmorning”)publicstaticstring[]mysplit(stringstrinput,stringsp){ stringtmp="";intstrlen=0,splen=0;intfound=0;string[]rt=null;if(strinput==null||sp==null||strinput.Length==0)returnnull;if(sp.Length==0)returnnewstring[]{strinput};ArrayListtmp3=newArrayList();strlen=strinput.Length;splen=sp.Length;for(inti=0;i{found=strinput.IndexOf(sp,i);if(found>=0){tmp="";tmp=strinput.Substring(i,found-i);tmp3.Add(tmp);i=found+splen-1;}else{stringtmp2="";tmp2=strinput.Substring(i);if(tmp2!="")tmp3.Add(tmp2);break;}}tmp3.TrimToSize();rt=(string[])tmp3.ToArray(typeof(String));tmp3.Clear();returnrt;}7.把一个由数字组成的字符串变成整数(像这样string=”123”变成int型的123(一百二十三))publicstaticinttrans(stringstr){ if(str==null||str.Length==0){return-1;}intresult=0;for(intj=0;j{if(0<=(str[j]-'0')&&(str[j]-'0')<=9){checked{result=result*10+(str[j]-'0');}}else{return-1;}}returnresult;} 8.二叉树的东西(这个写起来还蛮费劲的,应该不会考到吧~呵呵~~)二叉查找树//二叉查找树节点BinarysearchtreenodepublicclassBinarySearchTreeNode{publicintkey;//二叉查找树节点的值publicBinarySearchTreeNodeleft;//二叉查找树节点的左子节点publicBinarySearchTreeNoderight;//二叉查找树节点的右子节点///二叉查找树节点构造函数publicBinarySearchTreeNode(intnodeValue){key=nodeValue;//nodeValue节点的值left=null;right=null;}///插入节点publicvoidInsertNode(BinarySearchTreeNodenode){if(node.key>this.key){if(this.right==null){this.right=node;//node插入的节点return;}elsethis.right.InsertNode(node);}else{if(this.left==null){this.left=node;return;}elsethis.left.InsertNode(node);}}///二叉查找树查询publicboolSearchKey(intsearchValue){if(this.key==searchValue)//searchValue需要查询的值returntrue;//是否找到查询的值if(searchValue>this.key){if(this.right==null)returnfalse;elsereturnthis.right.SearchKey(searchValue);}else{if(this.left==null)returnfalse;elsereturnthis.left.SearchKey(searchValue);}}//中序遍历publicvoidMiddleDisplay(){if(this.left!=null)this.left.MiddleDisplay();Console.WriteLine(this.key);if(this.right!=null)this.right.MiddleDisplay();}//前序遍历publicvoidFrontDisplay(){Console.WriteLine(this.key);if(this.left!=null)this.left.FrontDisplay();if(this.right!=null)this.right.FrontDisplay();}//后序遍历publicvoidBehindDisplay(){if(this.left!=null)this.left.BehindDisplay();if(this.right!=null)this.right.BehindDisplay();Console.WriteLine(this.key);}}///二叉查找树publicclassBinarySearchTree{privateBinarySearchTreeNoderoot;///生成一个二叉查找树publicBinarySearchTree(){root=null;}///生成一个二叉查找树///二叉查找树根节点的值publicBinarySearchTree(intnodeValue){root=newBinarySearchTreeNode(nodeValue);}///在二叉查找树上插入一个节点///插入节点的值publicvoidInsertBinarySearchTreeNode(intnodeValue){BinarySearchTreeNodeinsertNode=newBinarySearchTreeNode(nodeValue);if(root==null){root=insertNode;return;}elseroot.InsertNode(insertNode);return;}///在二叉查找树上查询一个数///需要查询的值///是否找到查询的值publicboolSearchKey(intsearchValue){if(root.key==searchValue)returntrue;elsereturnroot.SearchKey(searchValue);}///二叉查找树中序遍历publicvoidMiddleDisplay(){root.MiddleDisplay();return;}///二叉查找树前序遍历publicvoidFrontDisplay(){root.FrontDisplay();return;}///二叉查找树后序遍历publicvoidBehindDisplay(){root.BehindDisplay();return;}///二叉查找树排序///需要排序的数组publicstaticvoidBinarySearchTreeSort(int[]a){BinarySearchTreet=newBinarySearchTree();for(inti=0;it.InsertBinarySearchTreeNode(a[i]);t.MiddleDisplay();return;}///二叉查找树查找///进行查找的数组///需要查找的树publicstaticboolBinarySearchTreeSearch(int[]a,intsearchKey){BinarySearchTreet=newBinarySearchTree();for(inti=0;it.InsertBinarySearchTreeNode(a[i]);returnt.SearchKey(searchKey);}}namespace二叉树{classNode{intn;publicNode(intx){n=x;}publicNodeLeft;publicNodeRight;publicvoidInsert(Nodenode){if(node.n>this.n){if(this.Right==null)this.Right=node;elsethis.Right.Insert(node);}else{if(this.Left==null){this.Left=node;}else{this.Left.Insert(node);}}}//递归publicvoidShow(){Console.WriteLine(n);}}classBinaryTree{Noderoot;publicvoidGenerateTree(Nodenode)//高内聚,低耦合{if(root==null){root=node;return;}//如果树是空,第一次加节点root.Insert(node);}publicvoidShowInOrder(Nodenode)//中序遍历(inorder):左中右。先(前)序遍历(preorder):中左右。后序遍历(postorder):左右中。{if(node==null)return;//递归必须有个终止条件,递归方法中一定要接受参数ShowInOrder(node.Left);node.Show();ShowInOrder(node.Right);}publicvoidShow(){ShowInOrder(root);}}classA{staticvoidMain(){BinaryTreeb=newBinaryTree();Nodenode=newNode(5);b.GenerateTree(node);node=newNode(13);b.GenerateTree(node);node=newNode(6);b.GenerateTree(node);node=newNode(26);b.GenerateTree(node);node=newNode(7);b.GenerateTree(node);b.Show();}}}结果:5,6,7,13,26
s.Push(charCompare[i]);
q.Enqueue(charCompare[i]);
for(inti=0;i{if(s.Pop()!=q.Dequeue()){justfy=false;} }returnjustfy;}3.两个无序数组的合并(如果是字符串数组的话把int[]改成string[]就可以啦!)publicstaticListmerge(int[]a,int[]b){Listc=newList();inti=0;intj=0;if(a==null||a.Length==0){returnb.ToList();}if(b==null||b.Length==0){returna.ToList();}Array.Sort(a);//如果数组是有序的话这句就不用啦!Array.Sort(b);//如果数组是有序的话这句就不用啦!if(a[0]>b[0]){c.Add(b[0]);j++;}else{c.Add(a[0]);i++;}while(i{if(a[i]{c.Add(a[i]);i++;}else{c.Add(b[j]);j++;}}while(i{c.Add(a[i]);i++;}while(j{c.Add(b[j]);j++;}returnc;}4.一个数组的去重(比如122334455变成12345,这个可以用来在两个有序或无序数组合并之后做去重用)publicstaticListdistll(int[]ar){Listlist1=newList();list1.Add(ar[0]);for(inti=0;i{intj=0;while(j{if(ar[i]==list1[j]){break;}else{j++;}}if(j==list1.Count()){list1.Add(ar[i]);}}returnlist1;}5.一个无序数组先排序再去重。(比如:输入:2143517645,得到1234567)publicstaticint[]distill(int[]ar){if(ar.Length>0){Array.Sort(ar);intsize=1;for(inti=1;i{if(ar[i]!=ar[i-1]){size++;}}int[]temp=newint[size];intk=0;temp[k++]=ar[0];for(intj=1;j{if(ar[j-1]!=ar[j]){temp[k++]=ar[j];}}returntemp;}elsereturnnull;}6.字符串比较去重,删除string2中和string1中相同的字符。(是这样子滴:string1=abc,string2=safbhcmnads,那么去重之后我要得到的东西是:string3=sfhmnds)publicstaticListdistill(stringsta,stringstb){ Listar=newList();if(sta==null||sta.Length==0){for(intl=0;l{ar.Add(stb[l].ToString());}returnar;}if(stb==null||stb.Length==0)returnar;else{for(intj=0;j{inti=0;while(i{if(stb[j]==sta[i]){break;}else{i++;}}if(i==sta.Length){ar.Add(stb[j].ToString());}}returnar;}} 6.不让你直接用库函数split来实现字符串的分割(比如:用”sp”来分割”goodspmorning”,之后会得到”goodmorning”)publicstaticstring[]mysplit(stringstrinput,stringsp){ stringtmp="";intstrlen=0,splen=0;intfound=0;string[]rt=null;if(strinput==null||sp==null||strinput.Length==0)returnnull;if(sp.Length==0)returnnewstring[]{strinput};ArrayListtmp3=newArrayList();strlen=strinput.Length;splen=sp.Length;for(inti=0;i{found=strinput.IndexOf(sp,i);if(found>=0){tmp="";tmp=strinput.Substring(i,found-i);tmp3.Add(tmp);i=found+splen-1;}else{stringtmp2="";tmp2=strinput.Substring(i);if(tmp2!="")tmp3.Add(tmp2);break;}}tmp3.TrimToSize();rt=(string[])tmp3.ToArray(typeof(String));tmp3.Clear();returnrt;}7.把一个由数字组成的字符串变成整数(像这样string=”123”变成int型的123(一百二十三))publicstaticinttrans(stringstr){ if(str==null||str.Length==0){return-1;}intresult=0;for(intj=0;j{if(0<=(str[j]-'0')&&(str[j]-'0')<=9){checked{result=result*10+(str[j]-'0');}}else{return-1;}}returnresult;} 8.二叉树的东西(这个写起来还蛮费劲的,应该不会考到吧~呵呵~~)二叉查找树//二叉查找树节点BinarysearchtreenodepublicclassBinarySearchTreeNode{publicintkey;//二叉查找树节点的值publicBinarySearchTreeNodeleft;//二叉查找树节点的左子节点publicBinarySearchTreeNoderight;//二叉查找树节点的右子节点///二叉查找树节点构造函数publicBinarySearchTreeNode(intnodeValue){key=nodeValue;//nodeValue节点的值left=null;right=null;}///插入节点publicvoidInsertNode(BinarySearchTreeNodenode){if(node.key>this.key){if(this.right==null){this.right=node;//node插入的节点return;}elsethis.right.InsertNode(node);}else{if(this.left==null){this.left=node;return;}elsethis.left.InsertNode(node);}}///二叉查找树查询publicboolSearchKey(intsearchValue){if(this.key==searchValue)//searchValue需要查询的值returntrue;//是否找到查询的值if(searchValue>this.key){if(this.right==null)returnfalse;elsereturnthis.right.SearchKey(searchValue);}else{if(this.left==null)returnfalse;elsereturnthis.left.SearchKey(searchValue);}}//中序遍历publicvoidMiddleDisplay(){if(this.left!=null)this.left.MiddleDisplay();Console.WriteLine(this.key);if(this.right!=null)this.right.MiddleDisplay();}//前序遍历publicvoidFrontDisplay(){Console.WriteLine(this.key);if(this.left!=null)this.left.FrontDisplay();if(this.right!=null)this.right.FrontDisplay();}//后序遍历publicvoidBehindDisplay(){if(this.left!=null)this.left.BehindDisplay();if(this.right!=null)this.right.BehindDisplay();Console.WriteLine(this.key);}}///二叉查找树publicclassBinarySearchTree{privateBinarySearchTreeNoderoot;///生成一个二叉查找树publicBinarySearchTree(){root=null;}///生成一个二叉查找树///二叉查找树根节点的值publicBinarySearchTree(intnodeValue){root=newBinarySearchTreeNode(nodeValue);}///在二叉查找树上插入一个节点///插入节点的值publicvoidInsertBinarySearchTreeNode(intnodeValue){BinarySearchTreeNodeinsertNode=newBinarySearchTreeNode(nodeValue);if(root==null){root=insertNode;return;}elseroot.InsertNode(insertNode);return;}///在二叉查找树上查询一个数///需要查询的值///是否找到查询的值publicboolSearchKey(intsearchValue){if(root.key==searchValue)returntrue;elsereturnroot.SearchKey(searchValue);}///二叉查找树中序遍历publicvoidMiddleDisplay(){root.MiddleDisplay();return;}///二叉查找树前序遍历publicvoidFrontDisplay(){root.FrontDisplay();return;}///二叉查找树后序遍历publicvoidBehindDisplay(){root.BehindDisplay();return;}///二叉查找树排序///需要排序的数组publicstaticvoidBinarySearchTreeSort(int[]a){BinarySearchTreet=newBinarySearchTree();for(inti=0;it.InsertBinarySearchTreeNode(a[i]);t.MiddleDisplay();return;}///二叉查找树查找///进行查找的数组///需要查找的树publicstaticboolBinarySearchTreeSearch(int[]a,intsearchKey){BinarySearchTreet=newBinarySearchTree();for(inti=0;it.InsertBinarySearchTreeNode(a[i]);returnt.SearchKey(searchKey);}}namespace二叉树{classNode{intn;publicNode(intx){n=x;}publicNodeLeft;publicNodeRight;publicvoidInsert(Nodenode){if(node.n>this.n){if(this.Right==null)this.Right=node;elsethis.Right.Insert(node);}else{if(this.Left==null){this.Left=node;}else{this.Left.Insert(node);}}}//递归publicvoidShow(){Console.WriteLine(n);}}classBinaryTree{Noderoot;publicvoidGenerateTree(Nodenode)//高内聚,低耦合{if(root==null){root=node;return;}//如果树是空,第一次加节点root.Insert(node);}publicvoidShowInOrder(Nodenode)//中序遍历(inorder):左中右。先(前)序遍历(preorder):中左右。后序遍历(postorder):左右中。{if(node==null)return;//递归必须有个终止条件,递归方法中一定要接受参数ShowInOrder(node.Left);node.Show();ShowInOrder(node.Right);}publicvoidShow(){ShowInOrder(root);}}classA{staticvoidMain(){BinaryTreeb=newBinaryTree();Nodenode=newNode(5);b.GenerateTree(node);node=newNode(13);b.GenerateTree(node);node=newNode(6);b.GenerateTree(node);node=newNode(26);b.GenerateTree(node);node=newNode(7);b.GenerateTree(node);b.Show();}}}结果:5,6,7,13,26
if(s.Pop()!
=q.Dequeue())
justfy=false;
returnjustfy;
3.两个无序数组的合并(如果是字符串数组的话把int[]改成string[]就可以啦!
publicstaticListmerge(int[]a,int[]b)
Listc=newList();
inti=0;
intj=0;
if(a==null||a.Length==0)
returnb.ToList();
if(b==null||b.Length==0)
returna.ToList();
Array.Sort(a);//如果数组是有序的话这句就不用啦!
Array.Sort(b);//如果数组是有序的话这句就不用啦!
if(a[0]>b[0])
c.Add(b[0]);
j++;
else
c.Add(a[0]);
i++;
while(i{if(a[i]{c.Add(a[i]);i++;}else{c.Add(b[j]);j++;}}while(i{c.Add(a[i]);i++;}while(j{c.Add(b[j]);j++;}returnc;}4.一个数组的去重(比如122334455变成12345,这个可以用来在两个有序或无序数组合并之后做去重用)publicstaticListdistll(int[]ar){Listlist1=newList();list1.Add(ar[0]);for(inti=0;i{intj=0;while(j{if(ar[i]==list1[j]){break;}else{j++;}}if(j==list1.Count()){list1.Add(ar[i]);}}returnlist1;}5.一个无序数组先排序再去重。(比如:输入:2143517645,得到1234567)publicstaticint[]distill(int[]ar){if(ar.Length>0){Array.Sort(ar);intsize=1;for(inti=1;i{if(ar[i]!=ar[i-1]){size++;}}int[]temp=newint[size];intk=0;temp[k++]=ar[0];for(intj=1;j{if(ar[j-1]!=ar[j]){temp[k++]=ar[j];}}returntemp;}elsereturnnull;}6.字符串比较去重,删除string2中和string1中相同的字符。(是这样子滴:string1=abc,string2=safbhcmnads,那么去重之后我要得到的东西是:string3=sfhmnds)publicstaticListdistill(stringsta,stringstb){ Listar=newList();if(sta==null||sta.Length==0){for(intl=0;l{ar.Add(stb[l].ToString());}returnar;}if(stb==null||stb.Length==0)returnar;else{for(intj=0;j{inti=0;while(i{if(stb[j]==sta[i]){break;}else{i++;}}if(i==sta.Length){ar.Add(stb[j].ToString());}}returnar;}} 6.不让你直接用库函数split来实现字符串的分割(比如:用”sp”来分割”goodspmorning”,之后会得到”goodmorning”)publicstaticstring[]mysplit(stringstrinput,stringsp){ stringtmp="";intstrlen=0,splen=0;intfound=0;string[]rt=null;if(strinput==null||sp==null||strinput.Length==0)returnnull;if(sp.Length==0)returnnewstring[]{strinput};ArrayListtmp3=newArrayList();strlen=strinput.Length;splen=sp.Length;for(inti=0;i{found=strinput.IndexOf(sp,i);if(found>=0){tmp="";tmp=strinput.Substring(i,found-i);tmp3.Add(tmp);i=found+splen-1;}else{stringtmp2="";tmp2=strinput.Substring(i);if(tmp2!="")tmp3.Add(tmp2);break;}}tmp3.TrimToSize();rt=(string[])tmp3.ToArray(typeof(String));tmp3.Clear();returnrt;}7.把一个由数字组成的字符串变成整数(像这样string=”123”变成int型的123(一百二十三))publicstaticinttrans(stringstr){ if(str==null||str.Length==0){return-1;}intresult=0;for(intj=0;j{if(0<=(str[j]-'0')&&(str[j]-'0')<=9){checked{result=result*10+(str[j]-'0');}}else{return-1;}}returnresult;} 8.二叉树的东西(这个写起来还蛮费劲的,应该不会考到吧~呵呵~~)二叉查找树//二叉查找树节点BinarysearchtreenodepublicclassBinarySearchTreeNode{publicintkey;//二叉查找树节点的值publicBinarySearchTreeNodeleft;//二叉查找树节点的左子节点publicBinarySearchTreeNoderight;//二叉查找树节点的右子节点///二叉查找树节点构造函数publicBinarySearchTreeNode(intnodeValue){key=nodeValue;//nodeValue节点的值left=null;right=null;}///插入节点publicvoidInsertNode(BinarySearchTreeNodenode){if(node.key>this.key){if(this.right==null){this.right=node;//node插入的节点return;}elsethis.right.InsertNode(node);}else{if(this.left==null){this.left=node;return;}elsethis.left.InsertNode(node);}}///二叉查找树查询publicboolSearchKey(intsearchValue){if(this.key==searchValue)//searchValue需要查询的值returntrue;//是否找到查询的值if(searchValue>this.key){if(this.right==null)returnfalse;elsereturnthis.right.SearchKey(searchValue);}else{if(this.left==null)returnfalse;elsereturnthis.left.SearchKey(searchValue);}}//中序遍历publicvoidMiddleDisplay(){if(this.left!=null)this.left.MiddleDisplay();Console.WriteLine(this.key);if(this.right!=null)this.right.MiddleDisplay();}//前序遍历publicvoidFrontDisplay(){Console.WriteLine(this.key);if(this.left!=null)this.left.FrontDisplay();if(this.right!=null)this.right.FrontDisplay();}//后序遍历publicvoidBehindDisplay(){if(this.left!=null)this.left.BehindDisplay();if(this.right!=null)this.right.BehindDisplay();Console.WriteLine(this.key);}}///二叉查找树publicclassBinarySearchTree{privateBinarySearchTreeNoderoot;///生成一个二叉查找树publicBinarySearchTree(){root=null;}///生成一个二叉查找树///二叉查找树根节点的值publicBinarySearchTree(intnodeValue){root=newBinarySearchTreeNode(nodeValue);}///在二叉查找树上插入一个节点///插入节点的值publicvoidInsertBinarySearchTreeNode(intnodeValue){BinarySearchTreeNodeinsertNode=newBinarySearchTreeNode(nodeValue);if(root==null){root=insertNode;return;}elseroot.InsertNode(insertNode);return;}///在二叉查找树上查询一个数///需要查询的值///是否找到查询的值publicboolSearchKey(intsearchValue){if(root.key==searchValue)returntrue;elsereturnroot.SearchKey(searchValue);}///二叉查找树中序遍历publicvoidMiddleDisplay(){root.MiddleDisplay();return;}///二叉查找树前序遍历publicvoidFrontDisplay(){root.FrontDisplay();return;}///二叉查找树后序遍历publicvoidBehindDisplay(){root.BehindDisplay();return;}///二叉查找树排序///需要排序的数组publicstaticvoidBinarySearchTreeSort(int[]a){BinarySearchTreet=newBinarySearchTree();for(inti=0;it.InsertBinarySearchTreeNode(a[i]);t.MiddleDisplay();return;}///二叉查找树查找///进行查找的数组///需要查找的树publicstaticboolBinarySearchTreeSearch(int[]a,intsearchKey){BinarySearchTreet=newBinarySearchTree();for(inti=0;it.InsertBinarySearchTreeNode(a[i]);returnt.SearchKey(searchKey);}}namespace二叉树{classNode{intn;publicNode(intx){n=x;}publicNodeLeft;publicNodeRight;publicvoidInsert(Nodenode){if(node.n>this.n){if(this.Right==null)this.Right=node;elsethis.Right.Insert(node);}else{if(this.Left==null){this.Left=node;}else{this.Left.Insert(node);}}}//递归publicvoidShow(){Console.WriteLine(n);}}classBinaryTree{Noderoot;publicvoidGenerateTree(Nodenode)//高内聚,低耦合{if(root==null){root=node;return;}//如果树是空,第一次加节点root.Insert(node);}publicvoidShowInOrder(Nodenode)//中序遍历(inorder):左中右。先(前)序遍历(preorder):中左右。后序遍历(postorder):左右中。{if(node==null)return;//递归必须有个终止条件,递归方法中一定要接受参数ShowInOrder(node.Left);node.Show();ShowInOrder(node.Right);}publicvoidShow(){ShowInOrder(root);}}classA{staticvoidMain(){BinaryTreeb=newBinaryTree();Nodenode=newNode(5);b.GenerateTree(node);node=newNode(13);b.GenerateTree(node);node=newNode(6);b.GenerateTree(node);node=newNode(26);b.GenerateTree(node);node=newNode(7);b.GenerateTree(node);b.Show();}}}结果:5,6,7,13,26
if(a[i]
c.Add(a[i]);
c.Add(b[j]);
while(i{c.Add(a[i]);i++;}while(j{c.Add(b[j]);j++;}returnc;}4.一个数组的去重(比如122334455变成12345,这个可以用来在两个有序或无序数组合并之后做去重用)publicstaticListdistll(int[]ar){Listlist1=newList();list1.Add(ar[0]);for(inti=0;i{intj=0;while(j{if(ar[i]==list1[j]){break;}else{j++;}}if(j==list1.Count()){list1.Add(ar[i]);}}returnlist1;}5.一个无序数组先排序再去重。(比如:输入:2143517645,得到1234567)publicstaticint[]distill(int[]ar){if(ar.Length>0){Array.Sort(ar);intsize=1;for(inti=1;i{if(ar[i]!=ar[i-1]){size++;}}int[]temp=newint[size];intk=0;temp[k++]=ar[0];for(intj=1;j{if(ar[j-1]!=ar[j]){temp[k++]=ar[j];}}returntemp;}elsereturnnull;}6.字符串比较去重,删除string2中和string1中相同的字符。(是这样子滴:string1=abc,string2=safbhcmnads,那么去重之后我要得到的东西是:string3=sfhmnds)publicstaticListdistill(stringsta,stringstb){ Listar=newList();if(sta==null||sta.Length==0){for(intl=0;l{ar.Add(stb[l].ToString());}returnar;}if(stb==null||stb.Length==0)returnar;else{for(intj=0;j{inti=0;while(i{if(stb[j]==sta[i]){break;}else{i++;}}if(i==sta.Length){ar.Add(stb[j].ToString());}}returnar;}} 6.不让你直接用库函数split来实现字符串的分割(比如:用”sp”来分割”goodspmorning”,之后会得到”goodmorning”)publicstaticstring[]mysplit(stringstrinput,stringsp){ stringtmp="";intstrlen=0,splen=0;intfound=0;string[]rt=null;if(strinput==null||sp==null||strinput.Length==0)returnnull;if(sp.Length==0)returnnewstring[]{strinput};ArrayListtmp3=newArrayList();strlen=strinput.Length;splen=sp.Length;for(inti=0;i{found=strinput.IndexOf(sp,i);if(found>=0){tmp="";tmp=strinput.Substring(i,found-i);tmp3.Add(tmp);i=found+splen-1;}else{stringtmp2="";tmp2=strinput.Substring(i);if(tmp2!="")tmp3.Add(tmp2);break;}}tmp3.TrimToSize();rt=(string[])tmp3.ToArray(typeof(String));tmp3.Clear();returnrt;}7.把一个由数字组成的字符串变成整数(像这样string=”123”变成int型的123(一百二十三))publicstaticinttrans(stringstr){ if(str==null||str.Length==0){return-1;}intresult=0;for(intj=0;j{if(0<=(str[j]-'0')&&(str[j]-'0')<=9){checked{result=result*10+(str[j]-'0');}}else{return-1;}}returnresult;} 8.二叉树的东西(这个写起来还蛮费劲的,应该不会考到吧~呵呵~~)二叉查找树//二叉查找树节点BinarysearchtreenodepublicclassBinarySearchTreeNode{publicintkey;//二叉查找树节点的值publicBinarySearchTreeNodeleft;//二叉查找树节点的左子节点publicBinarySearchTreeNoderight;//二叉查找树节点的右子节点///二叉查找树节点构造函数publicBinarySearchTreeNode(intnodeValue){key=nodeValue;//nodeValue节点的值left=null;right=null;}///插入节点publicvoidInsertNode(BinarySearchTreeNodenode){if(node.key>this.key){if(this.right==null){this.right=node;//node插入的节点return;}elsethis.right.InsertNode(node);}else{if(this.left==null){this.left=node;return;}elsethis.left.InsertNode(node);}}///二叉查找树查询publicboolSearchKey(intsearchValue){if(this.key==searchValue)//searchValue需要查询的值returntrue;//是否找到查询的值if(searchValue>this.key){if(this.right==null)returnfalse;elsereturnthis.right.SearchKey(searchValue);}else{if(this.left==null)returnfalse;elsereturnthis.left.SearchKey(searchValue);}}//中序遍历publicvoidMiddleDisplay(){if(this.left!=null)this.left.MiddleDisplay();Console.WriteLine(this.key);if(this.right!=null)this.right.MiddleDisplay();}//前序遍历publicvoidFrontDisplay(){Console.WriteLine(this.key);if(this.left!=null)this.left.FrontDisplay();if(this.right!=null)this.right.FrontDisplay();}//后序遍历publicvoidBehindDisplay(){if(this.left!=null)this.left.BehindDisplay();if(this.right!=null)this.right.BehindDisplay();Console.WriteLine(this.key);}}///二叉查找树publicclassBinarySearchTree{privateBinarySearchTreeNoderoot;///生成一个二叉查找树publicBinarySearchTree(){root=null;}///生成一个二叉查找树///二叉查找树根节点的值publicBinarySearchTree(intnodeValue){root=newBinarySearchTreeNode(nodeValue);}///在二叉查找树上插入一个节点///插入节点的值publicvoidInsertBinarySearchTreeNode(intnodeValue){BinarySearchTreeNodeinsertNode=newBinarySearchTreeNode(nodeValue);if(root==null){root=insertNode;return;}elseroot.InsertNode(insertNode);return;}///在二叉查找树上查询一个数///需要查询的值///是否找到查询的值publicboolSearchKey(intsearchValue){if(root.key==searchValue)returntrue;elsereturnroot.SearchKey(searchValue);}///二叉查找树中序遍历publicvoidMiddleDisplay(){root.MiddleDisplay();return;}///二叉查找树前序遍历publicvoidFrontDisplay(){root.FrontDisplay();return;}///二叉查找树后序遍历publicvoidBehindDisplay(){root.BehindDisplay();return;}///二叉查找树排序///需要排序的数组publicstaticvoidBinarySearchTreeSort(int[]a){BinarySearchTreet=newBinarySearchTree();for(inti=0;it.InsertBinarySearchTreeNode(a[i]);t.MiddleDisplay();return;}///二叉查找树查找///进行查找的数组///需要查找的树publicstaticboolBinarySearchTreeSearch(int[]a,intsearchKey){BinarySearchTreet=newBinarySearchTree();for(inti=0;it.InsertBinarySearchTreeNode(a[i]);returnt.SearchKey(searchKey);}}namespace二叉树{classNode{intn;publicNode(intx){n=x;}publicNodeLeft;publicNodeRight;publicvoidInsert(Nodenode){if(node.n>this.n){if(this.Right==null)this.Right=node;elsethis.Right.Insert(node);}else{if(this.Left==null){this.Left=node;}else{this.Left.Insert(node);}}}//递归publicvoidShow(){Console.WriteLine(n);}}classBinaryTree{Noderoot;publicvoidGenerateTree(Nodenode)//高内聚,低耦合{if(root==null){root=node;return;}//如果树是空,第一次加节点root.Insert(node);}publicvoidShowInOrder(Nodenode)//中序遍历(inorder):左中右。先(前)序遍历(preorder):中左右。后序遍历(postorder):左右中。{if(node==null)return;//递归必须有个终止条件,递归方法中一定要接受参数ShowInOrder(node.Left);node.Show();ShowInOrder(node.Right);}publicvoidShow(){ShowInOrder(root);}}classA{staticvoidMain(){BinaryTreeb=newBinaryTree();Nodenode=newNode(5);b.GenerateTree(node);node=newNode(13);b.GenerateTree(node);node=newNode(6);b.GenerateTree(node);node=newNode(26);b.GenerateTree(node);node=newNode(7);b.GenerateTree(node);b.Show();}}}结果:5,6,7,13,26
while(j{c.Add(b[j]);j++;}returnc;}4.一个数组的去重(比如122334455变成12345,这个可以用来在两个有序或无序数组合并之后做去重用)publicstaticListdistll(int[]ar){Listlist1=newList();list1.Add(ar[0]);for(inti=0;i{intj=0;while(j{if(ar[i]==list1[j]){break;}else{j++;}}if(j==list1.Count()){list1.Add(ar[i]);}}returnlist1;}5.一个无序数组先排序再去重。(比如:输入:2143517645,得到1234567)publicstaticint[]distill(int[]ar){if(ar.Length>0){Array.Sort(ar);intsize=1;for(inti=1;i{if(ar[i]!=ar[i-1]){size++;}}int[]temp=newint[size];intk=0;temp[k++]=ar[0];for(intj=1;j{if(ar[j-1]!=ar[j]){temp[k++]=ar[j];}}returntemp;}elsereturnnull;}6.字符串比较去重,删除string2中和string1中相同的字符。(是这样子滴:string1=abc,string2=safbhcmnads,那么去重之后我要得到的东西是:string3=sfhmnds)publicstaticListdistill(stringsta,stringstb){ Listar=newList();if(sta==null||sta.Length==0){for(intl=0;l{ar.Add(stb[l].ToString());}returnar;}if(stb==null||stb.Length==0)returnar;else{for(intj=0;j{inti=0;while(i{if(stb[j]==sta[i]){break;}else{i++;}}if(i==sta.Length){ar.Add(stb[j].ToString());}}returnar;}} 6.不让你直接用库函数split来实现字符串的分割(比如:用”sp”来分割”goodspmorning”,之后会得到”goodmorning”)publicstaticstring[]mysplit(stringstrinput,stringsp){ stringtmp="";intstrlen=0,splen=0;intfound=0;string[]rt=null;if(strinput==null||sp==null||strinput.Length==0)returnnull;if(sp.Length==0)returnnewstring[]{strinput};ArrayListtmp3=newArrayList();strlen=strinput.Length;splen=sp.Length;for(inti=0;i{found=strinput.IndexOf(sp,i);if(found>=0){tmp="";tmp=strinput.Substring(i,found-i);tmp3.Add(tmp);i=found+splen-1;}else{stringtmp2="";tmp2=strinput.Substring(i);if(tmp2!="")tmp3.Add(tmp2);break;}}tmp3.TrimToSize();rt=(string[])tmp3.ToArray(typeof(String));tmp3.Clear();returnrt;}7.把一个由数字组成的字符串变成整数(像这样string=”123”变成int型的123(一百二十三))publicstaticinttrans(stringstr){ if(str==null||str.Length==0){return-1;}intresult=0;for(intj=0;j{if(0<=(str[j]-'0')&&(str[j]-'0')<=9){checked{result=result*10+(str[j]-'0');}}else{return-1;}}returnresult;} 8.二叉树的东西(这个写起来还蛮费劲的,应该不会考到吧~呵呵~~)二叉查找树//二叉查找树节点BinarysearchtreenodepublicclassBinarySearchTreeNode{publicintkey;//二叉查找树节点的值publicBinarySearchTreeNodeleft;//二叉查找树节点的左子节点publicBinarySearchTreeNoderight;//二叉查找树节点的右子节点///二叉查找树节点构造函数publicBinarySearchTreeNode(intnodeValue){key=nodeValue;//nodeValue节点的值left=null;right=null;}///插入节点publicvoidInsertNode(BinarySearchTreeNodenode){if(node.key>this.key){if(this.right==null){this.right=node;//node插入的节点return;}elsethis.right.InsertNode(node);}else{if(this.left==null){this.left=node;return;}elsethis.left.InsertNode(node);}}///二叉查找树查询publicboolSearchKey(intsearchValue){if(this.key==searchValue)//searchValue需要查询的值returntrue;//是否找到查询的值if(searchValue>this.key){if(this.right==null)returnfalse;elsereturnthis.right.SearchKey(searchValue);}else{if(this.left==null)returnfalse;elsereturnthis.left.SearchKey(searchValue);}}//中序遍历publicvoidMiddleDisplay(){if(this.left!=null)this.left.MiddleDisplay();Console.WriteLine(this.key);if(this.right!=null)this.right.MiddleDisplay();}//前序遍历publicvoidFrontDisplay(){Console.WriteLine(this.key);if(this.left!=null)this.left.FrontDisplay();if(this.right!=null)this.right.FrontDisplay();}//后序遍历publicvoidBehindDisplay(){if(this.left!=null)this.left.BehindDisplay();if(this.right!=null)this.right.BehindDisplay();Console.WriteLine(this.key);}}///二叉查找树publicclassBinarySearchTree{privateBinarySearchTreeNoderoot;///生成一个二叉查找树publicBinarySearchTree(){root=null;}///生成一个二叉查找树///二叉查找树根节点的值publicBinarySearchTree(intnodeValue){root=newBinarySearchTreeNode(nodeValue);}///在二叉查找树上插入一个节点///插入节点的值publicvoidInsertBinarySearchTreeNode(intnodeValue){BinarySearchTreeNodeinsertNode=newBinarySearchTreeNode(nodeValue);if(root==null){root=insertNode;return;}elseroot.InsertNode(insertNode);return;}///在二叉查找树上查询一个数///需要查询的值///是否找到查询的值publicboolSearchKey(intsearchValue){if(root.key==searchValue)returntrue;elsereturnroot.SearchKey(searchValue);}///二叉查找树中序遍历publicvoidMiddleDisplay(){root.MiddleDisplay();return;}///二叉查找树前序遍历publicvoidFrontDisplay(){root.FrontDisplay();return;}///二叉查找树后序遍历publicvoidBehindDisplay(){root.BehindDisplay();return;}///二叉查找树排序///需要排序的数组publicstaticvoidBinarySearchTreeSort(int[]a){BinarySearchTreet=newBinarySearchTree();for(inti=0;it.InsertBinarySearchTreeNode(a[i]);t.MiddleDisplay();return;}///二叉查找树查找///进行查找的数组///需要查找的树publicstaticboolBinarySearchTreeSearch(int[]a,intsearchKey){BinarySearchTreet=newBinarySearchTree();for(inti=0;it.InsertBinarySearchTreeNode(a[i]);returnt.SearchKey(searchKey);}}namespace二叉树{classNode{intn;publicNode(intx){n=x;}publicNodeLeft;publicNodeRight;publicvoidInsert(Nodenode){if(node.n>this.n){if(this.Right==null)this.Right=node;elsethis.Right.Insert(node);}else{if(this.Left==null){this.Left=node;}else{this.Left.Insert(node);}}}//递归publicvoidShow(){Console.WriteLine(n);}}classBinaryTree{Noderoot;publicvoidGenerateTree(Nodenode)//高内聚,低耦合{if(root==null){root=node;return;}//如果树是空,第一次加节点root.Insert(node);}publicvoidShowInOrder(Nodenode)//中序遍历(inorder):左中右。先(前)序遍历(preorder):中左右。后序遍历(postorder):左右中。{if(node==null)return;//递归必须有个终止条件,递归方法中一定要接受参数ShowInOrder(node.Left);node.Show();ShowInOrder(node.Right);}publicvoidShow(){ShowInOrder(root);}}classA{staticvoidMain(){BinaryTreeb=newBinaryTree();Nodenode=newNode(5);b.GenerateTree(node);node=newNode(13);b.GenerateTree(node);node=newNode(6);b.GenerateTree(node);node=newNode(26);b.GenerateTree(node);node=newNode(7);b.GenerateTree(node);b.Show();}}}结果:5,6,7,13,26
returnc;
4.一个数组的去重(比如122334455变成12345,这个可以用来在两个有序或无序数组合并之后做去重用)
publicstaticListdistll(int[]ar)
Listlist1=newList();
list1.Add(ar[0]);
for(inti=0;i{intj=0;while(j{if(ar[i]==list1[j]){break;}else{j++;}}if(j==list1.Count()){list1.Add(ar[i]);}}returnlist1;}5.一个无序数组先排序再去重。(比如:输入:2143517645,得到1234567)publicstaticint[]distill(int[]ar){if(ar.Length>0){Array.Sort(ar);intsize=1;for(inti=1;i{if(ar[i]!=ar[i-1]){size++;}}int[]temp=newint[size];intk=0;temp[k++]=ar[0];for(intj=1;j{if(ar[j-1]!=ar[j]){temp[k++]=ar[j];}}returntemp;}elsereturnnull;}6.字符串比较去重,删除string2中和string1中相同的字符。(是这样子滴:string1=abc,string2=safbhcmnads,那么去重之后我要得到的东西是:string3=sfhmnds)publicstaticListdistill(stringsta,stringstb){ Listar=newList();if(sta==null||sta.Length==0){for(intl=0;l{ar.Add(stb[l].ToString());}returnar;}if(stb==null||stb.Length==0)returnar;else{for(intj=0;j{inti=0;while(i{if(stb[j]==sta[i]){break;}else{i++;}}if(i==sta.Length){ar.Add(stb[j].ToString());}}returnar;}} 6.不让你直接用库函数split来实现字符串的分割(比如:用”sp”来分割”goodspmorning”,之后会得到”goodmorning”)publicstaticstring[]mysplit(stringstrinput,stringsp){ stringtmp="";intstrlen=0,splen=0;intfound=0;string[]rt=null;if(strinput==null||sp==null||strinput.Length==0)returnnull;if(sp.Length==0)returnnewstring[]{strinput};ArrayListtmp3=newArrayList();strlen=strinput.Length;splen=sp.Length;for(inti=0;i{found=strinput.IndexOf(sp,i);if(found>=0){tmp="";tmp=strinput.Substring(i,found-i);tmp3.Add(tmp);i=found+splen-1;}else{stringtmp2="";tmp2=strinput.Substring(i);if(tmp2!="")tmp3.Add(tmp2);break;}}tmp3.TrimToSize();rt=(string[])tmp3.ToArray(typeof(String));tmp3.Clear();returnrt;}7.把一个由数字组成的字符串变成整数(像这样string=”123”变成int型的123(一百二十三))publicstaticinttrans(stringstr){ if(str==null||str.Length==0){return-1;}intresult=0;for(intj=0;j{if(0<=(str[j]-'0')&&(str[j]-'0')<=9){checked{result=result*10+(str[j]-'0');}}else{return-1;}}returnresult;} 8.二叉树的东西(这个写起来还蛮费劲的,应该不会考到吧~呵呵~~)二叉查找树//二叉查找树节点BinarysearchtreenodepublicclassBinarySearchTreeNode{publicintkey;//二叉查找树节点的值publicBinarySearchTreeNodeleft;//二叉查找树节点的左子节点publicBinarySearchTreeNoderight;//二叉查找树节点的右子节点///二叉查找树节点构造函数publicBinarySearchTreeNode(intnodeValue){key=nodeValue;//nodeValue节点的值left=null;right=null;}///插入节点publicvoidInsertNode(BinarySearchTreeNodenode){if(node.key>this.key){if(this.right==null){this.right=node;//node插入的节点return;}elsethis.right.InsertNode(node);}else{if(this.left==null){this.left=node;return;}elsethis.left.InsertNode(node);}}///二叉查找树查询publicboolSearchKey(intsearchValue){if(this.key==searchValue)//searchValue需要查询的值returntrue;//是否找到查询的值if(searchValue>this.key){if(this.right==null)returnfalse;elsereturnthis.right.SearchKey(searchValue);}else{if(this.left==null)returnfalse;elsereturnthis.left.SearchKey(searchValue);}}//中序遍历publicvoidMiddleDisplay(){if(this.left!=null)this.left.MiddleDisplay();Console.WriteLine(this.key);if(this.right!=null)this.right.MiddleDisplay();}//前序遍历publicvoidFrontDisplay(){Console.WriteLine(this.key);if(this.left!=null)this.left.FrontDisplay();if(this.right!=null)this.right.FrontDisplay();}//后序遍历publicvoidBehindDisplay(){if(this.left!=null)this.left.BehindDisplay();if(this.right!=null)this.right.BehindDisplay();Console.WriteLine(this.key);}}///二叉查找树publicclassBinarySearchTree{privateBinarySearchTreeNoderoot;///生成一个二叉查找树publicBinarySearchTree(){root=null;}///生成一个二叉查找树///二叉查找树根节点的值publicBinarySearchTree(intnodeValue){root=newBinarySearchTreeNode(nodeValue);}///在二叉查找树上插入一个节点///插入节点的值publicvoidInsertBinarySearchTreeNode(intnodeValue){BinarySearchTreeNodeinsertNode=newBinarySearchTreeNode(nodeValue);if(root==null){root=insertNode;return;}elseroot.InsertNode(insertNode);return;}///在二叉查找树上查询一个数///需要查询的值///是否找到查询的值publicboolSearchKey(intsearchValue){if(root.key==searchValue)returntrue;elsereturnroot.SearchKey(searchValue);}///二叉查找树中序遍历publicvoidMiddleDisplay(){root.MiddleDisplay();return;}///二叉查找树前序遍历publicvoidFrontDisplay(){root.FrontDisplay();return;}///二叉查找树后序遍历publicvoidBehindDisplay(){root.BehindDisplay();return;}///二叉查找树排序///需要排序的数组publicstaticvoidBinarySearchTreeSort(int[]a){BinarySearchTreet=newBinarySearchTree();for(inti=0;it.InsertBinarySearchTreeNode(a[i]);t.MiddleDisplay();return;}///二叉查找树查找///进行查找的数组///需要查找的树publicstaticboolBinarySearchTreeSearch(int[]a,intsearchKey){BinarySearchTreet=newBinarySearchTree();for(inti=0;it.InsertBinarySearchTreeNode(a[i]);returnt.SearchKey(searchKey);}}namespace二叉树{classNode{intn;publicNode(intx){n=x;}publicNodeLeft;publicNodeRight;publicvoidInsert(Nodenode){if(node.n>this.n){if(this.Right==null)this.Right=node;elsethis.Right.Insert(node);}else{if(this.Left==null){this.Left=node;}else{this.Left.Insert(node);}}}//递归publicvoidShow(){Console.WriteLine(n);}}classBinaryTree{Noderoot;publicvoidGenerateTree(Nodenode)//高内聚,低耦合{if(root==null){root=node;return;}//如果树是空,第一次加节点root.Insert(node);}publicvoidShowInOrder(Nodenode)//中序遍历(inorder):左中右。先(前)序遍历(preorder):中左右。后序遍历(postorder):左右中。{if(node==null)return;//递归必须有个终止条件,递归方法中一定要接受参数ShowInOrder(node.Left);node.Show();ShowInOrder(node.Right);}publicvoidShow(){ShowInOrder(root);}}classA{staticvoidMain(){BinaryTreeb=newBinaryTree();Nodenode=newNode(5);b.GenerateTree(node);node=newNode(13);b.GenerateTree(node);node=newNode(6);b.GenerateTree(node);node=newNode(26);b.GenerateTree(node);node=newNode(7);b.GenerateTree(node);b.Show();}}}结果:5,6,7,13,26
while(j{if(ar[i]==list1[j]){break;}else{j++;}}if(j==list1.Count()){list1.Add(ar[i]);}}returnlist1;}5.一个无序数组先排序再去重。(比如:输入:2143517645,得到1234567)publicstaticint[]distill(int[]ar){if(ar.Length>0){Array.Sort(ar);intsize=1;for(inti=1;i{if(ar[i]!=ar[i-1]){size++;}}int[]temp=newint[size];intk=0;temp[k++]=ar[0];for(intj=1;j{if(ar[j-1]!=ar[j]){temp[k++]=ar[j];}}returntemp;}elsereturnnull;}6.字符串比较去重,删除string2中和string1中相同的字符。(是这样子滴:string1=abc,string2=safbhcmnads,那么去重之后我要得到的东西是:string3=sfhmnds)publicstaticListdistill(stringsta,stringstb){ Listar=newList();if(sta==null||sta.Length==0){for(intl=0;l{ar.Add(stb[l].ToString());}returnar;}if(stb==null||stb.Length==0)returnar;else{for(intj=0;j{inti=0;while(i{if(stb[j]==sta[i]){break;}else{i++;}}if(i==sta.Length){ar.Add(stb[j].ToString());}}returnar;}} 6.不让你直接用库函数split来实现字符串的分割(比如:用”sp”来分割”goodspmorning”,之后会得到”goodmorning”)publicstaticstring[]mysplit(stringstrinput,stringsp){ stringtmp="";intstrlen=0,splen=0;intfound=0;string[]rt=null;if(strinput==null||sp==null||strinput.Length==0)returnnull;if(sp.Length==0)returnnewstring[]{strinput};ArrayListtmp3=newArrayList();strlen=strinput.Length;splen=sp.Length;for(inti=0;i{found=strinput.IndexOf(sp,i);if(found>=0){tmp="";tmp=strinput.Substring(i,found-i);tmp3.Add(tmp);i=found+splen-1;}else{stringtmp2="";tmp2=strinput.Substring(i);if(tmp2!="")tmp3.Add(tmp2);break;}}tmp3.TrimToSize();rt=(string[])tmp3.ToArray(typeof(String));tmp3.Clear();returnrt;}7.把一个由数字组成的字符串变成整数(像这样string=”123”变成int型的123(一百二十三))publicstaticinttrans(stringstr){ if(str==null||str.Length==0){return-1;}intresult=0;for(intj=0;j{if(0<=(str[j]-'0')&&(str[j]-'0')<=9){checked{result=result*10+(str[j]-'0');}}else{return-1;}}returnresult;} 8.二叉树的东西(这个写起来还蛮费劲的,应该不会考到吧~呵呵~~)二叉查找树//二叉查找树节点BinarysearchtreenodepublicclassBinarySearchTreeNode{publicintkey;//二叉查找树节点的值publicBinarySearchTreeNodeleft;//二叉查找树节点的左子节点publicBinarySearchTreeNoderight;//二叉查找树节点的右子节点///二叉查找树节点构造函数publicBinarySearchTreeNode(intnodeValue){key=nodeValue;//nodeValue节点的值left=null;right=null;}///插入节点publicvoidInsertNode(BinarySearchTreeNodenode){if(node.key>this.key){if(this.right==null){this.right=node;//node插入的节点return;}elsethis.right.InsertNode(node);}else{if(this.left==null){this.left=node;return;}elsethis.left.InsertNode(node);}}///二叉查找树查询publicboolSearchKey(intsearchValue){if(this.key==searchValue)//searchValue需要查询的值returntrue;//是否找到查询的值if(searchValue>this.key){if(this.right==null)returnfalse;elsereturnthis.right.SearchKey(searchValue);}else{if(this.left==null)returnfalse;elsereturnthis.left.SearchKey(searchValue);}}//中序遍历publicvoidMiddleDisplay(){if(this.left!=null)this.left.MiddleDisplay();Console.WriteLine(this.key);if(this.right!=null)this.right.MiddleDisplay();}//前序遍历publicvoidFrontDisplay(){Console.WriteLine(this.key);if(this.left!=null)this.left.FrontDisplay();if(this.right!=null)this.right.FrontDisplay();}//后序遍历publicvoidBehindDisplay(){if(this.left!=null)this.left.BehindDisplay();if(this.right!=null)this.right.BehindDisplay();Console.WriteLine(this.key);}}///二叉查找树publicclassBinarySearchTree{privateBinarySearchTreeNoderoot;///生成一个二叉查找树publicBinarySearchTree(){root=null;}///生成一个二叉查找树///二叉查找树根节点的值publicBinarySearchTree(intnodeValue){root=newBinarySearchTreeNode(nodeValue);}///在二叉查找树上插入一个节点///插入节点的值publicvoidInsertBinarySearchTreeNode(intnodeValue){BinarySearchTreeNodeinsertNode=newBinarySearchTreeNode(nodeValue);if(root==null){root=insertNode;return;}elseroot.InsertNode(insertNode);return;}///在二叉查找树上查询一个数///需要查询的值///是否找到查询的值publicboolSearchKey(intsearchValue){if(root.key==searchValue)returntrue;elsereturnroot.SearchKey(searchValue);}///二叉查找树中序遍历publicvoidMiddleDisplay(){root.MiddleDisplay();return;}///二叉查找树前序遍历publicvoidFrontDisplay(){root.FrontDisplay();return;}///二叉查找树后序遍历publicvoidBehindDisplay(){root.BehindDisplay();return;}///二叉查找树排序///需要排序的数组publicstaticvoidBinarySearchTreeSort(int[]a){BinarySearchTreet=newBinarySearchTree();for(inti=0;it.InsertBinarySearchTreeNode(a[i]);t.MiddleDisplay();return;}///二叉查找树查找///进行查找的数组///需要查找的树publicstaticboolBinarySearchTreeSearch(int[]a,intsearchKey){BinarySearchTreet=newBinarySearchTree();for(inti=0;it.InsertBinarySearchTreeNode(a[i]);returnt.SearchKey(searchKey);}}namespace二叉树{classNode{intn;publicNode(intx){n=x;}publicNodeLeft;publicNodeRight;publicvoidInsert(Nodenode){if(node.n>this.n){if(this.Right==null)this.Right=node;elsethis.Right.Insert(node);}else{if(this.Left==null){this.Left=node;}else{this.Left.Insert(node);}}}//递归publicvoidShow(){Console.WriteLine(n);}}classBinaryTree{Noderoot;publicvoidGenerateTree(Nodenode)//高内聚,低耦合{if(root==null){root=node;return;}//如果树是空,第一次加节点root.Insert(node);}publicvoidShowInOrder(Nodenode)//中序遍历(inorder):左中右。先(前)序遍历(preorder):中左右。后序遍历(postorder):左右中。{if(node==null)return;//递归必须有个终止条件,递归方法中一定要接受参数ShowInOrder(node.Left);node.Show();ShowInOrder(node.Right);}publicvoidShow(){ShowInOrder(root);}}classA{staticvoidMain(){BinaryTreeb=newBinaryTree();Nodenode=newNode(5);b.GenerateTree(node);node=newNode(13);b.GenerateTree(node);node=newNode(6);b.GenerateTree(node);node=newNode(26);b.GenerateTree(node);node=newNode(7);b.GenerateTree(node);b.Show();}}}结果:5,6,7,13,26
if(ar[i]==list1[j])
break;
if(j==list1.Count())
list1.Add(ar[i]);
returnlist1;
5.一个无序数组先排序再去重。
(比如:
输入:
2143517645,得到1234567)
publicstaticint[]distill(int[]ar)
if(ar.Length>0)
Array.Sort(ar);
intsize=1;
for(inti=1;i{if(ar[i]!=ar[i-1]){size++;}}int[]temp=newint[size];intk=0;temp[k++]=ar[0];for(intj=1;j{if(ar[j-1]!=ar[j]){temp[k++]=ar[j];}}returntemp;}elsereturnnull;}6.字符串比较去重,删除string2中和string1中相同的字符。(是这样子滴:string1=abc,string2=safbhcmnads,那么去重之后我要得到的东西是:string3=sfhmnds)publicstaticListdistill(stringsta,stringstb){ Listar=newList();if(sta==null||sta.Length==0){for(intl=0;l{ar.Add(stb[l].ToString());}returnar;}if(stb==null||stb.Length==0)returnar;else{for(intj=0;j{inti=0;while(i{if(stb[j]==sta[i]){break;}else{i++;}}if(i==sta.Length){ar.Add(stb[j].ToString());}}returnar;}} 6.不让你直接用库函数split来实现字符串的分割(比如:用”sp”来分割”goodspmorning”,之后会得到”goodmorning”)publicstaticstring[]mysplit(stringstrinput,stringsp){ stringtmp="";intstrlen=0,splen=0;intfound=0;string[]rt=null;if(strinput==null||sp==null||strinput.Length==0)returnnull;if(sp.Length==0)returnnewstring[]{strinput};ArrayListtmp3=newArrayList();strlen=strinput.Length;splen=sp.Length;for(inti=0;i{found=strinput.IndexOf(sp,i);if(found>=0){tmp="";tmp=strinput.Substring(i,found-i);tmp3.Add(tmp);i=found+splen-1;}else{stringtmp2="";tmp2=strinput.Substring(i);if(tmp2!="")tmp3.Add(tmp2);break;}}tmp3.TrimToSize();rt=(string[])tmp3.ToArray(typeof(String));tmp3.Clear();returnrt;}7.把一个由数字组成的字符串变成整数(像这样string=”123”变成int型的123(一百二十三))publicstaticinttrans(stringstr){ if(str==null||str.Length==0){return-1;}intresult=0;for(intj=0;j{if(0<=(str[j]-'0')&&(str[j]-'0')<=9){checked{result=result*10+(str[j]-'0');}}else{return-1;}}returnresult;} 8.二叉树的东西(这个写起来还蛮费劲的,应该不会考到吧~呵呵~~)二叉查找树//二叉查找树节点BinarysearchtreenodepublicclassBinarySearchTreeNode{publicintkey;//二叉查找树节点的值publicBinarySearchTreeNodeleft;//二叉查找树节点的左子节点publicBinarySearchTreeNoderight;//二叉查找树节点的右子节点///二叉查找树节点构造函数publicBinarySearchTreeNode(intnodeValue){key=nodeValue;//nodeValue节点的值left=null;right=null;}///插入节点publicvoidInsertNode(BinarySearchTreeNodenode){if(node.key>this.key){if(this.right==null){this.right=node;//node插入的节点return;}elsethis.right.InsertNode(node);}else{if(this.left==null){this.left=node;return;}elsethis.left.InsertNode(node);}}///二叉查找树查询publicboolSearchKey(intsearchValue){if(this.key==searchValue)//searchValue需要查询的值returntrue;//是否找到查询的值if(searchValue>this.key){if(this.right==null)returnfalse;elsereturnthis.right.SearchKey(searchValue);}else{if(this.left==null)returnfalse;elsereturnthis.left.SearchKey(searchValue);}}//中序遍历publicvoidMiddleDisplay(){if(this.left!=null)this.left.MiddleDisplay();Console.WriteLine(this.key);if(this.right!=null)this.right.MiddleDisplay();}//前序遍历publicvoidFrontDisplay(){Console.WriteLine(this.key);if(this.left!=null)this.left.FrontDisplay();if(this.right!=null)this.right.FrontDisplay();}//后序遍历publicvoidBehindDisplay(){if(this.left!=null)this.left.BehindDisplay();if(this.right!=null)this.right.BehindDisplay();Console.WriteLine(this.key);}}///二叉查找树publicclassBinarySearchTree{privateBinarySearchTreeNoderoot;///生成一个二叉查找树publicBinarySearchTree(){root=null;}///生成一个二叉查找树///二叉查找树根节点的值publicBinarySearchTree(intnodeValue){root=newBinarySearchTreeNode(nodeValue);}///在二叉查找树上插入一个节点///插入节点的值publicvoidInsertBinarySearchTreeNode(intnodeValue){BinarySearchTreeNodeinsertNode=newBinarySearchTreeNode(nodeValue);if(root==null){root=insertNode;return;}elseroot.InsertNode(insertNode);return;}///在二叉查找树上查询一个数///需要查询的值///是否找到查询的值publicboolSearchKey(intsearchValue){if(root.key==searchValue)returntrue;elsereturnroot.SearchKey(searchValue);}///二叉查找树中序遍历publicvoidMiddleDisplay(){root.MiddleDisplay();return;}///二叉查找树前序遍历publicvoidFrontDisplay(){root.FrontDisplay();return;}///二叉查找树后序遍历publicvoidBehindDisplay(){root.BehindDisplay();return;}///二叉查找树排序///需要排序的数组publicstaticvoidBinarySearchTreeSort(int[]a){BinarySearchTreet=newBinarySearchTree();for(inti=0;it.InsertBinarySearchTreeNode(a[i]);t.MiddleDisplay();return;}///二叉查找树查找///进行查找的数组///需要查找的树publicstaticboolBinarySearchTreeSearch(int[]a,intsearchKey){BinarySearchTreet=newBinarySearchTree();for(inti=0;it.InsertBinarySearchTreeNode(a[i]);returnt.SearchKey(searchKey);}}namespace二叉树{classNode{intn;publicNode(intx){n=x;}publicNodeLeft;publicNodeRight;publicvoidInsert(Nodenode){if(node.n>this.n){if(this.Right==null)this.Right=node;elsethis.Right.Insert(node);}else{if(this.Left==null){this.Left=node;}else{this.Left.Insert(node);}}}//递归publicvoidShow(){Console.WriteLine(n);}}classBinaryTree{Noderoot;publicvoidGenerateTree(Nodenode)//高内聚,低耦合{if(root==null){root=node;return;}//如果树是空,第一次加节点root.Insert(node);}publicvoidShowInOrder(Nodenode)//中序遍历(inorder):左中右。先(前)序遍历(preorder):中左右。后序遍历(postorder):左右中。{if(node==null)return;//递归必须有个终止条件,递归方法中一定要接受参数ShowInOrder(node.Left);node.Show();ShowInOrder(node.Right);}publicvoidShow(){ShowInOrder(root);}}classA{staticvoidMain(){BinaryTreeb=newBinaryTree();Nodenode=newNode(5);b.GenerateTree(node);node=newNode(13);b.GenerateTree(node);node=newNode(6);b.GenerateTree(node);node=newNode(26);b.GenerateTree(node);node=newNode(7);b.GenerateTree(node);b.Show();}}}结果:5,6,7,13,26
if(ar[i]!
=ar[i-1])
size++;
int[]temp=newint[size];
intk=0;
temp[k++]=ar[0];
for(intj=1;j{if(ar[j-1]!=ar[j]){temp[k++]=ar[j];}}returntemp;}elsereturnnull;}6.字符串比较去重,删除string2中和string1中相同的字符。(是这样子滴:string1=abc,string2=safbhcmnads,那么去重之后我要得到的东西是:string3=sfhmnds)publicstaticListdistill(stringsta,stringstb){ Listar=newList();if(sta==null||sta.Length==0){for(intl=0;l{ar.Add(stb[l].ToString());}returnar;}if(stb==null||stb.Length==0)returnar;else{for(intj=0;j{inti=0;while(i{if(stb[j]==sta[i]){break;}else{i++;}}if(i==sta.Length){ar.Add(stb[j].ToString());}}returnar;}} 6.不让你直接用库函数split来实现字符串的分割(比如:用”sp”来分割”goodspmorning”,之后会得到”goodmorning”)publicstaticstring[]mysplit(stringstrinput,stringsp){ stringtmp="";intstrlen=0,splen=0;intfound=0;string[]rt=null;if(strinput==null||sp==null||strinput.Length==0)returnnull;if(sp.Length==0)returnnewstring[]{strinput};ArrayListtmp3=newArrayList();strlen=strinput.Length;splen=sp.Length;for(inti=0;i{found=strinput.IndexOf(sp,i);if(found>=0){tmp="";tmp=strinput.Substring(i,found-i);tmp3.Add(tmp);i=found+splen-1;}else{stringtmp2="";tmp2=strinput.Substring(i);if(tmp2!="")tmp3.Add(tmp2);break;}}tmp3.TrimToSize();rt=(string[])tmp3.ToArray(typeof(String));tmp3.Clear();returnrt;}7.把一个由数字组成的字符串变成整数(像这样string=”123”变成int型的123(一百二十三))publicstaticinttrans(stringstr){ if(str==null||str.Length==0){return-1;}intresult=0;for(intj=0;j{if(0<=(str[j]-'0')&&(str[j]-'0')<=9){checked{result=result*10+(str[j]-'0');}}else{return-1;}}returnresult;} 8.二叉树的东西(这个写起来还蛮费劲的,应该不会考到吧~呵呵~~)二叉查找树//二叉查找树节点BinarysearchtreenodepublicclassBinarySearchTreeNode{publicintkey;//二叉查找树节点的值publicBinarySearchTreeNodeleft;//二叉查找树节点的左子节点publicBinarySearchTreeNoderight;//二叉查找树节点的右子节点///二叉查找树节点构造函数publicBinarySearchTreeNode(intnodeValue){key=nodeValue;//nodeValue节点的值left=null;right=null;}///插入节点publicvoidInsertNode(BinarySearchTreeNodenode){if(node.key>this.key){if(this.right==null){this.right=node;//node插入的节点return;}elsethis.right.InsertNode(node);}else{if(this.left==null){this.left=node;return;}elsethis.left.InsertNode(node);}}///二叉查找树查询publicboolSearchKey(intsearchValue){if(this.key==searchValue)//searchValue需要查询的值returntrue;//是否找到查询的值if(searchValue>this.key){if(this.right==null)returnfalse;elsereturnthis.right.SearchKey(searchValue);}else{if(this.left==null)returnfalse;elsereturnthis.left.SearchKey(searchValue);}}//中序遍历publicvoidMiddleDisplay(){if(this.left!=null)this.left.MiddleDisplay();Console.WriteLine(this.key);if(this.right!=null)this.right.MiddleDisplay();}//前序遍历publicvoidFrontDisplay(){Console.WriteLine(this.key);if(this.left!=null)this.left.FrontDisplay();if(this.right!=null)this.right.FrontDisplay();}//后序遍历publicvoidBehindDisplay(){if(this.left!=null)this.left.BehindDisplay();if(this.right!=null)this.right.BehindDisplay();Console.WriteLine(this.key);}}///二叉查找树publicclassBinarySearchTree{privateBinarySearchTreeNoderoot;///生成一个二叉查找树publicBinarySearchTree(){root=null;}///生成一个二叉查找树///二叉查找树根节点的值publicBinarySearchTree(intnodeValue){root=newBinarySearchTreeNode(nodeValue);}///在二叉查找树上插入一个节点///插入节点的值publicvoidInsertBinarySearchTreeNode(intnodeValue){BinarySearchTreeNodeinsertNode=newBinarySearchTreeNode(nodeValue);if(root==null){root=insertNode;return;}elseroot.InsertNode(insertNode);return;}///在二叉查找树上查询一个数///需要查询的值///是否找到查询的值publicboolSearchKey(intsearchValue){if(root.key==searchValue)returntrue;elsereturnroot.SearchKey(searchValue);}///二叉查找树中序遍历publicvoidMiddleDisplay(){root.MiddleDisplay();return;}///二叉查找树前序遍历publicvoidFrontDisplay(){root.FrontDisplay();return;}///二叉查找树后序遍历publicvoidBehindDisplay(){root.BehindDisplay();return;}///二叉查找树排序///需要排序的数组publicstaticvoidBinarySearchTreeSort(int[]a){BinarySearchTreet=newBinarySearchTree();for(inti=0;it.InsertBinarySearchTreeNode(a[i]);t.MiddleDisplay();return;}///二叉查找树查找///进行查找的数组///需要查找的树publicstaticboolBinarySearchTreeSearch(int[]a,intsearchKey){BinarySearchTreet=newBinarySearchTree();for(inti=0;it.InsertBinarySearchTreeNode(a[i]);returnt.SearchKey(searchKey);}}namespace二叉树{classNode{intn;publicNode(intx){n=x;}publicNodeLeft;publicNodeRight;publicvoidInsert(Nodenode){if(node.n>this.n){if(this.Right==null)this.Right=node;elsethis.Right.Insert(node);}else{if(this.Left==null){this.Left=node;}else{this.Left.Insert(node);}}}//递归publicvoidShow(){Console.WriteLine(n);}}classBinaryTree{Noderoot;publicvoidGenerateTree(Nodenode)//高内聚,低耦合{if(root==null){root=node;return;}//如果树是空,第一次加节点root.Insert(node);}publicvoidShowInOrder(Nodenode)//中序遍历(inorder):左中右。先(前)序遍历(preorder):中左右。后序遍历(postorder):左右中。{if(node==null)return;//递归必须有个终止条件,递归方法中一定要接受参数ShowInOrder(node.Left);node.Show();ShowInOrder(node.Right);}publicvoidShow(){ShowInOrder(root);}}classA{staticvoidMain(){BinaryTreeb=newBinaryTree();Nodenode=newNode(5);b.GenerateTree(node);node=newNode(13);b.GenerateTree(node);node=newNode(6);b.GenerateTree(node);node=newNode(26);b.GenerateTree(node);node=newNode(7);b.GenerateTree(node);b.Show();}}}结果:5,6,7,13,26
if(ar[j-1]!
=ar[j])
temp[k++]=ar[j];
returntemp;
returnnull;
6.字符串比较去重,删除string2中和string1中相同的字符。
(是这样子滴:
string1=abc,string2=safbhcmnads,那么去重之后我要得到的东西是:
string3=sfhmnds)
publicstaticListdistill(stringsta,stringstb)
Listar=newList();
if(sta==null||sta.Length==0)
for(intl=0;l{ar.Add(stb[l].ToString());}returnar;}if(stb==null||stb.Length==0)returnar;else{for(intj=0;j{inti=0;while(i{if(stb[j]==sta[i]){break;}else{i++;}}if(i==sta.Length){ar.Add(stb[j].ToString());}}returnar;}} 6.不让你直接用库函数split来实现字符串的分割(比如:用”sp”来分割”goodspmorning”,之后会得到”goodmorning”)publicstaticstring[]mysplit(stringstrinput,stringsp){ stringtmp="";intstrlen=0,splen=0;intfound=0;string[]rt=null;if(strinput==null||sp==null||strinput.Length==0)returnnull;if(sp.Length==0)returnnewstring[]{strinput};ArrayListtmp3=newArrayList();strlen=strinput.Length;splen=sp.Length;for(inti=0;i{found=strinput.IndexOf(sp,i);if(found>=0){tmp="";tmp=strinput.Substring(i,found-i);tmp3.Add(tmp);i=found+splen-1;}else{stringtmp2="";tmp2=strinput.Substring(i);if(tmp2!="")tmp3.Add(tmp2);break;}}tmp3.TrimToSize();rt=(string[])tmp3.ToArray(typeof(String));tmp3.Clear();returnrt;}7.把一个由数字组成的字符串变成整数(像这样string=”123”变成int型的123(一百二十三))publicstaticinttrans(stringstr){ if(str==null||str.Length==0){return-1;}intresult=0;for(intj=0;j{if(0<=(str[j]-'0')&&(str[j]-'0')<=9){checked{result=result*10+(str[j]-'0');}}else{return-1;}}returnresult;} 8.二叉树的东西(这个写起来还蛮费劲的,应该不会考到吧~呵呵~~)二叉查找树//二叉查找树节点BinarysearchtreenodepublicclassBinarySearchTreeNode{publicintkey;//二叉查找树节点的值publicBinarySearchTreeNodeleft;//二叉查找树节点的左子节点publicBinarySearchTreeNoderight;//二叉查找树节点的右子节点///二叉查找树节点构造函数publicBinarySearchTreeNode(intnodeValue){key=nodeValue;//nodeValue节点的值left=null;right=null;}///插入节点publicvoidInsertNode(BinarySearchTreeNodenode){if(node.key>this.key){if(this.right==null){this.right=node;//node插入的节点return;}elsethis.right.InsertNode(node);}else{if(this.left==null){this.left=node;return;}elsethis.left.InsertNode(node);}}///二叉查找树查询publicboolSearchKey(intsearchValue){if(this.key==searchValue)//searchValue需要查询的值returntrue;//是否找到查询的值if(searchValue>this.key){if(this.right==null)returnfalse;elsereturnthis.right.SearchKey(searchValue);}else{if(this.left==null)returnfalse;elsereturnthis.left.SearchKey(searchValue);}}//中序遍历publicvoidMiddleDisplay(){if(this.left!=null)this.left.MiddleDisplay();Console.WriteLine(this.key);if(this.right!=null)this.right.MiddleDisplay();}//前序遍历publicvoidFrontDisplay(){Console.WriteLine(this.key);if(this.left!=null)this.left.FrontDisplay();if(this.right!=null)this.right.FrontDisplay();}//后序遍历publicvoidBehindDisplay(){if(this.left!=null)this.left.BehindDisplay();if(this.right!=null)this.right.BehindDisplay();Console.WriteLine(this.key);}}///二叉查找树publicclassBinarySearchTree{privateBinarySearchTreeNoderoot;///生成一个二叉查找树publicBinarySearchTree(){root=null;}///生成一个二叉查找树///二叉查找树根节点的值publicBinarySearchTree(intnodeValue){root=newBinarySearchTreeNode(nodeValue);}///在二叉查找树上插入一个节点///插入节点的值publicvoidInsertBinarySearchTreeNode(intnodeValue){BinarySearchTreeNodeinsertNode=newBinarySearchTreeNode(nodeValue);if(root==null){root=insertNode;return;}elseroot.InsertNode(insertNode);return;}///在二叉查找树上查询一个数///需要查询的值///是否找到查询的值publicboolSearchKey(intsearchValue){if(root.key==searchValue)returntrue;elsereturnroot.SearchKey(searchValue);}///二叉查找树中序遍历publicvoidMiddleDisplay(){root.MiddleDisplay();return;}///二叉查找树前序遍历publicvoidFrontDisplay(){root.FrontDisplay();return;}///二叉查找树后序遍历publicvoidBehindDisplay(){root.BehindDisplay();return;}///二叉查找树排序///需要排序的数组publicstaticvoidBinarySearchTreeSort(int[]a){BinarySearchTreet=newBinarySearchTree();for(inti=0;it.InsertBinarySearchTreeNode(a[i]);t.MiddleDisplay();return;}///二叉查找树查找///进行查找的数组///需要查找的树publicstaticboolBinarySearchTreeSearch(int[]a,intsearchKey){BinarySearchTreet=newBinarySearchTree();for(inti=0;it.InsertBinarySearchTreeNode(a[i]);returnt.SearchKey(searchKey);}}namespace二叉树{classNode{intn;publicNode(intx){n=x;}publicNodeLeft;publicNodeRight;publicvoidInsert(Nodenode){if(node.n>this.n){if(this.Right==null)this.Right=node;elsethis.Right.Insert(node);}else{if(this.Left==null){this.Left=node;}else{this.Left.Insert(node);}}}//递归publicvoidShow(){Console.WriteLine(n);}}classBinaryTree{Noderoot;publicvoidGenerateTree(Nodenode)//高内聚,低耦合{if(root==null){root=node;return;}//如果树是空,第一次加节点root.Insert(node);}publicvoidShowInOrder(Nodenode)//中序遍历(inorder):左中右。先(前)序遍历(preorder):中左右。后序遍历(postorder):左右中。{if(node==null)return;//递归必须有个终止条件,递归方法中一定要接受参数ShowInOrder(node.Left);node.Show();ShowInOrder(node.Right);}publicvoidShow(){ShowInOrder(root);}}classA{staticvoidMain(){BinaryTreeb=newBinaryTree();Nodenode=newNode(5);b.GenerateTree(node);node=newNode(13);b.GenerateTree(node);node=newNode(6);b.GenerateTree(node);node=newNode(26);b.GenerateTree(node);node=newNode(7);b.GenerateTree(node);b.Show();}}}结果:5,6,7,13,26
ar.Add(stb[l].ToString());
returnar;
if(stb==null||stb.Length==0)
for(intj=0;j{inti=0;while(i{if(stb[j]==sta[i]){break;}else{i++;}}if(i==sta.Length){ar.Add(stb[j].ToString());}}returnar;}} 6.不让你直接用库函数split来实现字符串的分割(比如:用”sp”来分割”goodspmorning”,之后会得到”goodmorning”)publicstaticstring[]mysplit(stringstrinput,stringsp){ stringtmp="";intstrlen=0,splen=0;intfound=0;string[]rt=null;if(strinput==null||sp==null||strinput.Length==0)returnnull;if(sp.Length==0)returnnewstring[]{strinput};ArrayListtmp3=newArrayList();strlen=strinput.Length;splen=sp.Length;for(inti=0;i{found=strinput.IndexOf(sp,i);if(found>=0){tmp="";tmp=strinput.Substring(i,found-i);tmp3.Add(tmp);i=found+splen-1;}else{stringtmp2="";tmp2=strinput.Substring(i);if(tmp2!="")tmp3.Add(tmp2);break;}}tmp3.TrimToSize();rt=(string[])tmp3.ToArray(typeof(String));tmp3.Clear();returnrt;}7.把一个由数字组成的字符串变成整数(像这样string=”123”变成int型的123(一百二十三))publicstaticinttrans(stringstr){ if(str==null||str.Length==0){return-1;}intresult=0;for(intj=0;j{if(0<=(str[j]-'0')&&(str[j]-'0')<=9){checked{result=result*10+(str[j]-'0');}}else{return-1;}}returnresult;} 8.二叉树的东西(这个写起来还蛮费劲的,应该不会考到吧~呵呵~~)二叉查找树//二叉查找树节点BinarysearchtreenodepublicclassBinarySearchTreeNode{publicintkey;//二叉查找树节点的值publicBinarySearchTreeNodeleft;//二叉查找树节点的左子节点publicBinarySearchTreeNoderight;//二叉查找树节点的右子节点///二叉查找树节点构造函数publicBinarySearchTreeNode(intnodeValue){key=nodeValue;//nodeValue节点的值left=null;right=null;}///插入节点publicvoidInsertNode(BinarySearchTreeNodenode){if(node.key>this.key){if(this.right==null){this.right=node;//node插入的节点return;}elsethis.right.InsertNode(node);}else{if(this.left==null){this.left=node;return;}elsethis.left.InsertNode(node);}}///二叉查找树查询publicboolSearchKey(intsearchValue){if(this.key==searchValue)//searchValue需要查询的值returntrue;//是否找到查询的值if(searchValue>this.key){if(this.right==null)returnfalse;elsereturnthis.right.SearchKey(searchValue);}else{if(this.left==null)returnfalse;elsereturnthis.left.SearchKey(searchValue);}}//中序遍历publicvoidMiddleDisplay(){if(this.left!=null)this.left.MiddleDisplay();Console.WriteLine(this.key);if(this.right!=null)this.right.MiddleDisplay();}//前序遍历publicvoidFrontDisplay(){Console.WriteLine(this.key);if(this.left!=null)this.left.FrontDisplay();if(this.right!=null)this.right.FrontDisplay();}//后序遍历publicvoidBehindDisplay(){if(this.left!=null)this.left.BehindDisplay();if(this.right!=null)this.right.BehindDisplay();Console.WriteLine(this.key);}}///二叉查找树publicclassBinarySearchTree{privateBinarySearchTreeNoderoot;///生成一个二叉查找树publicBinarySearchTree(){root=null;}///生成一个二叉查找树///二叉查找树根节点的值publicBinarySearchTree(intnodeValue){root=newBinarySearchTreeNode(nodeValue);}///在二叉查找树上插入一个节点///插入节点的值publicvoidInsertBinarySearchTreeNode(intnodeValue){BinarySearchTreeNodeinsertNode=newBinarySearchTreeNode(nodeValue);if(root==null){root=insertNode;return;}elseroot.InsertNode(insertNode);return;}///在二叉查找树上查询一个数///需要查询的值///是否找到查询的值publicboolSearchKey(intsearchValue){if(root.key==searchValue)returntrue;elsereturnroot.SearchKey(searchValue);}///二叉查找树中序遍历publicvoidMiddleDisplay(){root.MiddleDisplay();return;}///二叉查找树前序遍历publicvoidFrontDisplay(){root.FrontDisplay();return;}///二叉查找树后序遍历publicvoidBehindDisplay(){root.BehindDisplay();return;}///二叉查找树排序///需要排序的数组publicstaticvoidBinarySearchTreeSort(int[]a){BinarySearchTreet=newBinarySearchTree();for(inti=0;it.InsertBinarySearchTreeNode(a[i]);t.MiddleDisplay();return;}///二叉查找树查找///进行查找的数组///需要查找的树publicstaticboolBinarySearchTreeSearch(int[]a,intsearchKey){BinarySearchTreet=newBinarySearchTree();for(inti=0;it.InsertBinarySearchTreeNode(a[i]);returnt.SearchKey(searchKey);}}namespace二叉树{classNode{intn;publicNode(intx){n=x;}publicNodeLeft;publicNodeRight;publicvoidInsert(Nodenode){if(node.n>this.n){if(this.Right==null)this.Right=node;elsethis.Right.Insert(node);}else{if(this.Left==null){this.Left=node;}else{this.Left.Insert(node);}}}//递归publicvoidShow(){Console.WriteLine(n);}}classBinaryTree{Noderoot;publicvoidGenerateTree(Nodenode)//高内聚,低耦合{if(root==null){root=node;return;}//如果树是空,第一次加节点root.Insert(node);}publicvoidShowInOrder(Nodenode)//中序遍历(inorder):左中右。先(前)序遍历(preorder):中左右。后序遍历(postorder):左右中。{if(node==null)return;//递归必须有个终止条件,递归方法中一定要接受参数ShowInOrder(node.Left);node.Show();ShowInOrder(node.Right);}publicvoidShow(){ShowInOrder(root);}}classA{staticvoidMain(){BinaryTreeb=newBinaryTree();Nodenode=newNode(5);b.GenerateTree(node);node=newNode(13);b.GenerateTree(node);node=newNode(6);b.GenerateTree(node);node=newNode(26);b.GenerateTree(node);node=newNode(7);b.GenerateTree(node);b.Show();}}}结果:5,6,7,13,26
while(i{if(stb[j]==sta[i]){break;}else{i++;}}if(i==sta.Length){ar.Add(stb[j].ToString());}}returnar;}} 6.不让你直接用库函数split来实现字符串的分割(比如:用”sp”来分割”goodspmorning”,之后会得到”goodmorning”)publicstaticstring[]mysplit(stringstrinput,stringsp){ stringtmp="";intstrlen=0,splen=0;intfound=0;string[]rt=null;if(strinput==null||sp==null||strinput.Length==0)returnnull;if(sp.Length==0)returnnewstring[]{strinput};ArrayListtmp3=newArrayList();strlen=strinput.Length;splen=sp.Length;for(inti=0;i{found=strinput.IndexOf(sp,i);if(found>=0){tmp="";tmp=strinput.Substring(i,found-i);tmp3.Add(tmp);i=found+splen-1;}else{stringtmp2="";tmp2=strinput.Substring(i);if(tmp2!="")tmp3.Add(tmp2);break;}}tmp3.TrimToSize();rt=(string[])tmp3.ToArray(typeof(String));tmp3.Clear();returnrt;}7.把一个由数字组成的字符串变成整数(像这样string=”123”变成int型的123(一百二十三))publicstaticinttrans(stringstr){ if(str==null||str.Length==0){return-1;}intresult=0;for(intj=0;j{if(0<=(str[j]-'0')&&(str[j]-'0')<=9){checked{result=result*10+(str[j]-'0');}}else{return-1;}}returnresult;} 8.二叉树的东西(这个写起来还蛮费劲的,应该不会考到吧~呵呵~~)二叉查找树//二叉查找树节点BinarysearchtreenodepublicclassBinarySearchTreeNode{publicintkey;//二叉查找树节点的值publicBinarySearchTreeNodeleft;//二叉查找树节点的左子节点publicBinarySearchTreeNoderight;//二叉查找树节点的右子节点///二叉查找树节点构造函数publicBinarySearchTreeNode(intnodeValue){key=nodeValue;//nodeValue节点的值left=null;right=null;}///插入节点publicvoidInsertNode(BinarySearchTreeNodenode){if(node.key>this.key){if(this.right==null){this.right=node;//node插入的节点return;}elsethis.right.InsertNode(node);}else{if(this.left==null){this.left=node;return;}elsethis.left.InsertNode(node);}}///二叉查找树查询publicboolSearchKey(intsearchValue){if(this.key==searchValue)//searchValue需要查询的值returntrue;//是否找到查询的值if(searchValue>this.key){if(this.right==null)returnfalse;elsereturnthis.right.SearchKey(searchValue);}else{if(this.left==null)returnfalse;elsereturnthis.left.SearchKey(searchValue);}}//中序遍历publicvoidMiddleDisplay(){if(this.left!=null)this.left.MiddleDisplay();Console.WriteLine(this.key);if(this.right!=null)this.right.MiddleDisplay();}//前序遍历publicvoidFrontDisplay(){Console.WriteLine(this.key);if(this.left!=null)this.left.FrontDisplay();if(this.right!=null)this.right.FrontDisplay();}//后序遍历publicvoidBehindDisplay(){if(this.left!=null)this.left.BehindDisplay();if(this.right!=null)this.right.BehindDisplay();Console.WriteLine(this.key);}}///二叉查找树publicclassBinarySearchTree{privateBinarySearchTreeNoderoot;///生成一个二叉查找树publicBinarySearchTree(){root=null;}///生成一个二叉查找树///二叉查找树根节点的值publicBinarySearchTree(intnodeValue){root=newBinarySearchTreeNode(nodeValue);}///在二叉查找树上插入一个节点///插入节点的值publicvoidInsertBinarySearchTreeNode(intnodeValue){BinarySearchTreeNodeinsertNode=newBinarySearchTreeNode(nodeValue);if(root==null){root=insertNode;return;}elseroot.InsertNode(insertNode);return;}///在二叉查找树上查询一个数///需要查询的值///是否找到查询的值publicboolSearchKey(intsearchValue){if(root.key==searchValue)returntrue;elsereturnroot.SearchKey(searchValue);}///二叉查找树中序遍历publicvoidMiddleDisplay(){root.MiddleDisplay();return;}///二叉查找树前序遍历publicvoidFrontDisplay(){root.FrontDisplay();return;}///二叉查找树后序遍历publicvoidBehindDisplay(){root.BehindDisplay();return;}///二叉查找树排序///需要排序的数组publicstaticvoidBinarySearchTreeSort(int[]a){BinarySearchTreet=newBinarySearchTree();for(inti=0;it.InsertBinarySearchTreeNode(a[i]);t.MiddleDisplay();return;}///二叉查找树查找///进行查找的数组///需要查找的树publicstaticboolBinarySearchTreeSearch(int[]a,intsearchKey){BinarySearchTreet=newBinarySearchTree();for(inti=0;it.InsertBinarySearchTreeNode(a[i]);returnt.SearchKey(searchKey);}}namespace二叉树{classNode{intn;publicNode(intx){n=x;}publicNodeLeft;publicNodeRight;publicvoidInsert(Nodenode){if(node.n>this.n){if(this.Right==null)this.Right=node;elsethis.Right.Insert(node);}else{if(this.Left==null){this.Left=node;}else{this.Left.Insert(node);}}}//递归publicvoidShow(){Console.WriteLine(n);}}classBinaryTree{Noderoot;publicvoidGenerateTree(Nodenode)//高内聚,低耦合{if(root==null){root=node;return;}//如果树是空,第一次加节点root.Insert(node);}publicvoidShowInOrder(Nodenode)//中序遍历(inorder):左中右。先(前)序遍历(preorder):中左右。后序遍历(postorder):左右中。{if(node==null)return;//递归必须有个终止条件,递归方法中一定要接受参数ShowInOrder(node.Left);node.Show();ShowInOrder(node.Right);}publicvoidShow(){ShowInOrder(root);}}classA{staticvoidMain(){BinaryTreeb=newBinaryTree();Nodenode=newNode(5);b.GenerateTree(node);node=newNode(13);b.GenerateTree(node);node=newNode(6);b.GenerateTree(node);node=newNode(26);b.GenerateTree(node);node=newNode(7);b.GenerateTree(node);b.Show();}}}结果:5,6,7,13,26
if(stb[j]==sta[i])
if(i==sta.Length)
ar.Add(stb[j].ToString());
6.不让你直接用库函数split来实现字符串的分割(比如:
用”sp”来分割”goodspmorning”,之后会得到”goodmorning”)
publicstaticstring[]mysplit(stringstrinput,stringsp)
stringtmp="";
intstrlen=0,splen=0;
intfound=0;
string[]rt=null;
if(strinput==null||sp==null||strinput.Length==0)
if(sp.Length==0)
returnnewstring[]{strinput};
ArrayListtmp3=newArrayList();
strlen=strinput.Length;
splen=sp.Length;
for(inti=0;i{found=strinput.IndexOf(sp,i);if(found>=0){tmp="";tmp=strinput.Substring(i,found-i);tmp3.Add(tmp);i=found+splen-1;}else{stringtmp2="";tmp2=strinput.Substring(i);if(tmp2!="")tmp3.Add(tmp2);break;}}tmp3.TrimToSize();rt=(string[])tmp3.ToArray(typeof(String));tmp3.Clear();returnrt;}7.把一个由数字组成的字符串变成整数(像这样string=”123”变成int型的123(一百二十三))publicstaticinttrans(stringstr){ if(str==null||str.Length==0){return-1;}intresult=0;for(intj=0;j{if(0<=(str[j]-'0')&&(str[j]-'0')<=9){checked{result=result*10+(str[j]-'0');}}else{return-1;}}returnresult;} 8.二叉树的东西(这个写起来还蛮费劲的,应该不会考到吧~呵呵~~)二叉查找树//二叉查找树节点BinarysearchtreenodepublicclassBinarySearchTreeNode{publicintkey;//二叉查找树节点的值publicBinarySearchTreeNodeleft;//二叉查找树节点的左子节点publicBinarySearchTreeNoderight;//二叉查找树节点的右子节点///二叉查找树节点构造函数publicBinarySearchTreeNode(intnodeValue){key=nodeValue;//nodeValue节点的值left=null;right=null;}///插入节点publicvoidInsertNode(BinarySearchTreeNodenode){if(node.key>this.key){if(this.right==null){this.right=node;//node插入的节点return;}elsethis.right.InsertNode(node);}else{if(this.left==null){this.left=node;return;}elsethis.left.InsertNode(node);}}///二叉查找树查询publicboolSearchKey(intsearchValue){if(this.key==searchValue)//searchValue需要查询的值returntrue;//是否找到查询的值if(searchValue>this.key){if(this.right==null)returnfalse;elsereturnthis.right.SearchKey(searchValue);}else{if(this.left==null)returnfalse;elsereturnthis.left.SearchKey(searchValue);}}//中序遍历publicvoidMiddleDisplay(){if(this.left!=null)this.left.MiddleDisplay();Console.WriteLine(this.key);if(this.right!=null)this.right.MiddleDisplay();}//前序遍历publicvoidFrontDisplay(){Console.WriteLine(this.key);if(this.left!=null)this.left.FrontDisplay();if(this.right!=null)this.right.FrontDisplay();}//后序遍历publicvoidBehindDisplay(){if(this.left!=null)this.left.BehindDisplay();if(this.right!=null)this.right.BehindDisplay();Console.WriteLine(this.key);}}///二叉查找树publicclassBinarySearchTree{privateBinarySearchTreeNoderoot;///生成一个二叉查找树publicBinarySearchTree(){root=null;}///生成一个二叉查找树///二叉查找树根节点的值publicBinarySearchTree(intnodeValue){root=newBinarySearchTreeNode(nodeValue);}///在二叉查找树上插入一个节点///插入节点的值publicvoidInsertBinarySearchTreeNode(intnodeValue){BinarySearchTreeNodeinsertNode=newBinarySearchTreeNode(nodeValue);if(root==null){root=insertNode;return;}elseroot.InsertNode(insertNode);return;}///在二叉查找树上查询一个数///需要查询的值///是否找到查询的值publicboolSearchKey(intsearchValue){if(root.key==searchValue)returntrue;elsereturnroot.SearchKey(searchValue);}///二叉查找树中序遍历publicvoidMiddleDisplay(){root.MiddleDisplay();return;}///二叉查找树前序遍历publicvoidFrontDisplay(){root.FrontDisplay();return;}///二叉查找树后序遍历publicvoidBehindDisplay(){root.BehindDisplay();return;}///二叉查找树排序///需要排序的数组publicstaticvoidBinarySearchTreeSort(int[]a){BinarySearchTreet=newBinarySearchTree();for(inti=0;it.InsertBinarySearchTreeNode(a[i]);t.MiddleDisplay();return;}///二叉查找树查找///进行查找的数组///需要查找的树publicstaticboolBinarySearchTreeSearch(int[]a,intsearchKey){BinarySearchTreet=newBinarySearchTree();for(inti=0;it.InsertBinarySearchTreeNode(a[i]);returnt.SearchKey(searchKey);}}namespace二叉树{classNode{intn;publicNode(intx){n=x;}publicNodeLeft;publicNodeRight;publicvoidInsert(Nodenode){if(node.n>this.n){if(this.Right==null)this.Right=node;elsethis.Right.Insert(node);}else{if(this.Left==null){this.Left=node;}else{this.Left.Insert(node);}}}//递归publicvoidShow(){Console.WriteLine(n);}}classBinaryTree{Noderoot;publicvoidGenerateTree(Nodenode)//高内聚,低耦合{if(root==null){root=node;return;}//如果树是空,第一次加节点root.Insert(node);}publicvoidShowInOrder(Nodenode)//中序遍历(inorder):左中右。先(前)序遍历(preorder):中左右。后序遍历(postorder):左右中。{if(node==null)return;//递归必须有个终止条件,递归方法中一定要接受参数ShowInOrder(node.Left);node.Show();ShowInOrder(node.Right);}publicvoidShow(){ShowInOrder(root);}}classA{staticvoidMain(){BinaryTreeb=newBinaryTree();Nodenode=newNode(5);b.GenerateTree(node);node=newNode(13);b.GenerateTree(node);node=newNode(6);b.GenerateTree(node);node=newNode(26);b.GenerateTree(node);node=newNode(7);b.GenerateTree(node);b.Show();}}}结果:5,6,7,13,26
found=strinput.IndexOf(sp,i);
if(found>=0)
tmp="";
tmp=strinput.Substring(i,found-i);
tmp3.Add(tmp);
i=found+splen-1;
stringtmp2="";
tmp2=strinput.Substring(i);
if(tmp2!
="")
tmp3.Add(tmp2);
tmp3.TrimToSize();
rt=(string[])tmp3.ToArray(typeof(String));
tmp3.Clear();
returnrt;
7.把一个由数字组成的字符串变成整数(像这样string=”123”变成int型的123(一百二十三))
publicstaticinttrans(stringstr)
if(str==null||str.Length==0)
return-1;
intresult=0;
for(intj=0;j{if(0<=(str[j]-'0')&&(str[j]-'0')<=9){checked{result=result*10+(str[j]-'0');}}else{return-1;}}returnresult;} 8.二叉树的东西(这个写起来还蛮费劲的,应该不会考到吧~呵呵~~)二叉查找树//二叉查找树节点BinarysearchtreenodepublicclassBinarySearchTreeNode{publicintkey;//二叉查找树节点的值publicBinarySearchTreeNodeleft;//二叉查找树节点的左子节点publicBinarySearchTreeNoderight;//二叉查找树节点的右子节点///二叉查找树节点构造函数publicBinarySearchTreeNode(intnodeValue){key=nodeValue;//nodeValue节点的值left=null;right=null;}///插入节点publicvoidInsertNode(BinarySearchTreeNodenode){if(node.key>this.key){if(this.right==null){this.right=node;//node插入的节点return;}elsethis.right.InsertNode(node);}else{if(this.left==null){this.left=node;return;}elsethis.left.InsertNode(node);}}///二叉查找树查询publicboolSearchKey(intsearchValue){if(this.key==searchValue)//searchValue需要查询的值returntrue;//是否找到查询的值if(searchValue>this.key){if(this.right==null)returnfalse;elsereturnthis.right.SearchKey(searchValue);}else{if(this.left==null)returnfalse;elsereturnthis.left.SearchKey(searchValue);}}//中序遍历publicvoidMiddleDisplay(){if(this.left!=null)this.left.MiddleDisplay();Console.WriteLine(this.key);if(this.right!=null)this.right.MiddleDisplay();}//前序遍历publicvoidFrontDisplay(){Console.WriteLine(this.key);if(this.left!=null)this.left.FrontDisplay();if(this.right!=null)this.right.FrontDisplay();}//后序遍历publicvoidBehindDisplay(){if(this.left!=null)this.left.BehindDisplay();if(this.right!=null)this.right.BehindDisplay();Console.WriteLine(this.key);}}///二叉查找树publicclassBinarySearchTree{privateBinarySearchTreeNoderoot;///生成一个二叉查找树publicBinarySearchTree(){root=null;}///生成一个二叉查找树///二叉查找树根节点的值publicBinarySearchTree(intnodeValue){root=newBinarySearchTreeNode(nodeValue);}///在二叉查找树上插入一个节点///插入节点的值publicvoidInsertBinarySearchTreeNode(intnodeValue){BinarySearchTreeNodeinsertNode=newBinarySearchTreeNode(nodeValue);if(root==null){root=insertNode;return;}elseroot.InsertNode(insertNode);return;}///在二叉查找树上查询一个数///需要查询的值///是否找到查询的值publicboolSearchKey(intsearchValue){if(root.key==searchValue)returntrue;elsereturnroot.SearchKey(searchValue);}///二叉查找树中序遍历publicvoidMiddleDisplay(){root.MiddleDisplay();return;}///二叉查找树前序遍历publicvoidFrontDisplay(){root.FrontDisplay();return;}///二叉查找树后序遍历publicvoidBehindDisplay(){root.BehindDisplay();return;}///二叉查找树排序///需要排序的数组publicstaticvoidBinarySearchTreeSort(int[]a){BinarySearchTreet=newBinarySearchTree();for(inti=0;it.InsertBinarySearchTreeNode(a[i]);t.MiddleDisplay();return;}///二叉查找树查找///进行查找的数组///需要查找的树publicstaticboolBinarySearchTreeSearch(int[]a,intsearchKey){BinarySearchTreet=newBinarySearchTree();for(inti=0;it.InsertBinarySearchTreeNode(a[i]);returnt.SearchKey(searchKey);}}namespace二叉树{classNode{intn;publicNode(intx){n=x;}publicNodeLeft;publicNodeRight;publicvoidInsert(Nodenode){if(node.n>this.n){if(this.Right==null)this.Right=node;elsethis.Right.Insert(node);}else{if(this.Left==null){this.Left=node;}else{this.Left.Insert(node);}}}//递归publicvoidShow(){Console.WriteLine(n);}}classBinaryTree{Noderoot;publicvoidGenerateTree(Nodenode)//高内聚,低耦合{if(root==null){root=node;return;}//如果树是空,第一次加节点root.Insert(node);}publicvoidShowInOrder(Nodenode)//中序遍历(inorder):左中右。先(前)序遍历(preorder):中左右。后序遍历(postorder):左右中。{if(node==null)return;//递归必须有个终止条件,递归方法中一定要接受参数ShowInOrder(node.Left);node.Show();ShowInOrder(node.Right);}publicvoidShow(){ShowInOrder(root);}}classA{staticvoidMain(){BinaryTreeb=newBinaryTree();Nodenode=newNode(5);b.GenerateTree(node);node=newNode(13);b.GenerateTree(node);node=newNode(6);b.GenerateTree(node);node=newNode(26);b.GenerateTree(node);node=newNode(7);b.GenerateTree(node);b.Show();}}}结果:5,6,7,13,26
if(0<=(str[j]-'0')&&(str[j]-'0')<=9)
checked
result=result*10+(str[j]-'0');
returnresult;
8.二叉树的东西(这个写起来还蛮费劲的,应该不会考到吧~呵呵~~)
二叉查找树
//二叉查找树节点Binarysearchtreenode
publicclassBinarySearchTreeNode
{publicintkey;//二叉查找树节点的值
publicBinarySearchTreeNodeleft;//二叉查找树节点的左子节点
publicBinarySearchTreeNoderight;//二叉查找树节点的右子节点
///二叉查找树节点构造函数
publicBinarySearchTreeNode(intnodeValue)
{key=nodeValue;//nodeValue节点的值
left=null;right=null;
///插入节点
publicvoidInsertNode(BinarySearchTreeNodenode)
{if(node.key>this.key)
{if(this.right==null)
{this.right=node;//node插入的节点
return;
this.right.InsertNode(node);
{if(this.left==null)
{this.left=node;return;}
this.left.InsertNode(node);
///二叉查找树查询
publicboolSearchKey(intsearchValue)
{if(this.key==searchValue)//searchValue需要查询的值
returntrue;//是否找到查询的值
if(searchValue>this.key)
{if(this.right==null)returnfalse;
returnthis.right.SearchKey(searchValue);
{if(this.left==null)returnfalse;
returnthis.left.SearchKey(searchValue);
//中序遍历
publicvoidMiddleDisplay()
{if(this.left!
=null)
this.left.MiddleDisplay();
Console.WriteLine(this.key);
if(this.right!
this.right.MiddleDisplay();
//前序遍历
publicvoidFrontDisplay()
{Console.WriteLine(this.key);
if(this.left!
this.left.FrontDisplay();
this.right.FrontDisplay();
//后序遍历
publicvoidBehindDisplay()
this.left.BehindDisplay();
this.right.BehindDisplay();
///二叉查找树
publicclassBinarySearchTree
{privateBinarySearchTreeNoderoot;
///生成一个二叉查找树
publicBinarySearchTree()
{root=null;}
///二叉查找树根节点的值
publicBinarySearchTree(intnodeValue)
{root=newBinarySearchTreeNode(nodeValue);}
///在二叉查找树上插入一个节点
///插入节点的值
publicvoidInsertBinarySearchTreeNode(intnodeValue)
{BinarySearchTreeNodeinsertNode=newBinarySearchTreeNode(nodeValue);
if(root==null)
{root=insertNode;
root.InsertNode(insertNode);
///在二叉查找树上查询一个数
///需要查询的值
///是否找到查询的值
{if(root.key==searchValue)returntrue;
returnroot.SearchKey(searchValue);
///二叉查找树中序遍历
{root.MiddleDisplay();return;}
///二叉查找树前序遍历
{root.FrontDisplay();return;}
///二叉查找树后序遍历
{root.BehindDisplay();return;}
///二叉查找树排序
///需要排序的数组
publicstaticvoidBinarySearchTreeSort(int[]a)
{BinarySearchTreet=newBinarySearchTree();
for(inti=0;it.InsertBinarySearchTreeNode(a[i]);t.MiddleDisplay();return;}///二叉查找树查找///进行查找的数组///需要查找的树publicstaticboolBinarySearchTreeSearch(int[]a,intsearchKey){BinarySearchTreet=newBinarySearchTree();for(inti=0;it.InsertBinarySearchTreeNode(a[i]);returnt.SearchKey(searchKey);}}namespace二叉树{classNode{intn;publicNode(intx){n=x;}publicNodeLeft;publicNodeRight;publicvoidInsert(Nodenode){if(node.n>this.n){if(this.Right==null)this.Right=node;elsethis.Right.Insert(node);}else{if(this.Left==null){this.Left=node;}else{this.Left.Insert(node);}}}//递归publicvoidShow(){Console.WriteLine(n);}}classBinaryTree{Noderoot;publicvoidGenerateTree(Nodenode)//高内聚,低耦合{if(root==null){root=node;return;}//如果树是空,第一次加节点root.Insert(node);}publicvoidShowInOrder(Nodenode)//中序遍历(inorder):左中右。先(前)序遍历(preorder):中左右。后序遍历(postorder):左右中。{if(node==null)return;//递归必须有个终止条件,递归方法中一定要接受参数ShowInOrder(node.Left);node.Show();ShowInOrder(node.Right);}publicvoidShow(){ShowInOrder(root);}}classA{staticvoidMain(){BinaryTreeb=newBinaryTree();Nodenode=newNode(5);b.GenerateTree(node);node=newNode(13);b.GenerateTree(node);node=newNode(6);b.GenerateTree(node);node=newNode(26);b.GenerateTree(node);node=newNode(7);b.GenerateTree(node);b.Show();}}}结果:5,6,7,13,26
t.InsertBinarySearchTreeNode(a[i]);
t.MiddleDisplay();return;
}///二叉查找树查找
///进行查找的数组
///需要查找的树
publicstaticboolBinarySearchTreeSearch(int[]a,intsearchKey)
for(inti=0;it.InsertBinarySearchTreeNode(a[i]);returnt.SearchKey(searchKey);}}namespace二叉树{classNode{intn;publicNode(intx){n=x;}publicNodeLeft;publicNodeRight;publicvoidInsert(Nodenode){if(node.n>this.n){if(this.Right==null)this.Right=node;elsethis.Right.Insert(node);}else{if(this.Left==null){this.Left=node;}else{this.Left.Insert(node);}}}//递归publicvoidShow(){Console.WriteLine(n);}}classBinaryTree{Noderoot;publicvoidGenerateTree(Nodenode)//高内聚,低耦合{if(root==null){root=node;return;}//如果树是空,第一次加节点root.Insert(node);}publicvoidShowInOrder(Nodenode)//中序遍历(inorder):左中右。先(前)序遍历(preorder):中左右。后序遍历(postorder):左右中。{if(node==null)return;//递归必须有个终止条件,递归方法中一定要接受参数ShowInOrder(node.Left);node.Show();ShowInOrder(node.Right);}publicvoidShow(){ShowInOrder(root);}}classA{staticvoidMain(){BinaryTreeb=newBinaryTree();Nodenode=newNode(5);b.GenerateTree(node);node=newNode(13);b.GenerateTree(node);node=newNode(6);b.GenerateTree(node);node=newNode(26);b.GenerateTree(node);node=newNode(7);b.GenerateTree(node);b.Show();}}}结果:5,6,7,13,26
returnt.SearchKey(searchKey);
namespace二叉树
{classNode
{intn;
publicNode(intx)
{n=x;}
publicNodeLeft;
publicNodeRight;
publicvoidInsert(Nodenode)
{if(node.n>this.n)
{if(this.Right==null)
this.Right=node;
this.Right.Insert(node);}
{if(this.Left==null)
{this.Left=node;}
{this.Left.Insert(node);}}}//递归
publicvoidShow()
{Console.WriteLine(n);}}
classBinaryTree
{Noderoot;
publicvoidGenerateTree(Nodenode)//高内聚,低耦合
{if(root==null)
{root=node;return;}//如果树是空,第一次加节点
root.Insert(node);
publicvoidShowInOrder(Nodenode)//中序遍历(inorder):
左中右。
先(前)序遍历(preorder):
中左右。
后序遍历(postorder):
左右中。
{if(node==null)return;//递归必须有个终止条件,递归方法中一定要接受参数
ShowInOrder(node.Left);
node.Show();
ShowInOrder(node.Right);
{ShowInOrder(root);}
classA
{staticvoidMain()
{BinaryTreeb=newBinaryTree();
Nodenode=newNode(5);
b.GenerateTree(node);
node=newNode(13);
node=newNode(6);
node=newNode(26);
node=newNode(7);
b.Show();}}}结果:
5,6,7,13,26
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1