/// <summary> /// Creates a 2D tree in a balanced way. /// </summary> /// <param name="distance_delegate"></param> /// <param name="sorted_points"></param> /// <param name="dimension"></param> public Tree2DNode(Tree2D <PointType> .Distance distance_delegate, List <PointType>[] sorted_points, int dimension) { // set the distance delegate. _distance_delegate = distance_delegate; // set the dimension. _dimension = dimension; // get the sorted list in question. List <PointType> points = sorted_points[_dimension]; // get the middle point. int middle = points.Count / 2; // create this point. _value = points[middle]; // split the list. List <PointType>[] lesser_points = new List <PointType> [2]; List <PointType>[] bigger_points = new List <PointType> [2]; lesser_points[_dimension] = new List <PointType>(points.GetRange( 0, middle - 1)); bigger_points[_dimension] = new List <PointType>(points.GetRange( middle + 1, points.Count - (middle + 1))); // calculate the other dimension. int other_dimension = (_dimension + 1) % 2; // remove the points from the other dimension lists. lesser_points[other_dimension] = new List <PointType>( sorted_points[other_dimension].Except <PointType>(bigger_points[_dimension])); lesser_points[other_dimension].Remove(_value); bigger_points[other_dimension] = new List <PointType>( sorted_points[other_dimension].Except <PointType>(lesser_points[_dimension])); bigger_points[other_dimension].Remove(_value); // create the other nodes. if (lesser_points[other_dimension].Count == 1) { _lesser = new Tree2DNode <PointType>(_distance_delegate, lesser_points[other_dimension][0], other_dimension); } else if (lesser_points[other_dimension].Count > 1) { _lesser = new Tree2DNode <PointType>(_distance_delegate, lesser_points, other_dimension); } if (bigger_points[other_dimension].Count == 1) { _bigger = new Tree2DNode <PointType>(_distance_delegate, bigger_points[other_dimension][0], other_dimension); } else if (bigger_points[other_dimension].Count > 1) { _bigger = new Tree2DNode <PointType>(_distance_delegate, bigger_points, other_dimension); } }
/// <summary> /// Creates a 2D tree. /// </summary> /// <param name="distance_delegate"></param> /// <param name="value"></param> /// <param name="dimension"></param> public Tree2DNode(Tree2D <PointType> .Distance distance_delegate, PointType value, int dimension) { // set the distance delegate. _distance_delegate = distance_delegate; _value = value; _dimension = dimension; }
public Tree2D(IEnumerable <PointType> points, Tree2D <PointType> .Distance distance_delegate) { this._distance_delegate = distance_delegate; List <PointType>[] sorted_points = new List <PointType> [2]; int num; for (int dim = 0; dim < 2; dim = num + 1) { List <PointType> pointTypeList = new List <PointType>(points); pointTypeList.Sort((Comparison <PointType>)((p1, p2) => p1[dim].CompareTo(p2[dim]))); sorted_points[dim] = pointTypeList; num = dim; } this._root = new Tree2DNode <PointType>(this._distance_delegate, sorted_points, 0); }
public Tree2DNode(Tree2D <PointType> .Distance distance_delegate, List <PointType>[] sorted_points, int dimension) { this._distance_delegate = distance_delegate; this._dimension = dimension; List <PointType> sortedPoint = sorted_points[this._dimension]; int index = sortedPoint.Count / 2; this._value = sortedPoint[index]; List <PointType>[] sorted_points1 = new List <PointType> [2]; List <PointType>[] sorted_points2 = new List <PointType> [2]; sorted_points1[this._dimension] = new List <PointType>((IEnumerable <PointType>)sortedPoint.GetRange(0, index - 1)); sorted_points2[this._dimension] = new List <PointType>((IEnumerable <PointType>)sortedPoint.GetRange(index + 1, sortedPoint.Count - (index + 1))); int dimension1 = (this._dimension + 1) % 2; sorted_points1[dimension1] = new List <PointType>(sorted_points[dimension1].Except <PointType>((IEnumerable <PointType>)sorted_points2[this._dimension])); sorted_points1[dimension1].Remove(this._value); sorted_points2[dimension1] = new List <PointType>(sorted_points[dimension1].Except <PointType>((IEnumerable <PointType>)sorted_points1[this._dimension])); sorted_points2[dimension1].Remove(this._value); if (sorted_points1[dimension1].Count == 1) { this._lesser = new Tree2DNode <PointType>(this._distance_delegate, sorted_points1[dimension1][0], dimension1); } else if (sorted_points1[dimension1].Count > 1) { this._lesser = new Tree2DNode <PointType>(this._distance_delegate, sorted_points1, dimension1); } if (sorted_points2[dimension1].Count == 1) { this._bigger = new Tree2DNode <PointType>(this._distance_delegate, sorted_points2[dimension1][0], dimension1); } else { if (sorted_points2[dimension1].Count <= 1) { return; } this._bigger = new Tree2DNode <PointType>(this._distance_delegate, sorted_points2, dimension1); } }
public Tree2DNode(Tree2D <PointType> .Distance distance_delegate, PointType value, int dimension) { this._distance_delegate = distance_delegate; this._value = value; this._dimension = dimension; }