Example #1
0
void D_HEAP<KeyType>::insert(const KeyType& node, mem_rc flag, int size) {

    switch (flag) {
    case PROHIBIT_MEMORY_REALLOCATION:
        break;
    case ALLOW_MEMORY_REALLOCATION_WCV:
        if (size_ == sizeTree_) {
            size_ += getReallocSize();
            tree_ = (KeyType*)realloc(tree_, size_ * sizeof(KeyType));
            if (tree_ == 0)
                throw myExcp("Memory allocation error.");
        }
        break;
    case ALLOW_MEMORY_REALLOCATION_WYV:
        if (size < 0)
            throw("Size must be >= 0");
        size_ += size;
        tree_ = (KeyType*)realloc(tree_, size_ * sizeof(KeyType));
        if (tree_ == 0)
            throw myExcp("Memory allocation error.");
        break;
    default:
        throw myExcp("Incorrect flag value");
    }

    if (size_ == sizeTree_)
        throw myExcp("No memory for insert node.");
    sizeTree_++;
    tree_[sizeTree_ - 1] = node;
    siftUp(sizeTree_ - 1);
}
Example #2
0
D_HEAP<KeyType>::D_HEAP(int size, int d) {
    if (d <= 0)
        throw myExcp("'d' must be greater than 0.");

    if (size < 0)
        throw myExcp("Size must be >= 0");


    size_ = size;
    d_ = d;
    sizeTree_ = 0;
    tree_ = new KeyType[size_];

    if (tree_ == 0)
        throw myExcp("Memory allocation error.");
}
Example #3
0
void D_HEAP<KeyType>::swap(int first, int second) {
    if ((first >= sizeTree_) || (second >= sizeTree_) || (first < 0) || (second < 0))
        throw myExcp("Out of range.");

    KeyType tmp = tree_[first];
    tree_[first] = tree_[second];
    tree_[second] = tmp;
}
Example #4
0
void D_HEAP<KeyType>::deleteMinElem(void) {
    if (sizeTree_ == 0) {
        throw myExcp("Heap is empty.");
    }

    tree_[0] = tree_[--sizeTree_];
    if (sizeTree_ != 0)
        siftDown(0);
}
Node<KeyType>* List<KeyType>::find(const KeyType& key) const{
    if (first_ == 0)
        throw myExcp("List doesn't exist");
      
    Node<KeyType>* node = first_;
    do {
        if (node->key_ == key)
            break;
        node = node->next_;
    } while (node != 0);

    if (node == 0){
        delete node;
        throw myExcp("Node is not found");
    }

    return node;
}
Example #6
0
void D_HEAP<KeyType>::siftDown(int idx) {
    if ((idx >= sizeTree_) || (idx < 0))
        throw myExcp("Out of range.");

    int c = minChild(idx);
    while ((c != -1) && (tree_[c] < tree_[idx])) {
        swap((int)c, idx);
        idx = (int)c;
        c = minChild(idx);
    }
}
void List<KeyType>::push(const KeyType& key){
    Node<KeyType>* node;
    try {
        node = new Node<KeyType>(key);
    }
    catch (...) {
        throw myExcp("No memory.");
    }
    
    node->next_ = first_;
    first_ = node;
}
Example #8
0
D_HEAP<KeyType>::D_HEAP(const D_HEAP<KeyType>& tree) {
    size_ = tree.size_;
    sizeTree_ = tree.sizeTree_;
    d_ = tree.d_;

    tree_ = new KeyType[size_];
    if (tree_ == 0)
        throw myExcp("Memory allocation error.");

    for (int i = 0; i < sizeTree_; i++) {
        tree_[i] = tree.tree_[i];
    }
}
Example #9
0
void D_HEAP<KeyType>::siftUp(int idx) {
    if ((idx >= sizeTree_) || (idx < 0))
        throw myExcp("Out of range.");
    if (idx == 0) return;

    int parent = getParentIndex(idx);

    while ((parent != -1) && (tree_[parent] > tree_[idx])) {
        swap(parent, idx);
        idx = parent;
        parent = getParentIndex(idx);
    }
}
void List<KeyType>::pushAfter(const KeyType& findKey, const KeyType& key){
    Node<KeyType>* firstOccurrence;
    
    firstOccurrence = find(findKey);

    Node<KeyType>* tmp = firstOccurrence->next_;

    Node<KeyType>* node;
    try{
        node = new Node<KeyType>(key);
    } catch (...) {
        throw myExcp("No memory.");
    }

    firstOccurrence->next_ = node;
    node->next_ = tmp;
}
Example #11
0
void D_HEAP<KeyType>::deleteElem(int idx) {
    if ((idx >= sizeTree_) || (idx < 0))
        throw myExcp("Out of range.");

    swap(idx, sizeTree_ - 1);

    sizeTree_--;

    if (tree_[idx] < tree_[getParentIndex(idx)]) {
        cout << 2 << endl;

        siftUp(idx);
    }
    else {

        siftDown(idx);
    }
}
void List<KeyType>::pushEnd(const KeyType& key){
    if (first_ == 0) {
        push(key);
        return;
    }

    Node<KeyType>* node;
    try {
        node = new Node<KeyType>(key);
    } catch (...) {
        throw myExcp("No memory.");
    }

    Node<KeyType>* first = first_;

    while (first->next_ != 0)
        first = first->next_;
        
    first->next_ = node;
}
Example #13
0
PRIORITY_QUEUE<KeyType>::PRIORITY_QUEUE(int d) {
    if (d <= 0)
        throw myExcp("D must be > 0");

    heap_ = new D_HEAP<KeyType>(0, 1);
}
Example #14
0
void PRIORITY_QUEUE<KeyType>::pop(void) {
    if (isEmpty())
        throw myExcp("Priority queue is empty.");
    heap_->deleteMinElem();
}
Example #15
0
PRIORITY_QUEUE<KeyType>::PRIORITY_QUEUE(const PRIORITY_QUEUE& queue) {
    heap_ = new D_HEAP<KeyType>(*queue.heap_);
    if (heap_ == NULL)
        throw myExcp("Memory allocationn error.");
}
Example #16
0
KeyType PRIORITY_QUEUE<KeyType>::back(void) const {
    if (isEmpty())
        throw myExcp("Priority queue is empty.");
    KeyType tmp = heap_->getNodeKey(0);
    return tmp;
}
Example #17
0
KeyType D_HEAP<KeyType>::getNodeKey(int idx) const {
    if ((idx >= sizeTree_) || (idx < 0))
        throw myExcp("Out of range.");

    return tree_[idx];
}