[Data structure] Creation of heap

1. Concept and structure of heap

1. What is a heap?

The heap is a data structure that stores elements in the sequential storage method of a binary tree, and at the same time, the data stored in the father node must be larger than the data stored in the son node, or the data stored in the father node must be smaller than the data stored in the son node. The heap with the largest root node is called the maximum heap or large root heap, and the heap with the smallest root node is called the minimum heap or small root heap.

2. Nature of heap

  • The value of a node in the heap is always no greater than or no less than the value of its parent node
  • The heap is always a complete binary tree
  • The root node of the heap is always an extreme value

3. Heap structure and classification

Insert image description here
Small heap: all parent nodes are smaller than the child nodes, and the root node is the smallest
Insert image description here
Big pile: all parent nodes are larger than the child nodes, and the root node is the largest

2. Creation of heap

The heap structure used in the following code:

typedef int HPDataType;
typedef struct Heap
{
    
    
	HPDataType* a;
	int size;
	int capacity;
}Heap;

Important relationships:
Insert image description here

1. Heap downward adjustment algorithm

For example, there is an array:

int arr[] = {
    
    27,15,19,18,28,34,65,49,25,37};

This array is logically viewed as a complete binary tree, and it can be adjusted into a large or small heap through a downward adjustment algorithm starting from the root node.
The downward adjustment algorithm has a premise: the left and right subtrees must be a heap before they can be adjusted.

Insert image description here
Insert image description here

The following code is adjusted to a small heap using the downward adjustment method:

//向下调整
void AdjustDown(HPDataType* a, int n, int parent)
{
    
    
	//先默认左孩子是较小的
	int child = parent * 2 + 1;

	while (child < n)
	{
    
    
		//找出左右孩子中较小的
		if (child + 1 < n && a[child + 1] < a[child])
		{
    
    
			child++;
		}
		//孩子节点更小则交换
		if (a[child] < a[parent])
		{
    
    
			Swap(&a[child], &a[parent]);
			parent = child;
			child = parent * 2 + 1;
		}
		else
		{
    
    
			break;
		}
	}
}

2. Heap upward adjustment algorithm

For example, for the following small heap:
Insert image description here
after inserting the number 1 into the small heap, it is no longer a small heap, and it needs to be adjusted using an upward adjustment algorithm starting from this element.

There is a premise for upward adjustment: the previous element is a heap

Insert image description here
The following code is adjusted to a small heap using the upward adjustment method:

//向上调整
void AdjustUp(HPDataType* a, int child)
{
    
    
	//找出双亲的下标
	int parent = (child - 1) / 2;

	while (child>0)
	{
    
    
		//孩子结点比双亲小则交换
		if (a[child] < a[parent])
		{
    
    
			Swap(&a[child], &a[parent]);
			child = parent;
			parent = (child - 1) / 2;
		}
		else
		{
    
    
			break;
		}
	}
}

3. Creation of heap (upward adjustment algorithm)

There are prerequisites for using the upward adjustment algorithm and the downward adjustment algorithm. For an array, if you want it to be regarded as a heap in terms of logical structure, you can first make it meet the conditions of one of the two algorithms. Then make adjustments.

For a single node, it can be regarded as either a large heap or a small heap. Therefore, the array elements can be adjusted sequentially through the upward adjustment algorithm. The element before the adjustment must be a heap. If the conditions are met, the following is Method code:

// 堆的构建
void HeapCreate(Heap* hp, HPDataType* a, int n)
{
    
    
	assert(hp);
	assert(a);

    //开辟存放堆元素的内存空间
	hp->a = (HPDataType*)malloc(sizeof(HPDataType) * n);
	if (hp->a == NULL)
	{
    
    
		perror("malloc fail");
		exit(-1);
	}

	hp->size = n;
	hp->capacity = n;

    //将数组元素复制到堆中存放元素的内存空间内
	memcpy(hp->a, a, sizeof(HPDataType) * n);

    //从下标1开始进行向上调整
	for (int i = 1; i < n; i++)
	{
    
    
		AdjustUp(hp->a, i);
	}
}

Insert image description here

Guess you like

Origin blog.csdn.net/zcxyywd/article/details/132828015