/** * returns the first element and removes it from the heap. * * @return the first element * @throws Exception if no elements in heap */ public MyHeapElement get() throws Exception { if (m_heap[0].index == 0) { throw new Exception("No elements present in the heap"); } MyHeapElement r = m_heap[1]; m_heap[1] = m_heap[m_heap[0].index]; m_heap[0].index--; downheap(); return r; }
/** * Puts an element by substituting it in place of the top most element. * * @param d The distance value. * @throws Exception If distance is smaller than that of the head element. */ public void putBySubstitute(double d) throws Exception { MyHeapElement head = get(); put(d); if (head.distance == m_heap[1].distance) { putKthNearest(head.distance); } else if (head.distance > m_heap[1].distance) { m_KthNearest = null; m_KthNearestSize = 0; initSize = 10; } else if (head.distance < m_heap[1].distance) { throw new Exception("The substituted element is greater than the " + "head element. put() should have been called " + "in place of putBySubstitute()"); } }
/** * adds the distance value to the heap. * * @param d the distance value * @throws Exception if the heap gets too large */ public void put(double d) throws Exception { if ((m_heap[0].index + 1) > (m_heap.length - 1)) { throw new Exception("the number of elements cannot exceed the " + "initially set maximum limit"); } m_heap[0].index++; m_heap[m_heap[0].index] = new MyHeapElement(d); upheap(); }
CoverTreeNode parent, child; d_node par; MyHeap upper_k = new MyHeap(k); double d = Math.sqrt(m_DistanceFunction.distance(m_Root.p(), target, Double.POSITIVE_INFINITY, m_TreeStats)), upper_bound; for (int c = 0; c < parent.num_children; c++) { child = parent.children.element(c); upper_bound = upper_k.peek().distance; if (c == 0) { d = par.dist; upper_bound = upper_k.peek().distance; for (int i = 0; i < zero_set.length; i++) { tmpnode = zero_set.element(i);
CoverTreeNode parent, child; d_node par; MyHeap upper_k = new MyHeap(k); double d = Math.sqrt(m_DistanceFunction.distance(m_Root.p(), target, Double.POSITIVE_INFINITY, m_TreeStats)), upper_bound; for (int c = 0; c < parent.num_children; c++) { child = parent.children.element(c); upper_bound = upper_k.peek().distance; if (c == 0) { d = par.dist; upper_bound = upper_k.peek().distance; for (int i = 0; i < zero_set.length; i++) { tmpnode = zero_set.element(i);
for (int i = 0; i < zero_set.length; i++) { ele = zero_set.element(i); double upper_dist = new_upper_k.peek().distance + query_chi.max_dist; if (shell(ele.dist, query_chi.parent_dist, upper_dist)) { double d = Math.sqrt(m_DistanceFunction.distance(query_chi.p(), if (d < new_upper_k.peek().distance) { update(new_upper_k, d);
CoverTreeNode query_chi = query.children.element(0); brute_nearest(k, query_chi, zero_set, upper_k, results); MyHeap new_upper_k = new MyHeap(k); setter(new_upper_k, upper_k.peek().distance + query_chi.parent_dist, k); copy_zero_set(query_chi, new_upper_k, zero_set, new_zero_set); brute_nearest(k, query_chi, new_zero_set, new_upper_k, results); for (int i = 0; i < zero_set.length; i++) { ele = zero_set.element(i); if (ele.dist <= upper_k.peek().distance) {
for (int i = 0; i < zero_set.length; i++) { ele = zero_set.element(i); double upper_dist = new_upper_k.peek().distance + query_chi.max_dist; if (shell(ele.dist, query_chi.parent_dist, upper_dist)) { double d = Math.sqrt(m_DistanceFunction.distance(query_chi.p(), if (d < new_upper_k.peek().distance) { update(new_upper_k, d);
CoverTreeNode query_chi = query.children.element(0); brute_nearest(k, query_chi, zero_set, upper_k, results); MyHeap new_upper_k = new MyHeap(k); setter(new_upper_k, upper_k.peek().distance + query_chi.parent_dist, k); copy_zero_set(query_chi, new_upper_k, zero_set, new_zero_set); brute_nearest(k, query_chi, new_zero_set, new_upper_k, results); for (int i = 0; i < zero_set.length; i++) { ele = zero_set.element(i); if (ele.dist <= upper_k.peek().distance) {
MyHeap upper_k = new MyHeap(k);
MyHeap upper_k = new MyHeap(k);
/** * Puts an element by substituting it in place of the top most element. * * @param d The distance value. * @throws Exception If distance is smaller than that of the head element. */ public void putBySubstitute(double d) throws Exception { MyHeapElement head = get(); put(d); if (head.distance == m_heap[1].distance) { putKthNearest(head.distance); } else if (head.distance > m_heap[1].distance) { m_KthNearest = null; m_KthNearestSize = 0; initSize = 10; } else if (head.distance < m_heap[1].distance) { throw new Exception("The substituted element is greater than the " + "head element. put() should have been called " + "in place of putBySubstitute()"); } }
/** * Initializes a heap with k values of the the given upper_bound. * * @param heap The heap to put values into. * @param upper_bound The value to put into heap (the value with which it * should be initialized). * @param k The number of times upper_bound should be put into heap for * initialization. * @throws Exception If there is some problem in initializing the heap (if k * > size of the heap). */ protected void setter(MyHeap heap, double upper_bound, final int k) throws Exception { if (heap.size() > 0) { heap.m_heap[0].index = 0; } while (heap.size() < k) { heap.put(upper_bound); } }
/** * returns the first element and removes it from the heap. * * @return the first element * @throws Exception if no elements in heap */ public MyHeapElement get() throws Exception { if (m_heap[0].index == 0) { throw new Exception("No elements present in the heap"); } MyHeapElement r = m_heap[1]; m_heap[1] = m_heap[m_heap[0].index]; m_heap[0].index--; downheap(); return r; }