[soft test] data structure - heap structure (small top heap, big top heap and binary heap)

1. What is a heap?

The heap structure is a tree-shaped data structure based on a complete binary tree with special properties. The heap
is a complete binary tree, namely

  • Except for the last layer, the nodes of other layers are full
  • The nodes of the last layer are arranged from left to right.

Two, the characteristics of the heap

A heap has the following characteristics: ordered

2.1 orderly

A heap is an ordered data structure that can perform insertion, deletion, and lookup operations in O(log n) time.

2.2 Good performance

By maintaining the value size relationship between nodes, the heap structure can maintain the nature of the heap in operations such as insertion and deletion, and has better performance.

3. Heap insertion and deletion operations

3.1 Heap insertion operation

The insertion operation of the heap is relatively simple.
The insertion operation of the heap only needs to
(1) insert a new node into the last position of the last layer
(2) perform a floating operation (Heapify Up) to maintain the nature of the heap.

3.2 Heap delete operation

The deletion operation of the heap is a little more complicated, and
the insertion operation of the heap needs to be processed in two cases:

3.2.1 The deleted node is a leaf node

Just delete the node directly.

3.2.2 The deleted node is not a leaf node

Find the successor node (that is, the minimum value in its right subtree) or the predecessor node (that is, the maximum value in its left subtree) of the node to be deleted, replace the node to be deleted with this node, and then replace the replaced node Perform a sinking operation (Heapify Down) to maintain the nature of the heap.

4. Common heap structures

Small top heap, big top heap and binary heap are common heap structures.

4.1 Big Top Heap

4.1.1 Introduction to Big Top Heap

Max Heap is a special heap structure.
The value of each node of the Max Heap is greater than or equal to the value of its child nodes.
Max Heap is usually used to implement a priority queue, in which the top element of the heap is the most priority Elements.

The big top heap is a binary heap that is greater than or equal to the parent node, that is, the value of each node is not less than the value of its parent node.
The nature of the big top heap is that the top element of the heap is not smaller than its child nodes, that is, the value of the top of the heap is not less than the value of its child nodes.

4.1.2 Insertion, deletion and search operations of the big top heap

The insertion, deletion, and search operations of the large top heap can be implemented using the following functions:

  • Insertion operation: When inserting a node, first check whether the bottom of the heap is empty, if so, insert the new node directly into the bottom of the heap; if the bottom of the heap is not empty, insert the new node into the left or right subtree at the bottom of the heap , and make color adjustments and rotations to ensure the nature of the big top pile.
  • Delete operation: When deleting a node, first check whether the top of the heap is the node to be deleted, if so, replace the largest node among the child nodes on the top of the heap, and perform color adjustment and rotation to ensure the nature of the big top heap .
  • Search operation: When searching for a node, first check whether the top of the heap is the node to be searched, if yes, return the top node of the heap; if the top of the heap is not the node to be searched, continue to search in the left subtree or right subtree until Until the node you are looking for is found or the subtree is empty.

4.2 Small top heap

4.2.1 Introduction of Small Top Heap

Small top heap (Min Heap) is also a special heap structure.
The value of each node of the small top heap is less than or equal to the value of its child nodes.
The small top heap is usually used to implement a priority queue, in which the top element of the heap is the least priority Elements.
The small top heap is a binary heap that is less than or equal to the parent node, that is, the value of each node is not greater than the value of its parent node. The nature of the small top heap is that the top element of the heap is not greater than its child nodes, that is, the value at the top of the heap is not greater than the value of its child nodes.

4.2.2 Insertion, deletion and search operations of the small top heap

The insertion, deletion, and search operations of the small top heap can be implemented using the following functions:

  • Insertion operation: When inserting a node, first check whether the bottom of the heap is empty, if so, insert the new node directly into the bottom of the heap; if the bottom of the heap is not empty, insert the new node into the left or right subtree at the bottom of the heap , and make color adjustments and rotations to ensure the nature of the small top pile.
  • Delete operation: When deleting a node, first check whether the top of the heap is the node to be deleted, if yes, replace the top of the heap with the smallest node among the child nodes on the top of the heap, and perform color adjustment and rotation to ensure the nature of the small top heap .
  • Search operation: When searching for a node, first check whether the top of the heap is the node to be searched, if yes, return the top node of the heap; if the top of the heap is not the node to be searched, continue to search in the left subtree or right subtree until Until the node you are looking for is found or the subtree is empty.

4.3 Binary heaps

4.3.1 Introduction to Binary Heap

A binary heap is a type of binary tree in which each node has two children, divided into a left child and a right child.
The nature of the binary heap is that the value of the parent node is not less than the value of its child nodes, that is, the value of the parent node is not greater than the value of its child nodes.
In a binary heap, the largest (or smallest) node is called the top (or bottom) of the heap.
In a binary heap, the top and bottom of the heap point to the largest (or smallest) element of the heap, respectively.
The insertion, deletion and search operations of the binary heap can be implemented using the following functions:

4.3.2 Insertion, deletion and search operations of binary heap

  • Insertion operation: When inserting a node, first check whether the bottom of the heap is empty, if so, insert the new node directly into the bottom of the heap; if the bottom of the heap is not empty, insert the new node into the left or right subtree at the bottom of the heap , and color adjustment and rotation are performed to ensure the properties of the binary heap.
  • Deletion operation: When deleting a node, first check whether the top of the heap is the node to be deleted, if so, replace the largest (or smallest) node among the child nodes at the top of the heap, and perform color adjustment and rotation to ensure two The nature of the fork pile.
  • Search operation: When searching for a node, first check whether the top of the heap is the node to be searched, if yes, return the top node of the heap; if the top of the heap is not the node to be searched, continue to search in the left subtree or right subtree until Until the node you are looking for is found or the subtree is empty.

Guess you like

Origin blog.csdn.net/wstever/article/details/129978013