示例#1
0
SkewHeap *Meld(SkewHeap *a, SkewHeap *b) {
  if (a == NULL) { return b; }
  if (b == NULL) { return a; }
  if (a->v < b->v) { swap(a, b); }
  a->r = Meld(a->r, b);
  swap(a->l, a->r);
  return a;
}
T& MinHblt<T>::RemoveMin() {
    if (root_ == NULL)
        throw queueEmpty();
    HbltNode<T> *left = root_->left_;
    HbltNode<T> *right = root_->right_;
    T& min = root_->element_;
    delete root_;
    root_ = left;
    Meld(root_, right);
    size_--;
    return min;
}
void MinHblt<T>::Initialize(T* elements, int size) {
    Queue<HbltNode<T>*> q(size);
    PostOrder(Destroy);
    
    for (int i = 1; i <= size; i++)
        q.Add(new HbltNode<T>(elements[i]));
    
    for (int i = 1; i <= size - 1; i++) {
        HbltNode<T> *a = q.Remove();
        HbltNode<T> *b = q.Remove();
        Meld(a,b);
        q.Add(a);
    }
    
    if (size > 0)
        root_ = q.Peek();
    size_ = size;
}
void MinHblt<T>::Meld(HbltNode<T>* &x, HbltNode<T>* &y) {
    if (y == NULL)
        return;
    if (x == NULL) {
        x = y;
        return;
    }
    if (y->element_ < x->element_) //change to > for maxHblt
        swap(x, y);
    // now x->element_ <= y->element_
    Meld(x->right_,y);
    
    if (x->left_ == NULL) {
        x->left_ = x->right_;
        x->right_ = NULL;
        x->sVal_ = 1;
    }
    else {
        if (x->left_->sVal_ < x->right_->sVal_)
            swap(x->left_, x->right_);
        x->sVal_ = x->right_->sVal_ + 1;
    }
}
void MinHblt<T>::Meld(MinHblt<T>& hblt) {
    Meld(root_, hblt.root_);
    size_ += hblt.size_;
    hblt.root_ = NULL;
    hblt.size_ = 0;
}
void MinHblt<T>::Add(const T& element) {
    HbltNode<T> *q = new HbltNode<T>(element);
    Meld(root_, q);
    size_++;
}
示例#7
0
SkewHeap *Pop(SkewHeap *a) {
  return Meld(a->l, a->r);
}
示例#8
0
SkewHeap *Push(SkewHeap *a, const Node &v) {
  assert(pool != NULL);
  SkewHeap *b = pool++;
  *b = SkewHeap(v);
  return Meld(a, b);
}