本文实例讲述了C#创建二叉搜索树的方法。分享给大家供大家参考。具体如下:
public static BinaryTreeNode BuildBinarySearchTree(int[] sortedArray){ if (sortedArray.Length == 0) return null; int _mid = sortedArray.Length / 2; BinaryTreeNode _root = new BinaryTreeNode(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;}public int[] GetSubArray(int[] array, int start, int end){ List<int> _result = new List<int>(); for (int i = start; i <= end; i++) { _result.Add(array[i]); } return _result.ToArray();}
希望本文所述对大家的C#程序设计有所帮助。
新闻热点
疑难解答