AdBrite

Your Ad Here

Thursday, April 21, 2011

}
template<class T>
void priority_queue<T>::_dequeue(T& data)
{
 if(root==NULL) return;
 node* pre = root;     //remember the old position of the root node
 while(root->right != NULL) {
  pre = root;
  root = root->right;  //move the root node down the tree to the biggest value
 }//now i've got the biggest element and it's parent
  if(pre != root) {
   //if it doesn't have any childs
   if(root->left == NULL) {
    node* tmp = root;
    data = root->data;
    root = pre;
    root->right = NULL;
    delete tmp;
   } else {
    node* tmp = root;
    data = root->data;
    root = pre;
    root->right = tmp->left;
    delete tmp;
   }
   //if the previous node is the root
  } else {
   data = root->data;
   node* tmp = root;
   root = root->left;
   delete tmp;
  }
}

No comments:

Post a Comment

BidVertiser

pocket cents

PocketCents Local Online Advertising