Heap & Heap Row

1. Heap: When 
talking about the heap, we must first understand the binary tree:

- Binary tree:
As the name suggests, each node has at most two subtrees (the maximum degree is 2). Trees do not have left and right, but binary trees have left and right. The nth layer has at most 2 to the nth power -1 nodes or 2**n - 1.
Binary tree is divided into:
a. Empty binary tree
b. Only the root node
c. Left or right subtree is empty
d. Neither left or right subtree is empty

Full binary tree:
depth is k, and there are 2 to the k power -1 node It is called a full binary tree, that is, a d

complete binary tree:
a binary tree with n nodes of depth k, compared with a full binary tree with the same depth of k, if the nodes labeled 1~n correspond one by one, it is a complete binary

tree . Not a tree: (for the following reasons)
A tree is defined in graph theory as a graph with n-1 edges connecting n vertices. The vertex set of the graph is not empty, so the vertices of the tree are not empty. An N-ary tree is defined in graph theory, which can be an empty tree.

- Binary tree storage:
Complete binary tree:
Stored in an array, the most space-saving and simplest storage method. Node subscripts are zero-based. The subscript of the i node is i, that is, the subscript of the 0th node is 0, and the subscript of the nth node is n.
The subscripts of its left and right subtree nodes are 2 * i + 1 and 2 * i + 2 .

General binary tree:
In order to reflect the relationship between the nodes of the binary tree, it should also follow the complete binary tree for array numbering. Therefore, storage wastes a lot of space, and there will be many null values ​​in the array.

Therefore, it is usually stored in the form of a binary linked list: left child pointer field: lchild, data field: data, right child pointer field: rchild.
If you use a ternary linked list, you need to add another parent pointer field to specify the parent.

- Binary heap:
1. The key of the parent node is always greater than or equal to (less than or equal to) the key of any child node. (It is the maximum heap if it is greater than the child node)
2. The left subtree and right subtree of each node are a binary heap (both are max heap or min heap, also known as big root heap and small root heap).

Several other heaps (binomial heaps, Fibonacci heaps, etc.) are used less often, and binary heaps are generally referred to as heaps for short.

Second, the series of operations on the
heap 1. The concept and storage
of the heap As above, the binary heap is generally represented by an array, which is stored in the sequential storage method of a complete binary tree.
Heap-heap structure:
#define HeapSize 128
typedef struct minHeap{ #The small root heap defines
HElemType heap[HeapSize]; #The array that stores the elements in the small root heap
int n #The number of current elements of the small root heap
}

2. The establishment of the heap :
先视为完全二叉树顺序存储,此时它并不满足是一个小根堆或是大根堆。故要进一步调整为堆化树。

例:现有元素初始排列为{53,17,78,09,45,65,87,23},初次结构如下,但是初始的树不满足大根堆和小根堆的条件,因此调整如下:

如上即初步调整成为小根堆。
 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325362044&siteId=291194637