大小堆的建立



#include<iostream>
#include<cstdlib>
#include<vector>
#include<cassert>
using namespace std;
template<class T>
struct Less
{
bool operator()(T& l,T& r)
{
return l<r;
}
};
template<class T>
struct Great
{
bool operator()(T& l,T& r)
{
return l>r;
}


};
template<class T,template<class> class Compare>
class Heap
{
public:
Heap(const T* a,size_t size)
{
int i=0;
_a.reserve(size);
for(i=0;i<size;i++)
{
_a.push_back(a[i]);
}
for(i=(size-2)/2;i>=0;i--)
{
AdjustDown(i);          //默认大堆建立
}
Display();
}
size_t Size()
{
return _a.size();
}


bool Empyt()
{
return _a.size()==0;
}
void Insert(T& t)
{
_a.push_back(t); 
size_t size=_a.size();
AdjustUp(size-1);
Display();


}
void Delete()
{
size_t size=_a.size();  
        assert(size>0);  
swap(_a[0];_a[size-1]);
_a.pop_back();
AdjustDown(0);
Display();
}
void Display()
{
if(_a.size()!=0)
{
  for(int i=0;i<_a.size();i++)
  {
  cout<<_a[i]<<" ";
  
  }
}
cout<<endl;
}
void Sort()
{
_Sort(_a.size()-1);
Display();

}
protected:
void AdjustDown(size_t parent)            //向下调整算法建立大堆
{
size_t child =parent*2+1;  
        while(child<_a.size())  
        {  
            Compare<T> com;  
        if(child+1<_a.size()&&com(_a[child+1],_a[child]))  
        {  
            ++child;  
        }  
            if(com(_a[child],_a[parent]))  
            {  
            swap(_a[parent],_a[child]);  
            parent=child;  
            child=parent*2+1;  
        }  
        else  
            break;  
}
}
void AdjustUp(size_t child)              //向上调整算法建立小堆
{
assert(!child>_a.size);
Compare<T> com; ;
int parent=(child-1)/2;
while(child>0)
{
if(com(_a[child],_a[parent]))
{
swap(_a[child],_a[parent]);
child=parent;
parent=(child-1)/2;
}
else
break;
}
}
void _Sort(size_t child)
{
int parent=(child-1)/2;
while(child>0)
{
if(_a[child]<_a[parent])
{
swap(_a[child],_a[parent]);
   child=parent;
}
if(_a[child-1]<_a[child])
{
swap(_a[child-1],_a[child]);
}
else 
break;
}

}
private:
vector<T> _a;


};
int main()
{
int a[]= {10,11, 13, 12, 16, 18, 15, 17, 14, 19};
Heap<int,Less> heap(a,sizeof(a)/sizeof(a[0]));
system("pause");
return 0;
}

### Java 中的手动实现大根堆和小根堆 #### 大根堆的实现 为了构建一个大根堆,在Java中需要定义一个类来管理数组并提供必要的方法。以下是创建大根堆的具体方式: ```java public class MaxHeap { private int[] heap; private int size; public MaxHeap(int capacity) { this.heap = new int[capacity]; this.size = 0; } // 插入新元素到最大堆中 public void insert(int value) { if (size >= heap.length) throw new RuntimeException("Heap is full"); heap[size++] = value; siftUp(size - 1); } // 上浮操作,用于维护最大堆性质 private void siftUp(int index) { while ((index - 1) / 2]) { swap(index, (index - 1) / 2); index = (index - 1) / 2; } } // 删除最大值(即堆顶) public int extractMax() { if (size <= 0) throw new RuntimeException("Heap is empty"); int maxVal = heap[0]; heap[0] = heap[--size]; // 将最后一个节点移到顶部 siftDown(0); return maxVal; } // 下沉操作,同样是为了保持最大堆特性 private void siftDown(int index) { int largestIndex = index; int leftChildIndex = 2 * index + 1; int rightChildIndex = 2 * index + 2; if (leftChildIndex < size && heap[leftChildIndex] > heap[largestIndex]) largestIndex = leftChildIndex; if (rightChildIndex < size && heap[rightChildIndex] > heap[largestIndex]) largestIndex = rightChildIndex; if (largestIndex != index) { swap(largestIndex, index); siftDown(largestIndex); } } // 辅助函数交换两个位置上的数值 private void swap(int i, int j) { int temp = heap[i]; heap[i] = heap[j]; heap[j] = temp; } } ``` 这段代码展示了如何在一个固定大小的数组内建立和维护一个大根堆[^1]。 #### 小根堆的实现 对于小根堆而言,只需要改变比较条件即可转换上述的大根堆逻辑。具体来说就是在`insert()` 和 `extractMin()` 方法里以及`siftUp()` 和 `siftDown()` 的内部判断语句处修改相应的不等号方向: ```java // 修改后的上浮操作适用于最小堆 private void siftUpForMinHeap(int index) { while ((index - 1) / 2 >= 0 && heap[index] < heap[(index - 1) / 2]) { swap(index, (index - 1) / 2); index = (index - 1) / 2; } } // 修改后的下沉操作也适应于最小堆 private void siftDownForMinHeap(int index) { int smallestIndex = index; int leftChildIndex = 2 * index + 1; int rightChildIndex = 2 * index + 2; if (leftChildIndex < size && heap[leftChildIndex] < heap[smallestIndex]) smallestIndex = leftChildIndex; if (rightChildIndex < size && heap[rightChildIndex] < heap[smallestIndex]) smallestIndex = rightChildIndex; if (smallestIndex != index) { swap(smallestIndex, index); siftDownForMinHeap(smallestIndex); } } ``` 通过这种方式可以在相同的数据结构基础上轻松切换成一个小根堆[^4]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值