Data Structures and Algorithms - Basic Storage of the Heap

Table of contents

1. Concept and introduction

2. Applicable instructions

3. Structural diagram

4. Java example code

Five. The difference between the heap and the stack


1. Concept and introduction

Heap is a general term for a special class of data structures in computer science.

The heap is usually an array of objects that can be viewed as a complete binary tree.

A heap satisfies the following properties:

  • The value of a node in the heap is always not greater than or not less than the value of its parent node.
  • The heap is always a complete binary tree.

2. Applicable instructions

The heap uses a complete binary tree structure to maintain a set of data, and then performs related operations. The time complexity of a general operation is between O(1)~O(logn). The heap is usually used for dynamic allocation and release programs. The object used.

If it is a priority queue usage scenario, ordinary arrays or sequential arrays, the worst case is O(n^2), and the heap data structure can also improve the efficiency of entering and exiting the queue.

3. Structural diagram

A binary heap is a complete binary tree, and the value of a node in the heap is always not greater than the value of its parent node. The depth of the complete binary tree is k. Except for the kth layer, other layers (1~k-1 ) reaches the maximum number of nodes, and all the nodes of the kth layer are continuously concentrated on the far left.

The maximum root node of the heap is called the maximum heap, as shown in the following figure:

We can use an array to store a binary heap, and the label on the right is the index of the array

 

Assuming that the index position of the current element is i, the rule can be obtained:

parent(i) = i/2 (rounded)
left child(i) = 2*i
right child(i) = 2*i +1

 4. Java example code

package runoob.heap;

/**
 * 堆定义
 */
public class MaxHeap<T> {
    private T[] data;
    private int count;
    // 构造函数, 构造一个空堆, 可容纳capacity个元素
    public MaxHeap(int capacity){
        data = (T[])new Object[capacity+1];
        count = 0;
    }
    // 返回堆中的元素个数
    public int size(){
        return count;
    }
    // 返回一个布尔值, 表示堆中是否为空
    public boolean isEmpty(){
        return count == 0;
    }
    // 测试 MaxHeap
    public static void main(String[] args) {
        MaxHeap<Integer> maxHeap = new MaxHeap<Integer>(100);
        System.out.println(maxHeap.size());
    }
}

Five. The difference between the heap and the stack

  • Stack Stack: It is private, and a stack is created every time a thread is created, and the data stored in the stack is the local basic type data in the current thread (eight basic types defined in java: boolean, char, byte, short, int , long, float, double), non-basic type objects only store an address pointing to the heap on the JVM stack
  • Heap: The area used by the JVM to store object instances and array values. It can be considered that the memory of all objects created by new in Java is allocated here, and the memory of objects in the Heap needs to wait for GC to reclaim

Guess you like

Origin blog.csdn.net/weixin_68773927/article/details/129720259