T RangeTree<T>::GetValueOnInterval(int idx, int subtree_left, int subtree_right, int searching_left, int searching_right) const { // printf("GVOI: %d v: %d sl: %d sr: %d sel: %d ser: %d\n", idx, // tree_[idx], subtree_left, subtree_right, searching_left, // searching_right); if (subtree_left == searching_left && subtree_right == searching_right) return tree_[idx]; const int left_child_boundary = subtree_left - 1 + GetSubtreeSize(subtree_left, subtree_right) / 2; T left_result; if (IsPointInLeftChild(searching_left, subtree_left, subtree_right)) { left_result = GetValueOnInterval(GetLeftChild(idx), subtree_left, left_child_boundary, searching_left, std::min(searching_right, left_child_boundary)); } const int right_child_boundary = left_child_boundary + 1; T right_result; if (IsPointInRightChild(searching_right, subtree_left, subtree_right)) { right_result = GetValueOnInterval(GetRightChild(idx), right_child_boundary, subtree_right, std::max(searching_left, right_child_boundary), searching_right); } if (IsPointInLeftChild(searching_right, subtree_left, subtree_right)) return left_result; if (IsPointInRightChild(searching_left, subtree_left, subtree_right)) return right_result; return (*operation_)(left_result, right_result); }
void RangeTree<T>::UpdatePoint(int point, T value) { int idx = rounded_capacity_ + point - 1; tree_[idx] = value; while (idx != 0) { idx = GetParent(idx); tree_[idx] = (*operation_)(tree_[GetLeftChild(idx)], tree_[GetRightChild(idx)]); } }
bool MinHeap<T>::GetMin(T& Item) { if (NumElements == 0) return false; // Min Value is at the top of the heap Item = Elements[0].Item; if (NumElements == 1) { NumElements--; return true; } // Now place the last item in the first spot and sift-down until heap is restored Elements[0] = Elements[NumElements-1]; NumElements--; Node* node = &Elements[0]; Node* leftChild = GetLeftChild(node); Node* rightChild = GetRightChild(node); while (1) { if (IsNodeLeaf(node)) break; if ( (leftChild == NULL && rightChild != NULL) || (IsNodeFull(node) && rightChild->Value <= leftChild->Value) ) { if (rightChild->Value < node->Value) { Swap(rightChild, node); node = rightChild; } else break; } else if ( (rightChild == NULL && leftChild != NULL) || (IsNodeFull(node) && leftChild->Value <= rightChild->Value) ) { if (leftChild->Value < node->Value) { Swap(leftChild, node); node = leftChild; } else break; } leftChild = GetLeftChild(node); rightChild = GetRightChild(node); } return true; }
void PercolateDown(Heap* heap,int i){ int left=GetLeftChild(heap,i); int right=GetRightChild(heap,i); int min; if(left !=-1 && heap->Array[i]>heap->Array[left]){ min=left; }else{ min=i; } if(right !=-1 && heap->Array[min]>heap->Array[right]){ min=right; } if(min!=i){ int tmpData=heap->Array[min]; heap->Array[min]=heap->Array[i]; heap->Array[i]=tmpData; PercolateDown(heap,min); } }
VParabola * VParabola::GetLeft (VParabola * p) { return GetLeftChild(GetLeftParent(p)); }
bool IsNodeFull(Node* node) { return (GetRightChild(node) != NULL && GetLeftChild(node) != NULL ); }
bool IsNodeLeaf(Node* node) { return (GetRightChild(node) == NULL && GetLeftChild(node) == NULL); }