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 OpsRTree::RTreeNode::AddChild(void *childPtr) { assert(!IsNodeFull()); assert(m_child[0] != NULL); for (int i = 1; i < MaxChildren; i++) { if (m_child[i] == NULL) { m_child[i] = childPtr; break; } } } // end: AddChild()