Database - 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. This is the main ways is to use heap representation.

        Complete binary tree with the nodes between the array subscript relationship:

  1. In the case of the known parent node
  • Children left index: left = 2 * parent + 1
  • Right child nodes: right = 2 * parent = 2

     2. known to have left the child or children

  • Parent node subscripts: parent = (child-1) / 2

stack

  1. Stack is logically a complete binary tree
  2. Stack is physically stored in an array
  3. Large values ​​of the binary tree nodes to any of its left subtree node and the right child, the stack of piles (pile large root), whereas a small heap (heap rootlets)

Heap of related operations

  1. downward adjustment (time complexity of O (log (n)))

  Provided: when the left and right subtrees stack are ordered (stack) can be adjusted downward to the specified node.

  operating:

1. index 如果已经是叶子结点,则整个调整过程结束
   判断 index 位置有没有孩子
   因为堆是完全二叉树,没有左孩子就一定没有右孩子,所以判断是否有左孩子
   因为堆的存储结构是数组,所以判断是否有左孩子即判断左孩子下标是否越界,即 left >= size 越界
2. 确定 left 或 right,谁是 index 的最小孩子 min
   如果右孩子不存在,则 min = left
   否则,比较 array[left] 和 array[right] 值得大小,选择小的为 min
3. 比较 array[index] 的值 和 array[min] 的值,如果 array[index] <= array[min],则满足堆的性质,调整结束。否则,交换 array[index] 和 array[min] 的值
4. 然后因为 min 位置的堆的性质可能被破坏,所以把 min 视作 index,向下重复以上过程

 Code: 

public static void shiftDownSmall(int []array,int size,int index) {
        int left = 2 * index + 1;//根据下标找到左孩子
        while (left < size) {
            int right=left+1;
            int min=left;
            if(right<size) {//右孩子存在,则与左孩子进行比较 找到最小孩子
                if (array[min] > array[right]) {
                    min = right;
                }
            }
            if (array[index] > array[min]) {//如果孩子结点的值小于指定下标 进行交换
                    swap(array, index, min);
                    index = min;//更新节点的值
                    left = 2 * index + 1;//继续计算结点下标 进行向下调整
            }else
                    break;
        }
    }//对给定下标位置元素进行小堆排序

  2. Construction of the stack (time complexity of O (n * log (n)))

  Prerequisite: left and right subtrees disorder that is not a heap

  Operation: Start from a leaf node of a binary tree after finding its parent node, in order to be adjusted backwards back down

  Code:

    public static void createHeapSmall(int []array,int size){
        for(int i=(size-2)/2;i>=0;i--)
        {
            shiftDownSmall(array,size,i);
        }
    }//建小堆
    // 因为堆本身就是数组 而此时的堆是无序的 不是小堆 也不是大堆
    // 建堆需要从最后一个节叶子点的双亲结点开始建堆
    public static void createHeapBig(int []array,int size){
        for(int i=(size-2)/2;i>=0;i--)
        {
            shiftDownBig(array,size,i);
        }
    }//建大堆

 

Published 40 original articles · won praise 4 · Views 870

Guess you like

Origin blog.csdn.net/weixin_44919969/article/details/101459720