C#创建二叉搜索树的方法
本文实例讲述了C#创建二叉搜索树的方法。分享给大家供大家参考。具体如下:
publicstaticBinaryTreeNodeBuildBinarySearchTree(int[]sortedArray)
{
if(sortedArray.Length==0)
returnnull;
int_mid=sortedArray.Length/2;
BinaryTreeNode_root=newBinaryTreeNode(sortedArray[_mid]);
int[]_left=GetSubArray(sortedArray,0,_mid-1);
int[]_right=GetSubArray(sortedArray,_mid+1,sortedArray.Length-1);
_root.Left=BuildBinarySearchTree(_left);
_root.Right=BuildBinarySearchTree(_right);
return_root;
}
publicint[]GetSubArray(int[]array,intstart,intend)
{
List<int>_result=newList<int>();
for(inti=start;i<=end;i++)
{
_result.Add(array[i]);
}
return_result.ToArray();
}
希望本文所述对大家的C#程序设计有所帮助。