Heap structure to achieve

Heap structure Introduction

Heap is a data structure that is logically a complete binary tree, is stored in an array. Heap big pile top, and the top of the heap of small points, the big top stack: For each parent node, the left and right child nodes are smaller than the parent, so the root is the top of the heap is saved in the largest heap element; minor vertex stack: the value of each node is less than the parent's child nodes, the top of the stack to save the smallest element stack.

Heap memory

Heap has been described above is to use an array to store, but how to keep it? In fact, the complete binary tree traversal sequence after layer turn into an array which is the same, such as following a complete binary tree:
image.png
the corresponding storage array:
image.png
in addition we also need to know the parent and child nodes in the array Some relational indexes:
the current node index iis:
left child index: 2 * i + 1
right child index: 2 * i + 2
parent index:(i - 1) / 2

The basic operation of the stack (here, a large stack top Case)

Float: the current position of the icorresponding element is moved upward, the parent node if the value is less than the current exchange, then the current position is updated to the location of the parent node, the parent node is determined, and then continue the process until it is repeated until the exchange.
Code corresponding to:

void seekUp(int i)
{
	while (i >= 0)
	{
		if (heap[i] > heap[(i - 1) / 2])
			swap(heap[i], heap[(i - 1) / 2]);
		else
		    return;
		i = (i - 1) / 2;
	}
}

Sinking: the current position of the icorresponding element is moved downward, if the child node is greater than the current value exchanged (if the value is greater than the left and right child nodes of the parent node of the child node is selected from the maximum value), then the current position is updated to be exchanged the position of the child node, the child node and then continue the process until the determination is repeated until the exchange does not.

void seekDown(int i)
{
	while (i < n)
	{
        int maxIdx = i;
        int leftIdx = i * 2 + 1;
        int rightIdx = i * 2 + 2;
        if(leftIdx < n && heap[leftIdx] > heap[maxIdx]) maxIdx = leftIdx;
        if(rightIdx < n && heap[rightIdx] > heap[maxIdx]) maxIdx = rightIdx; 
        if(maxIdx != i) swap(heap[maxIdx], heap[i]);
        else return;
	}
}

Delete: The last element of the array assigned the top of the heap and then lower operating

void deleteHeap()
{
	heap[0] = heap[--n];
	seekDown(0);
}

Insert: Add a metadata at the end of the array and then float operation

void insertHeap(int val)
{
	heap[n++]=val;
	seekUp(n-1);
}

Overall Code

class HEAD
{
public:
	HEAD() { n = 0; heap.resize(1000, 0); }
	HEAD(vector<int> v) :heap(v) { n = v.size(); }
	//插入
	void insertHeap(int val)
	{
		heap[n++] = val;
		seekUp(n - 1);
	}
	//删除
	void deleteHeap()
	{
		heap[0] = heap[--n];
		seekDown(0);
	}
	//获取顶端的值
	int getVal()
	{
		if (n == 0)
			return INT_MIN;
		return heap[0];
	}
	//下沉
	void seekDown(int i)
	{
		while (i < n)
		{
			int maxIdx = i;
			int leftIdx = i * 2 + 1;
			int rightIdx = i * 2 + 2;
			if (leftIdx < n && heap[leftIdx] > heap[maxIdx]) maxIdx = leftIdx;
			if (rightIdx < n && heap[rightIdx] > heap[maxIdx]) maxIdx = rightIdx;
			if (maxIdx != i) swap(heap[maxIdx], heap[i]);
			else return;
		}
	}
	//上浮
	void seekUp(int i)
	{
		while (i >= 0)
		{
			if (heap[i] > heap[(i - 1) / 2])
				swap(heap[i], heap[(i - 1) / 2]);
			else
				return;
			i = (i - 1) / 2;
		}
	}
	void printHeap()
	{
		for (int i = 0; i < n; i++)
			cout << heap[i] << " ";
		cout << endl;
	}
private:
	vector<int> heap;
	int n;
};
int main()
{
	HEAD h;
	h.insertHeap(5);
	h.insertHeap(14);
	h.insertHeap(4);
	h.printHeap();
	h.deleteHeap();
	h.printHeap();
	return 0;
}

operation result:

14 5 4
5 4
Released eight original articles · won praise 1 · views 27

Guess you like

Origin blog.csdn.net/qq_43479736/article/details/105082520