最小二叉堆

最小二叉堆

#include 
#include 

using namespace std;

//模板函数的实现与声明要写在一个文件中

template <class comparable>
class binaryMinHeap {
public:
    binaryMinHeap();
    void insert(const comparable &value);
    void deleteMin();
    void print();
    void clear();
    bool isExisted(const comparable &value);
private:
    vector arr;
    int size;
};

template <class comparable>
binaryMinHeap::binaryMinHeap() {
    arr.push_back(0);
    size = 0;
}

template <class comparable>
void binaryMinHeap::insert(const comparable &value) {
    if (size == 0) {
        arr.push_back(value);
        size++;
    }
    else {
        arr.push_back(0);
        int childPos = size + 1, parentPos = childPos / 2;
        while (1) {
            comparable parentValue = arr[parentPos];
            if (parentPos == 0 || parentValue <= value) {   
                arr[childPos] = value;
                size++;
                break;
            }
            else {
                arr[childPos] = parentValue;
                childPos /= 2;
                parentPos = childPos / 2;
            }
        }
    }
}

//从根节点开始寻找最小子节点去替换父节点的值
//直到找到一个适合最后一个节点插入的位置
template <class comparable>
void binaryMinHeap::deleteMin() {
    if (size == 0)
        return;
    //取得最后一个位置的节点值
    comparable temp = arr[size];
    size--;
    int parent = 1, left = 2, right = 3;
    while (1) {
        right = left + 1;
        //无节点
        if (left > size) {
            arr[parent] = temp;
            break;
        }
        //只有一个节点
        else if (right > size) {
            if (arr[left] < temp) {
                arr[parent] = arr[left];
                arr[left] = temp;
                break;
            }
            else {
                arr[parent] = temp;
                break;
            }
        }
        //有两个节点
        comparable l = arr[left], r = arr[right];
        comparable min = l > r ? r : l;
        left = l > r ? right : left;
        if (min < temp) {
            arr[parent] = arr[left];
            parent = left;
            left *= 2;
        }
        else {
            arr[parent] = temp;
            break;
        }
    }
}

template <class comparable>
void binaryMinHeap::print() {
    for (int i = 1; i <= size; i++)
    {
        cout << arr[i] << ' ';
    }
    cout << endl;
}

template <class comparable>
void binaryMinHeap::clear() {
    size = 0;
    arr.clear();
    //填充首位置是为了让第一个元素放在1而不是0位置上
    arr.push_back(0);
}

template <class comparable>
bool binaryMinHeap::isExisted(const comparable& x) {
    int i = 1;
    while (i <= size) {
        int temp = i * 2;
        //遍历每一层去搜索,若一层中所有节点的值都小于搜索值则表示搜索值不在堆中
        int smallerCount = 0;
        while (i <= temp) {
            if (i > size)
                break;
            else if (x == arr[i])
                return true;
            else if (x < arr[i])
                smallerCount++;
            i++;
        }
        if (smallerCount == temp)
            return false;
    }
    return false;
}

你可能感兴趣的:(算法)