private void SearchBetween(KDSearchScope[] searchAry, KDNode <T> node, ref List <T> result) { if (node != null) { if (this.IsInRange(node.TheValue, searchAry)) { result.Add(node.TheValue); } int index = node.Depth % this.dimension; if (searchAry[index].KDSearchType == KDSearchType.Default) { if (node.TheValue[index].CompareTo(searchAry[index].MinValue) < 0) { this.SearchBetween(searchAry, node.Right, ref result); } else if ((node.TheValue[index].CompareTo(searchAry[index].MinValue) >= 0) && (node.TheValue[index].CompareTo(searchAry[index].MaxValue) < 0)) { this.SearchBetween(searchAry, node.Left, ref result); this.SearchBetween(searchAry, node.Right, ref result); } else { this.SearchBetween(searchAry, node.Left, ref result); } } else { this.SearchBetween(searchAry, node.Left, ref result); this.SearchBetween(searchAry, node.Right, ref result); } } }
public KDNode(T val, int _depth) { this.left = null; this.right = null; this.theValue = val; this.depth = _depth; }
public KDNode <T> Insert(T val) { if (this.root == null) { this.root = new KDNode <T>(val, 0); return(this.root); } if (this.root.TheValue.Equals(val)) { return(this.root); } if (this.root.TheValue[0].CompareTo(val[0]) >= 0) { return(this.Insert(val, this.root.Left, this.root, true)); } return(this.Insert(val, this.root.Right, this.root, false)); }
private KDNode <T> search(T val, KDNode <T> node) { if (node == null) { return(null); } if (node.TheValue.Equals(val)) { return(node); } int num = node.Depth % this.dimension; if (node.TheValue[num].CompareTo(val[num]) <= 0) { return(this.search(val, node.Right)); } return(this.search(val, node.Left)); }
private KDNode <T> Insert(T val, KDNode <T> insertNode, KDNode <T> parentNode, bool isLeft) { if (insertNode == null) { if (isLeft) { parentNode.Left = new KDNode <T>(val, parentNode.Depth + 1); return(parentNode.Left); } parentNode.Right = new KDNode <T>(val, parentNode.Depth + 1); return(parentNode.Right); } if (insertNode.TheValue.Equals(val)) { return(insertNode); } int num = insertNode.Depth % this.dimension; if (insertNode.TheValue[num].CompareTo(val[num]) >= 0) { return(this.Insert(val, insertNode.Left, insertNode, true)); } return(this.Insert(val, insertNode.Right, insertNode, false)); }
private void SearchBetween(T min, T max, bool[] minClosedAry, bool[] maxClosedAry, KDNode <T> node, ref List <T> result) { if (node != null) { if (this.IsInRange(node.TheValue, min, max, minClosedAry, maxClosedAry)) { result.Add(node.TheValue); } int num = node.Depth % this.dimension; if (node.TheValue[num].CompareTo(min[num]) < 0) { this.SearchBetween(min, max, minClosedAry, maxClosedAry, node.Right, ref result); } else if ((node.TheValue[num].CompareTo(min[num]) >= 0) && (node.TheValue[num].CompareTo(max[num]) < 0)) { this.SearchBetween(min, max, minClosedAry, maxClosedAry, node.Left, ref result); this.SearchBetween(min, max, minClosedAry, maxClosedAry, node.Right, ref result); } else { this.SearchBetween(min, max, minClosedAry, maxClosedAry, node.Left, ref result); } } }
public KDTree(int _dimension) { this.root = null; this.dimension = 2; this.dimension = _dimension; }