Binary Heap - A. Base Priority Queue

Posted by kate_rose on Wed, 02 Feb 2022 01:39:44 +0100

Definition:

Binary heap is a special kind of heap. Binary heap is either a complete binary tree (binary tree) or a nearly complete binary tree (binary tree). There are two kinds of binary heaps: the maximum heap and the minimum heap. Maximum heap: The parent node's key value is always greater than or equal to the key value of any child node; Minimum heap: The parent node's key value is always less than or equal to the key value of any child node.

Scope of use:

Read-write algorithm complexity is O(logn), which is a relatively balanced data structure used for priority queues, ensuring that the header nodes are optimal, and then the nodes are out of order, performing tree depth rearrangement after each acquisition.

Direct Up Code - Binary Heap Implementation

package com.zby.calc;

import java.util.Random;

/**
 *
 * Maximum heap: Compare algorithms, maximum on top
 * Minimum heap: the smallest on top
 * Algorithmic Complexity O(log n)
 *
 * @author zby
 * @time 2021/6/8 14:36
 * ****************************************
 */
public class A_BinaryHeap<V extends Comparable<V>> {
    private V[] root;
    // Current capacity
    private int size;

    public A_BinaryHeap(V[] root)  {
        this.root = root;
    }
    /**
     *
     * @desc Take out the queue head
     ******************************************
     */
    public V removeTop() {
        V backdata = root[1];
        if(backdata==null)
            return null;
        root[1] = root[size];
        root[size] = null;
        size--;
        sink(1);
        return backdata;
    }
    /**
     * 
     * @desc insert
     ******************************************
     */
    public void add(V v) {
        size++;
        root[size] = v;
        swim(size);
    }
    /**
     * 
     * @desc Exchange Object
     ******************************************
     */
    public void exchangeV(int index1,int index2) {
        V v = root[index1];
        root[index1] = root[index2];
        root[index2] = v;
    }
    /**
     *
     * @desc Sink
     ******************************************
     */
    public void sink(int index) {
        if(2*index>size)
            return;
        int changeIndex = 2*index;
        if(size>=2*index+1&&root[2*index].compareTo(root[2*index+1])<0)
            changeIndex = 2*index+1;
        if(root[index].compareTo(root[changeIndex])>=0)
            return;
        exchangeV(index,changeIndex);
        sink(changeIndex);
    }
    /**
     *
     * @desc Floating Up
     ******************************************
     */
    public void swim(int index) {
        if(index<=1)
            return;
        if(root[index/2].compareTo(root[index])<0) {
            exchangeV(index / 2, index);
            swim(index / 2);
        } else
            return;
    }
    /**
     *
     * @desc main
     ******************************************
     */
    public static void main(String[] args) {
        A_BinaryHeap<Student> ss = new A_BinaryHeap<>(new Student[1000]);
        for (int i = 0; i < 10; i++) {
            ss.add(new Student(new Random().nextInt(1000)));
//            System.out.println("--------------------------------------------");
//            for (int i1 = 0; i1 < ss.root.length; i1++) {
//                Student s = ss.root[i1];
//                if(s==null)
//                    continue;
//                System.out.println(i1+"--------"+s.getIq());
//            }
        }
        System.out.println("Take out the data:");
        for (int j=0;j<100;j++) {
            Student student = ss.removeTop();
            if(student==null)
                continue;
            System.out.println(student.getIq());
        }
    }

    static class Student implements Comparable<Student> {
        int iq;
        public Student(int iq) {
            this.iq = iq;
        }
        public int getIq() {
            return iq;
        }
        public int compareTo(Student o) {
            return this.getIq()-o.getIq();
        }
    }

}

  1. When a new node is added, it is placed at the end, traversed up, and floated up to the highest position. Equivalent to dichotomy, the more data, the faster.
  2. After getting the head node, fill the root node of the size position into the head node and begin the sink calculation.
  3. The advantage is that the whole number is placed in an array, the parent node's current node index/2, the left child node 2index, and the right child node==2index+1. The whole number can be calculated quickly.
  4. This structure is suitable for inserting results in random locations, unlike hero rating leaderboards, which wear a piece of equipment plus points, but it is not enough to shake the whole tree, float very small, generally float or sink 1,2 units, there is a high probability that it will not change, and of course it will not get a sequential data structure.
  5. In summary, it is more appropriate to only get the highest expected value and then insert it later with a large variation. This sort is local, not full-field.

Topics: Java Algorithm data structure queue