二叉堆的操作总结(insert+deleteMin+increaseKey+decreaseKey+percolateDown+percolateUp)

【0】README

本文idea 均为原创, for source code, please visit https://github.com/pacosonTang/dataStructure-algorithmAnalysis/tree/master/chapter6/p140_binaryheap_conclusion 


【1】insert操作

// Attention, the index of the heap starts from 1
void insert(ElementType value, BinaryHeap bh)
{
	int i;

	if(isFull(bh))
	{
		Error("failed insertion , for the BinaryHeap is full, from func insert!");
		return ;	
	}

	for(i = ++bh->size; bh->elements[i/2] > value; i /= 2)
		bh->elements[i] = bh->elements[i / 2];

	bh->elements[i] = value;
}
二叉堆的操作总结(insert+deleteMin+increaseKey+decreaseKey+percolateDown+percolateUp)_第1张图片

二叉堆的操作总结(insert+deleteMin+increaseKey+decreaseKey+percolateDown+percolateUp)_第2张图片

二叉堆的操作总结(insert+deleteMin+increaseKey+decreaseKey+percolateDown+percolateUp)_第3张图片二叉堆的操作总结(insert+deleteMin+increaseKey+decreaseKey+percolateDown+percolateUp)_第4张图片


【2】deleteMin操作

ElementType deleteMin(BinaryHeap bh)
{    
    ElementType minimum;
    ElementType *data;    
        
    if(isEmpty(bh))
    {
        Error("failed deleting minimum , for the BinaryHeap is empty, from func deleteMin !");
        return -1;    
    }

    data = bh->elements;    
    minimum = data[1];

    swap(&data[1], &data[bh->size]); // &variable means nickname of the variable
    bh->size-- ; // size-- occurs prior to percolateDownFromOne 
    percolateDown(1, bh) ;    

    return minimum;
} 

// percolating down the element when its value is greater than children (minimal heap)
 //Attention: all of bh->elements starts from index 1
 void percolateDown(int index, BinaryHeap bh)
 {  
    ElementType *data;
    int size;
    ElementType temp;
    int child;

    data = bh->elements;
    size = bh->size;

    for(temp = data[index]; leftChild(index) <= size; index = child)
    {
        child = leftChild(index);
        if(child < size && data[child] > data[child+1])
        {
            child++;
        }
        if(temp > data[child])
        {
            data[index] = data[child];
        }
        else
        {
            break;
        }
    }
    data[index] = temp;
 }

二叉堆的操作总结(insert+deleteMin+increaseKey+decreaseKey+percolateDown+percolateUp)_第5张图片
二叉堆的操作总结(insert+deleteMin+increaseKey+decreaseKey+percolateDown+percolateUp)_第6张图片

【3】increaseKey操作,用到了下滤操作(干货——下滤操作)
// increase element's value
void increaseKey(int index, ElementType increment, BinaryHeap bh)
{   
    if(index > bh->size || index < 1)
    {
        Error(" failed increaseKey, since overstep the boundary! ");
        return ;
    }

    bh->elements[index] += increment; // update the element under given index
    percolateDown(index, bh);
}
// percolating down the element when its value is greater than children (minimal heap)
 //Attention: all of bh->elements starts from index 1
 void percolateDown(int index, BinaryHeap bh)
 {  
    ElementType *data;
    int size;
    ElementType temp;
    int child;

    data = bh->elements;
    size = bh->size;

    for(temp = data[index]; leftChild(index) <= size; index = child)
    {
        child = leftChild(index);
        if(child < size && data[child] > data[child+1])
        {
            child++;
        }
        if(temp > data[child])
        {
            data[index] = data[child];
        }
        else
        {
            break;
        }
    }
    data[index] = temp;
 }
二叉堆的操作总结(insert+deleteMin+increaseKey+decreaseKey+percolateDown+percolateUp)_第7张图片 二叉堆的操作总结(insert+deleteMin+increaseKey+decreaseKey+percolateDown+percolateUp)_第8张图片


【4】decreaseKey操作,用到了上滤操作(干货——上滤操作)
//decreasing value of the element under index by increment
void decreaseKey(int index, ElementType decrement, BinaryHeap bh)
{   
    if(index > bh->size || index < 1)
    {
        Error(" failed decreaseKey, since overstep the boundary! ");
        return ;
    }

    bh->elements[index] -= decrement; // update the element under given index
    percolateUp(index, bh);
}

int parentIndex(int index)
{
    return index / 2;
}

// percolating up the element when its value is greater than children (minimal heap)
 //Attention: all of bh->elements starts from index 1
 void percolateUp(int index, BinaryHeap bh)
 {  
    ElementType *data;
    ElementType temp;
    int size;   
    int parent;

    data = bh->elements;
    size = bh->size;

    for(temp = data[index]; parentIndex(index) > 0; index = parent)
    {
        parent = parentIndex(index);
        if(parent == 0 || temp > data[parent])  
        {
            break;
        }
        else        
        {
            data[index] = data[parent];                                         
        }
    }
    data[index] = temp;
 }
二叉堆的操作总结(insert+deleteMin+increaseKey+decreaseKey+percolateDown+percolateUp)_第9张图片 二叉堆的操作总结(insert+deleteMin+increaseKey+decreaseKey+percolateDown+percolateUp)_第10张图片

你可能感兴趣的:(二叉堆的操作总结(insert+deleteMin+increaseKey+decreaseKey+percolateDown+percolateUp))