Console.WriteLine(a[i]);
}结果:
2,5,34,66,87,99
二叉查找树
//二叉查找树节点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;
}
else
this.right.InsertNode(node);
}
else
{if(this.left==null)
{this.left=node;return;}
else
this.left.InsertNode(node);
}
}
///二叉查找树查询
publicboolSearchKey(intsearchValue)
{if(this.key==searchValue)//searchValue需要查询的值
returntrue;//是否找到查询的值
if(searchValue>this.key)
{if(this.right==null)returnfalse;
else
returnthis.right.SearchKey(searchValue);
}
else
{if(this.left==null)returnfalse;
else
returnthis.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=nul;}
///生成一个二叉查找树
///二叉查找树根节点的值
publicBinarySearchTree(intnodeValue)
{root=newBinarySearchTreeNode(nodeValue);}
///在二叉查找树上插入一个节点
///插入节点的值
publicvoidInsertBinarySearchTreeNode(intnodeValue)
{BinarySearchTreeNodeinsertNode=newBinarySearchTreeNode(nodeValue);
if(root==null)
{root=insertNode;
return;
}
else
root.InsertNode(insertNode);
return;
}
///在二叉查找树上查询一个数
///需要查询的值
///是否找到查询的值
publicboolSearchKey(intsearchValue)
{if(root.key==searchValue)returntrue;
else
returnroot.SearchKey(searchValue);
}
///二叉查找树中序遍历
publicvoidMiddleDisplay()
{root.MiddeleDisplay();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;
else
this.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
单链表
classNode
{inta;
publicNode(inta)
{this.a=a;}
publicintA
{get{returna;}set{a=value;}}
publicNodenext;
}
classLinkedList
{Nodeheader;
publicvoidGenerate(intx)
{if(header==null)
header=newNode(x);
else
{Noden=newNode(x);
if(n.A{n.next=header;
header=n;
return;
}
Nodetmp=header;
Nodet=header;
while(tmp.A{t=tmp;//为了下一次循环
tmp=tmp.next;
if(tmp==null)
break;
}
t.next=n;
n.next=tmp;
}
}
publicvoidOut()
{Nodetmp=header;
while(tmp!
=null)
{Console.WriteLine(tmp.A);
tmp=tmp.next;
}}}
classTest
{staticvoidMain()
{LinkedListll=newLinkedList();
ll.Generate(6);
ll.Generate(36);
ll.Generate(26);
ll.Generate(16);
ll.Out();
}}}
反向链表
classLink//thisclassreversetheLinkedList
{publicinta;
publicLinknext;
}
classcreateLink//theclasscreatetheLinkedList
{
Linkheader=null;
Linkp=null;
Linktemp=null;
Linkl=null;//Linkk=null;
Linkg=null;
publicvoidcreate()
{stringstr;
inti;
Console.WriteLine("Pleaseenternumber:
");
str=Console.ReadLine();
while(str!
="y")
{i=Convert.ToInt32(str);
temp=newLink();
temp.a=i;
temp.next=null;
if(g==null)
g=temp;
if(header==null)
header=temp;
if(p==null)
p=temp;
else
{p.next=temp;
p=p.next;
}
Console.WriteLine("pleaseenternumber:
");
str=Console.ReadLine();
}
}
publicvoiddisplay()
{while(header!
=null)
{Console.WriteLine(header.a);
header=header.next;
}
}
publicvoidreversed()//themothodreversedtheLinkedList
{Linkk=null;
Linktmp=null;
Linkcom=null;
if(tmp==null)
tmp=header.next;
while(tmp!
=null)
{//if(com==null)
//com=header;
l=tmp;
if(k==null)
{header.next=null;
k=header;
}
com=header;
header=l;
tmp=l.next;
l.next=com;
}
}
publicvoidshow()
{while(l!
=null)
{Console.WriteLine(l.a);
l=l.next;
}}}
classTester
{staticvoidMain()
{createLinkcl=newcreateLink();
cl.create();
//cl.display();
cl.reversed();
cl.show();
}}}
Stack栈
classNode
{inta;
publicNode(inta)
{this.a=a;}
publicintA
{get{returna;}set{a=value;}}
publicNodenext;
}
classLinkedList
{protectedNodeheader;
publicvoidGenerate(intx)
{if(header==null)
header=newNode(x);
else
{Noden=newNode(x);
n.next=header;