package org.tanc.algorithm.heap.impl;

import org.tanc.algorithm.heap.BaseHeap;

/**
 * 二叉堆（数组实现）
 * <p>
 * （1）任意节点小于（或大于）它的所有后裔，最小元（或最大元）在堆的根上（堆序性）。
 * （2）堆总是一棵完全树。即除了最底层，其他层的节点都被元素填满，且最底层尽可能地从左到右填入。
 * <p>
 * Created by tanc on 2017/2/14.
 */
public class BinaryHeapArrayImpl<E extends Comparable<? super E>> implements BaseHeap<E> {

    private E[] heap;
    private int count;

    @SuppressWarnings("unchecked")
    public BinaryHeapArrayImpl() {
        heap = (E[]) new Comparable[24];
        count = 0;
    }

    @Override
    public E deleteMin() {
        return null;
    }

    @Override
    public void insert(E element) {

        if (count == 0) {
            heap[0] = element;
            count++;
            return;
        }

        int index = ++count;
        for (; (index / 2 > 0) && (heap[index / 2 - 1].compareTo(element) > 0); index /= 2) {
            heap[index - 1] = heap[index / 2 - 1];
        }
        heap[index - 1] = element;
    }
}

@@ -326,24 +326,57 @@
 eHeap%3CE%3E %7B%0A%0A
+    // %E6%95%B0%E7%BB%84%E7%AC%AC%E4%B8%80%E4%BD%8D%E6%B2%A1%E6%9C%89%E4%BD%BF%E7%94%A8%EF%BC%8C%E7%AC%AC%E4%B8%80%E4%B8%AA%E6%8F%92%E5%85%A5%E5%85%83%E7%B4%A0%E4%BB%8E%E4%B8%8B%E6%A0%87 1 %E5%BC%80%E5%A7%8B%0A
     private 
@@ -669,118 +669,8 @@
 ) %7B%0A
-%0A        if (count == 0) %7B%0A            heap%5B0%5D = element;%0A            count++;%0A            return;%0A        %7D%0A%0A
     
@@ -711,30 +711,27 @@
 or (
-; (index / 2 %3E 0) && (
+heap%5B0%5D = element; 
 heap
@@ -740,20 +740,16 @@
 ndex / 2
- - 1
 %5D.compar
@@ -764,17 +764,16 @@
 ent) %3E 0
-)
 ; index 
@@ -798,28 +798,24 @@
   heap%5Bindex
- - 1
 %5D = heap%5Bind
@@ -820,20 +820,16 @@
 ndex / 2
- - 1
 %5D;%0A     
@@ -855,12 +855,8 @@
 ndex
- - 1
 %5D = 

update binary heap array implement insert method