Data Structures and Algorithms heap

  • Binary sequential storage
  • stack
  • Application heap

Binary sequential storage

      Use array holds binary tree, the binary tree method is about a sequence traversal manner into an array. Generally only suited for representing binary tree, because there will be a waste of non-complete binary tree space, the main use of this approach is the representation of the heap.
 
Subscript relationship:
Known parent (parent) subscript, then: left child (left) subscript parent +. 1 * 2 = ; the right child (right) parent superscript * 2 + 2 = ;
Known child (no distinction between left and right) (Child) subscript, then: parents (parent) index = (Child -. 1) / 2 ;
 

stack:

Some definitions heap:

1. The heap is a logically complete binary tree
2. The heap is physically stored in an array
3. meet any node values are greater than the value of the sub-tree nodes, called piles or large root stack, or the maximum stack; conversely, it is small pile or heap rootlets, or minimum heap
The basic function of the stack is set quickly find the best value
6 . Rough estimate, it may be considered to build the stack downward adjustment performed in the cycle, is O (n * log (n) ), in fact O (n)
 
Heap creation and operation: downward adjustment
 
1. The premise: left and right subtrees must already be a heap in order to adjust.
 
Analysis of ideas:
1) First, there must be an array, the array assignment
2) be adjusted downward (last to first find a parent node)
      Pointing to child nodes define a larger child elements, the elements when the child is greater than the root element, both the exchange. Pay attention not only to compare sub-trees, and compared subtree tree
 

public class TestHeap {
    public int[] elem;
    public int usedSize;

    public TestHeap() {
        this.elem = new int[10];
        this.usedSize = 0;
    }

    //创建一个最大堆
    public void creatHeap(int[] array) {
        //赋值
        for (int i = 0; i < array.length; i++) {
            this.elem[i] = array[i];
            this.usedSize++;
        }

        //向下调整的方式进行调整 i代表每一棵树的根节点
        for (int i = (this.usedSize-1-1)/2; i >= 0 ; i--) {
            //是每一棵树都按照向下调整的方式进行调整
            AdjustDown(i,this.usedSize);
        }
    }

    public void AdjustDown(int root, int length) {
        //root是向下调整的父节点 调整child是向下调整的子节点的最大值
        int child = 2*root+1;
        while (child < length) {
            if (child+1 < length && this.elem[child] < this.elem[child+1]) {
                child = child+1;
            }

            if (this.elem[child] > this.elem[root]) {
                int tmp = this.elem[child];
                this.elem[child] = this.elem[root];
                this.elem[root] = tmp;
                root = child;  //调整子树以下所有的子树
                child = 2*root+1;
            } else {
                break;
            }
        }
    }

    public void show() {
        for (int i = 0; i < this.usedSize; i++) {
            System.out.print(this.elem[i] +" ");
        }
        System.out.println();
    }
 }

Application heap

1, priority queue
Analysis of ideas:
a. Locate the last parent
b. Make comparison, if the left node and a right node is greater than the larger of the parent node, the parent node and child nodes are exchanged large
. C Note that for comparison sub node and the child node's child child = root root = (child-1) / 2;
 
Into the queue:
    //入队列
    public boolean isFull() {
        return this.usedSize == this.elem.length;
    }

    //开始向上调整
    public void AdjustUp(int child) {
        int root = (child-1)/2;
        while (child > 0) {
            if(this.elem[child] > this.elem[root]) {
                int tmp = this.elem[child];
                this.elem[child] = this.elem[root];
                this.elem[root] = tmp;
                child = root;
                root = (child-1)/2;
            }else {
                break;
            }
        }
    }
    
    public void pushHeap(int val) {
        if (isFull()) {
           this.elem = Arrays.copyOf(this.elem, this.elem.length*2);
        }
        this.elem[usedSize] = val;
        this.usedSize++;
        //开始向上调整
        AdjustUp(this.usedSize-1);
    }

The queue:

Thinking Analysis:
A the head elements and the tail element exchange.
B.usedSize--;
C is adjusted upward.

    //出队列
    public boolean isEmpty() {
        return usedSize == 0;
    }

    public void popHeap() {
        if (isEmpty()) {
            return;
        }
        int tmp = this.elem[0];
        this.elem[0] = this.elem[usedSize-1];
        this.elem[usedSize-1] = tmp;
        this.usedSize--;
        AdjustDown(0, this.usedSize);
    }

The first element of the queue obtained:

    public int getHeapTop() {
        if(isEmpty()) {
            return -1;
        }
        return this.elem[0];
    }

HEAPSORT:

   public void heapSort() {
        int end = this.usedSize-1;
        while (end > 0) {
            int tmp = this.elem[end];
            this.elem[end] = this.elem[0];
            this.elem[0] = tmp;
            AdjustDown(0,end);
            end--;
        }

java among the priority queues : PriorityQueue the implements Queue

        Queue<Integer> queue = new PriorityQueue<>();  //默认为最小根堆
        queue.offer(1);
        queue.offer(2);
        System.out.println(queue.poll()); //1
        System.out.println(queue.peek()); //2

2, TOP-K problem

Top-K, also known as the problem of massive data problem: eg there are one hundred million data, k largest / smallest first k before looking for the? ? ?

If you find a note before the k largest, to establish the size of a small heap k. Before looking for the k smallest, build piles of size k.

Analysis of ideas: the sequence of elements to be found before the establishment of k elements piles / small pile of (for example), and the top of the heap every element of comparison. If the array elements is greater than the top of the stack elements to be replaced, followed by downward adjustment (to the top element, and then into the end of the stack).

3, heap sort the problem *

From small to large == "piles, descending ==" small heap

Published 51 original articles · won praise 14 · views 2305

Guess you like

Origin blog.csdn.net/qq_41185460/article/details/103647001