Chinaunix首页 | 论坛 | 博客
  • 博客访问: 207110
  • 博文数量: 38
  • 博客积分: 1410
  • 博客等级: 上尉
  • 技术积分: 410
  • 用 户 组: 普通用户
  • 注册时间: 2007-04-09 12:32
文章分类

全部博文(38)

文章存档

2011年(1)

2008年(12)

2007年(25)

我的朋友

分类: C/C++

2007-06-18 19:56:21

//**************minHeap.h****************//
//******最小堆的类定义和各操作的实现*******//
//C++源码

#ifndef MINHEAP_H
#define MINHEAP_H

#include

using namespace std;

template
class MinHeap {
public:
    MinHeap(const int size=20) {
        maxSize=(size>0)?size:20;
        array=new T[maxSize];
        currentPosition=0;
    }
    ~MinHeap() { delete [] array; }
    void insert(const T &);  //插入
    void deleteNode(const T &);  //删除
    int search(const T &) const; //查找
    void printAll() const { //打印
        for (int i=0; i            cout<        cout<    }
private:
    T *array;
    int maxSize;
    int currentPosition;
    int parent(const int &) const; //传入参数所在位置的父亲节点位置
    int leftChild(const int &) const;
    int rightChild(const int &) const;
    void swap(const int &, const int &); //交换两个位置的数据
    void moveUp(int);
    void moveDown(int);
};

template
int MinHeap::parent(const int &pos) const {
    return int((pos-1)/2);
}

template
int MinHeap::leftChild(const int &pos) const {
    return pos*2+1;
}

template
int MinHeap::rightChild(const int &pos) const {
    return pos*2+2;
}

template
void MinHeap::swap(const int &pos1, const int &pos2) {
    T tmp=array[pos1];
    array[pos1]=array[pos2];
    array[pos2]=tmp;
}

template
void MinHeap::moveUp(int pos) {
    int par=parent(pos);
    while (par>=0) {
        if (par==0) {
            if (array[pos]                swap(pos, par);
                break;
            }
            else break;
        }
        if (array[pos]            swap(pos, par);
            pos=par;    
            par=parent(pos);
        }
        else break;
    }
}

template
void MinHeap::moveDown(int pos) {
    int left=leftChild(pos);
    while (left        if (array[pos]>array[left] && array[left]>array[left+1]) {
            swap(pos, left+1);
            pos=left+1;
            left=leftChild(pos);
        }
        else if (array[pos]>array[left]) {
            swap(pos, left);
            pos=left;
            left=leftChild(pos);
        }
        else break;
    }
}

template
void MinHeap::insert(const T &data) {
    if (currentPosition==maxSize)
        return;
    array[currentPosition]=data;
    moveUp(currentPosition);
    currentPosition++;
}

template
void MinHeap::deleteNode(const T &data) {
    int pos=search(data);
    if (pos!=-1) {
        if (pos==currentPosition-1)
            currentPosition--;
        else {
            array[pos]=array[--currentPosition];
            moveDown(pos);
        }
    }
}

template
int MinHeap::search(const T &data) const {
    for (int i=0; i        if (array[i]==data)
            return i;
    return -1;
}

#endif //minHeap.h end


//********************main.cpp**********************//

#include "minHeap.h"

int main() {
    MinHeap heap(30);
    heap.insert(32);
    heap.insert(23);   
    heap.insert(9);
    heap.insert(4);
    heap.insert(21);
    heap.insert(13);
    heap.insert(15);
    heap.insert(2);
    heap.printAll();
   
    heap.deleteNode(2);
    heap.printAll();
    return 0;
}
//main.cpp end
文件:min_heap.rar
大小:17KB
下载:下载
阅读(4558) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~